Skip to content

Commit

Permalink
SILGen: ignore unreachable var decls
Browse files Browse the repository at this point in the history
Fixes a crash in case a lazy var is declared after a return statement

swiftlang#73736
  • Loading branch information
eeckstein authored and ktoso committed Jun 14, 2024
1 parent 94ab261 commit 545ed6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {

// PatternBindingBecls represent local variable bindings that execute
// as part of the function's execution.
if (!isa<PatternBindingDecl>(D)) {
if (!isa<PatternBindingDecl>(D) && !isa<VarDecl>(D)) {
// Other decls define entities that may be used by the program, such as
// local function declarations. So handle them here, before checking for
// reachability, and then continue looping.
Expand Down Expand Up @@ -428,12 +428,9 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
SGF.emitIgnoredExpr(E);
} else {
auto *D = ESD.get<Decl*>();

// Only PatternBindingDecls should be emitted here.
// Other decls were handled above.
auto PBD = cast<PatternBindingDecl>(D);

SGF.visit(PBD);
assert((isa<PatternBindingDecl>(D) || isa<VarDecl>(D)) &&
"other decls should be handled before the reachability check");
SGF.visit(D);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/SILGen/local_decl_after_unreachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ func foo() {
// CHECK-LABEL: sil {{.*}} @{{.*}}3foo{{.*}}3bar{{.*}}F : {{.*}} {
func bar(_: Any) {}

// Check that we don't crash here
lazy var v = 42
}

0 comments on commit 545ed6d

Please sign in to comment.