SILGen: ignore unreachable var decls

Fixes a crash in case a lazy var is declared after a return statement

https://github.com/apple/swift/issues/73736
This commit is contained in:
Erik Eckstein
2024-06-10 16:19:47 +02:00
parent 25830d6bc3
commit 2200632a95
2 changed files with 6 additions and 7 deletions

View File

@@ -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.
@@ -429,12 +429,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);
}
}
}

View File

@@ -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
}