Skip to content

Commit

Permalink
Diagnose unreachable optional evaluation exprs
Browse files Browse the repository at this point in the history
OptionalEvaluationExprs are always implicit and were
being let through SILGen's unreachable diagnostics
branch.  Decompose the structure to check to see if its
contents are not implicit expressions.  If that is the
case, then diagnose them.

Resolves SR-5763.
  • Loading branch information
CodaFi committed Sep 11, 2017
1 parent 12841a9 commit bbdc546
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,22 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
if (auto *S = ESD.dyn_cast<Stmt*>()) {
if (S->isImplicit()) continue;
} else if (auto *E = ESD.dyn_cast<Expr*>()) {
if (E->isImplicit()) continue;
// Optional chaining expressions are wrapped in a structure like.
//
// (optional_evaluation_expr implicit type='T?'
// (call_expr type='T?'
// (exprs...
//
// Walk through it to find out if the statement is actually implicit.
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(E)) {
if (auto *IIO = dyn_cast<InjectIntoOptionalExpr>(OEE->getSubExpr()))
if (IIO->getSubExpr()->isImplicit()) continue;
if (auto *C = dyn_cast<CallExpr>(OEE->getSubExpr()))
if (C->isImplicit()) continue;
} else if (E->isImplicit()) {
// Ignore all other implicit expressions.
continue;
}
}

if (StmtType != UnknownStmtType) {
Expand Down
9 changes: 9 additions & 0 deletions test/SILGen/unreachable_code.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func testUnreachableCase5(a : Tree) {
}
}

func testOptionalEvaluationBreak(a : Tree) {
class SR5763 { func foo() {} }
func createOptional() -> SR5763? { return SR5763() }
switch a {
case _:
break
createOptional()?.foo() // expected-warning {{code after 'break' will never be executed}}
}
}

func testUnreachableAfterThrow(e: Error) throws {
throw e
Expand Down

0 comments on commit bbdc546

Please sign in to comment.