Skip to content

Fix incorrect scopes of variables declared in guard let statements (again) #41069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,8 +1375,13 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,

switch (elt.getKind()) {
case StmtConditionElement::CK_PatternBinding: {
InitializationPtr initialization =
emitPatternBindingInitialization(elt.getPattern(), FalseDest);
// Begin a new binding scope, which is popped when the next innermost debug
// scope ends. The cleanup location loc isn't the perfect source location
// but it's close enough.
B.getSILGenFunction().enterDebugScope(loc,
/*isBindingScope=*/true);
InitializationPtr initialization =
emitPatternBindingInitialization(elt.getPattern(), FalseDest);

// Emit the initial value into the initialization.
FullExpr Scope(Cleanups, CleanupLocation(elt.getInitializer()));
Expand Down
5 changes: 3 additions & 2 deletions test/DebugInfo/guard-let-scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ func f(c: AnyObject?) {
let x = c
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}} parent @{{.*}}1f
// CHECK: sil_scope [[S2:[0-9]+]] { {{.*}} parent [[S1]] }
// CHECK: sil_scope [[S3:[0-9]+]] { loc "{{.*}}":[[@LINE+3]]:17 parent [[S2]] }
// CHECK: sil_scope [[S3:[0-9]+]] { {{.*}} parent [[S2]] }
// CHECK: sil_scope [[S4:[0-9]+]] { loc "{{.*}}":[[@LINE+3]]:17 parent [[S3]] }
// CHECK: debug_value %{{.*}} : $Optional<AnyObject>, let, name "x"{{.*}} scope [[S2]]
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S3]]
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S4]]
guard let x = x else {
fatalError(".")
}
Expand Down
27 changes: 27 additions & 0 deletions test/DebugInfo/guard-let-scope2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// REQUIRES: objc_interop
// RUN: %target-swift-frontend -emit-sil -Xllvm -sil-print-debuginfo %s \
// RUN: | %FileCheck %s
import Foundation

func takeClosure2 (_ closure: @escaping () -> Bool) { assert(closure()) }

struct SomeObject {
var s = ""
var today = Date()
}

public func f(x: String?) throws {
var s : SomeObject? = nil
takeClosure2 {
s = SomeObject()
return s != nil
}
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}} parent @{{.*}}1f
// CHECK: sil_scope [[S2:[0-9]+]] { {{.*}} parent [[S1]] }
// CHECK: sil_scope [[S3:[0-9]+]] { {{.*}} parent [[S2]] }
// CHECK: alloc_stack $SomeObject, let, name "s", {{.*}} scope [[S3]]
guard let s = s else {
assert(false)
return
}
}
14 changes: 7 additions & 7 deletions test/SILOptimizer/definite-init-wrongscope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public class M {

// CHECK-LABEL: sil [ossa] @$s3del1MC4fromAcA12WithDelegate_p_tKcfc : $@convention(method) (@in WithDelegate, @owned M) -> (@owned M, @error Error)

// CHECK: [[I:%.*]] = integer_literal $Builtin.Int2, 1, loc {{.*}}:23:12, scope 5
// CHECK: [[V:%.*]] = load [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 5
// CHECK: [[OR:%.*]] = builtin "or_Int2"([[V]] : $Builtin.Int2, [[I]] : $Builtin.Int2) : $Builtin.Int2, loc {{.*}}:23:12, scope 5
// CHECK: store [[OR]] to [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 5
// CHECK: store %{{.*}} to [init] %{{.*}} : $*C, loc {{.*}}:26:20, scope 5
// CHECK: [[I:%.*]] = integer_literal $Builtin.Int2, 1, loc {{.*}}:23:12, scope 6
// CHECK: [[V:%.*]] = load [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 6
// CHECK: [[OR:%.*]] = builtin "or_Int2"([[V]] : $Builtin.Int2, [[I]] : $Builtin.Int2) : $Builtin.Int2, loc {{.*}}:23:12, scope 6
// CHECK: store [[OR]] to [trivial] %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 6
// CHECK: store %{{.*}} to [init] %{{.*}} : $*C, loc {{.*}}:26:20, scope 6

// Make sure the dealloc_stack gets the same scope of the instructions surrounding it.

// CHECK: destroy_addr %0 : $*WithDelegate, loc {{.*}}:29:5, scope 5
// CHECK: dealloc_stack %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 5
// CHECK: destroy_addr %0 : $*WithDelegate, loc {{.*}}:29:5, scope 6
// CHECK: dealloc_stack %2 : $*Builtin.Int2, loc {{.*}}:23:12, scope 6
// CHECK: throw %{{.*}} : $Error, loc {{.*}}:23:12, scope 1