Skip to content

Commit b2619ef

Browse files
committed
Emit Debug Variables for Errors Bound in Catch Clauses
Make error variables accessible to a fresh debug scope for the catch clause. rdar://85982381
1 parent aebd771 commit b2619ef

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,6 +3007,10 @@ void SILGenFunction::emitCatchDispatch(DoCatchStmt *S, ManagedValue exn,
30073007
// If we don't have a multi-pattern 'catch', we can emit the
30083008
// body inline. Emit the statement here and bail early.
30093009
if (clause->getCaseLabelItems().size() == 1) {
3010+
// Debug values for catch clause variables must be nested within a scope for
3011+
// the catch block to avoid name conflicts.
3012+
DebugScope scope(*this, CleanupLocation(clause));
3013+
30103014
// If we have case body vars, set them up to point at the matching var
30113015
// decls.
30123016
if (clause->hasCaseBodyVariables()) {
@@ -3027,6 +3031,11 @@ void SILGenFunction::emitCatchDispatch(DoCatchStmt *S, ManagedValue exn,
30273031
// Ok, we found a match. Update the VarLocs for the case block.
30283032
auto v = VarLocs[vd];
30293033
VarLocs[expected] = v;
3034+
3035+
// Emit a debug description of the incoming arg, nested within the scope
3036+
// for the pattern match.
3037+
SILDebugVariable dbgVar(vd->isLet(), /*ArgNo=*/0);
3038+
B.emitDebugDescription(vd, v.value, dbgVar);
30303039
}
30313040
}
30323041
}

test/DebugInfo/catch_let.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,45 @@ func throwing() throws -> () {
99

1010
func use<T>(_ t: T) {}
1111

12-
public func foo() {
12+
// CHECK-LABEL: define {{.*}}explicitBinding{{.*}}
13+
public func explicitBinding() {
1314
do {
1415
try throwing()
1516
}
1617
catch let error {
17-
// CHECK: call void @llvm.dbg.declare(metadata %swift.error** %{{.*}}, metadata ![[ERROR:[0-9]+]],
18-
// CHECK: ![[ERROR]] = !DILocalVariable(name: "error"
18+
// CHECK: call void @llvm.dbg.declare(metadata %swift.error** %{{.*}}, metadata ![[EXPLICIT_ERROR:[0-9]+]],
1919
use(error)
2020
}
2121
}
22-
foo();
22+
explicitBinding()
23+
24+
// CHECK-LABEL: define {{.*}}implicitBinding{{.*}}
25+
public func implicitBinding() {
26+
do {
27+
try throwing()
28+
}
29+
catch {
30+
// CHECK: call void @llvm.dbg.declare(metadata %swift.error** %{{.*}}, metadata ![[IMPLICIT_ERROR:[0-9]+]],
31+
use(error)
32+
}
33+
}
34+
implicitBinding()
35+
36+
// CHECK-LABEL: define {{.*}}multiBinding{{.*}}
37+
public func multiBinding() {
38+
do {
39+
try throwing()
40+
}
41+
catch let error as MyError, let error as MyError {
42+
// CHECK: call void @llvm.dbg.declare(metadata %swift.error** %{{.*}}, metadata ![[MULTI_BINDING_ERROR:[0-9]+]],
43+
// CHECK-NOT: call void @llvm.dbg.declare(metadata %swift.error** %{{.*}}
44+
use(error)
45+
} catch {
46+
use(error)
47+
}
48+
}
49+
multiBinding()
50+
51+
// CHECK: ![[EXPLICIT_ERROR]] = !DILocalVariable(name: "error"
52+
// CHECK: ![[IMPLICIT_ERROR]] = !DILocalVariable(name: "error"
53+
// CHECK: ![[MULTI_BINDING_ERROR]] = !DILocalVariable(name: "error"

test/SILGen/errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func dont_return<T>(_ argument: T) throws -> T {
108108
// Catch HomeworkError.CatAteIt.
109109
// CHECK: [[MATCH]]([[T0:%.*]] : @owned $Cat):
110110
// CHECK-NEXT: [[BORROWED_T0:%.*]] = begin_borrow [lexical] [[T0]]
111+
// CHECK-NEXT: debug_value [[BORROWED_T0]] : $Cat
111112
// CHECK-NEXT: [[T0_COPY:%.*]] = copy_value [[BORROWED_T0]]
112113
// CHECK-NEXT: end_borrow [[BORROWED_T0]]
113114
// CHECK-NEXT: destroy_value [[T0]]

0 commit comments

Comments
 (0)