Skip to content

Commit 99cfcf1

Browse files
authored
Transforms: re-check error handling for nested functions as well (#12950)
Since both in-tree AST transforms (playground and program counter) add new ApplyExprs (and literals that turn into ApplyExprs), we need to recheck nested function bodies as well as top-level ones. rdar://problem/28784059
1 parent ce2966d commit 99cfcf1

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

Diff for: lib/Sema/PCMacro.cpp

+7-15
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,12 @@ class Instrumenter : InstrumenterBase {
300300
EndLoc = FD->getParameterLists().back()->getSourceRange().End;
301301
}
302302

303-
if (EndLoc.isValid()) {
304-
BraceStmt *NNB = prependLoggerCall(NB, {StartLoc, EndLoc});
305-
if (NNB != B) {
306-
FD->setBody(NNB);
307-
}
308-
} else {
309-
if (NB != B) {
310-
FD->setBody(NB);
311-
}
303+
if (EndLoc.isValid())
304+
NB = prependLoggerCall(NB, {StartLoc, EndLoc});
305+
306+
if (NB != B) {
307+
FD->setBody(NB);
308+
TypeChecker(Context).checkFunctionErrorHandling(FD);
312309
}
313310
}
314311
} else if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {
@@ -675,12 +672,7 @@ void swift::performPCMacro(SourceFile &SF, TopLevelContext &TLC) {
675672
if (FD->getBody()) {
676673
ASTContext &ctx = FD->getASTContext();
677674
Instrumenter I(ctx, FD, TmpNameIndex);
678-
Decl *NewDecl = I.transformDecl(FD);
679-
if (AbstractFunctionDecl *NFD =
680-
dyn_cast<AbstractFunctionDecl>(NewDecl)) {
681-
TypeChecker TC(ctx);
682-
TC.checkFunctionErrorHandling(NFD);
683-
}
675+
I.transformDecl(FD);
684676
return false;
685677
}
686678
}

Diff for: lib/Sema/PlaygroundTransform.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ class Instrumenter : InstrumenterBase {
264264
BraceStmt *NB = transformBraceStmt(B);
265265
if (NB != B) {
266266
FD->setBody(NB);
267+
TypeChecker(Context).checkFunctionErrorHandling(FD);
267268
}
268269
}
269270
} else if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {

Diff for: test/PlaygroundTransform/nested_function.swift

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: cp %s %t/main.swift
3+
// RUN: %target-build-swift -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %t/main.swift
4+
// RUN: %target-run %t/main | %FileCheck %s
5+
// RUN: %target-build-swift -Xfrontend -pc-macro -Xfrontend -playground -Xfrontend -debugger-support -o %t/main %S/Inputs/PlaygroundsRuntime.swift %S/Inputs/SilentPCMacroRuntime.swift %t/main.swift
6+
// RUN: %target-run %t/main | %FileCheck %s
7+
// REQUIRES: executable_test
8+
9+
func returnSum() -> Int {
10+
var y = 10
11+
func add() {
12+
y += 5
13+
}
14+
add()
15+
let addAgain = {
16+
y += 5
17+
}
18+
addAgain()
19+
let addMulti = {
20+
y += 5
21+
_ = 0 // force a multi-statement closure
22+
}
23+
addMulti()
24+
return y
25+
}
26+
27+
returnSum()
28+
29+
// CHECK-NOT: $builtin
30+
// CHECK: [9:{{.*}}] $builtin_log_scope_entry
31+
// CHECK-NEXT: [10:{{.*}}] $builtin_log[y='10']
32+
// CHECK-NEXT: [11:{{.*}}] $builtin_log_scope_entry
33+
// CHECK-NEXT: [12:{{.*}}] $builtin_log[y='15']
34+
// CHECK-NEXT: [11:{{.*}}] $builtin_log_scope_exit
35+
// CHECK-NEXT: [15:{{.*}}] $builtin_log[addAgain='{{.*}}']
36+
// CHECK-NEXT: [15:{{.*}}] $builtin_log_scope_entry
37+
38+
// FIXME: We drop the log for the addition here.
39+
// CHECK-NEXT: [16:{{.*}}] $builtin_log[='()']
40+
41+
// CHECK-NEXT: [15:{{.*}}] $builtin_log_scope_exit
42+
43+
// FIXME: There's an extra, unbalanced scope exit here.
44+
// CHECK-NEXT: [9:{{.*}}] $builtin_log_scope_exit
45+
46+
// CHECK-NEXT: [19:{{.*}}] $builtin_log[addMulti='{{.*}}']
47+
// CHECK-NEXT: [19:{{.*}}] $builtin_log_scope_entry
48+
// CHECK-NEXT: [20:{{.*}}] $builtin_log[y='25']
49+
// CHECK-NEXT: [21:{{.*}}] $builtin_log[='0']
50+
// CHECK-NEXT: [19:{{.*}}] $builtin_log_scope_exit
51+
// CHECK-NEXT: [24:{{.*}}] $builtin_log[='25']
52+
// CHECK-NEXT: [9:{{.*}}] $builtin_log_scope_exit
53+
// CHECK-NEXT: [27:{{.*}}] $builtin_log[='25']
54+
// CHECK-NOT: $builtin

0 commit comments

Comments
 (0)