Skip to content

Commit

Permalink
Diagnose the first and third operands of a C-style for loop
Browse files Browse the repository at this point in the history
as ignored expressions.

Swift SVN r32291
  • Loading branch information
rjmccall committed Sep 29, 2015
1 parent e27b1e7 commit 7eb830f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
TypeCheckExprFlags::IsDiscarded))
return nullptr;
FS->setInitializer(Initializer);
TC.checkIgnoredExpr(Initializer);
}

if (auto *Cond = FS->getCond().getPtrOrNull()) {
Expand All @@ -532,6 +533,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
TypeCheckExprFlags::IsDiscarded))
return nullptr;
FS->setIncrement(Increment);
TC.checkIgnoredExpr(Increment);
}

AddLabeledStmt loopNest(*this, FS);
Expand Down Expand Up @@ -964,6 +966,16 @@ bool TypeChecker::typeCheckCatchPattern(CatchStmt *S, DeclContext *DC) {
}

void TypeChecker::checkIgnoredExpr(Expr *E) {
// For parity with C, several places in the grammar accept multiple
// comma-separated expressions and then bind them together as an implicit
// tuple. Break these apart and check them separately.
if (E->isImplicit() && isa<TupleExpr>(E)) {
for (auto Elt : cast<TupleExpr>(E)->getElements()) {
checkIgnoredExpr(Elt);
}
return;
}

// Complain about l-values that are neither loaded nor stored.
if (E->getType()->isLValueType()) {
diagnose(E->getLoc(), diag::expression_unused_lvalue)
Expand Down
12 changes: 0 additions & 12 deletions test/SILGen/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,6 @@ func for_each_loop(x: [C]) {
_ = 0
}

// <rdar://problem/16650625>
func for_ignored_lvalue_init() {
var i = 0
for i; i < 10; ++i {}
}


// CHECK-LABEL: sil hidden @{{.*}}test_break
func test_break(i : Int) {
switch i {
Expand Down Expand Up @@ -353,11 +346,6 @@ func test_if_break(a : Bool) {
// CHECK: return
}

// rdar://problem/18643692
func for_loop_multi_iter() {
for (var i = 0, x = 0; i < 10; i++, x) { --x }
}

// CHECK-LABEL: sil hidden @_TF10statements7test_doFT_T_
func test_do() {
// CHECK: [[BAR:%.*]] = function_ref @_TF10statements3barFSiT_
Expand Down
15 changes: 15 additions & 0 deletions test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,19 @@ func testThrowNil() throws {
}


// <rdar://problem/16650625>
func for_ignored_lvalue_init() {
var i = 0
for i; // expected-error {{expression resolves to an unused l-value}}
i < 10; ++i {}
}

// rdar://problem/18643692
func for_loop_multi_iter() {
for (var i = 0, x = 0; i < 10; i++,
x) { // expected-error {{expression resolves to an unused l-value}}
--x
}
}


0 comments on commit 7eb830f

Please sign in to comment.