Skip to content

Commit 2200632

Browse files
committed
SILGen: ignore unreachable var decls
Fixes a crash in case a lazy var is declared after a return statement #73736
1 parent 25830d6 commit 2200632

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

lib/SILGen/SILGenStmt.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
323323

324324
// PatternBindingBecls represent local variable bindings that execute
325325
// as part of the function's execution.
326-
if (!isa<PatternBindingDecl>(D)) {
326+
if (!isa<PatternBindingDecl>(D) && !isa<VarDecl>(D)) {
327327
// Other decls define entities that may be used by the program, such as
328328
// local function declarations. So handle them here, before checking for
329329
// reachability, and then continue looping.
@@ -429,12 +429,9 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
429429
SGF.emitIgnoredExpr(E);
430430
} else {
431431
auto *D = ESD.get<Decl*>();
432-
433-
// Only PatternBindingDecls should be emitted here.
434-
// Other decls were handled above.
435-
auto PBD = cast<PatternBindingDecl>(D);
436-
437-
SGF.visit(PBD);
432+
assert((isa<PatternBindingDecl>(D) || isa<VarDecl>(D)) &&
433+
"other decls should be handled before the reachability check");
434+
SGF.visit(D);
438435
}
439436
}
440437
}

test/SILGen/local_decl_after_unreachable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ func foo() {
1313
// CHECK-LABEL: sil {{.*}} @{{.*}}3foo{{.*}}3bar{{.*}}F : {{.*}} {
1414
func bar(_: Any) {}
1515

16+
// Check that we don't crash here
17+
lazy var v = 42
1618
}

0 commit comments

Comments
 (0)