Skip to content

Commit a6ffb09

Browse files
authored
Merge pull request #59134 from xedin/rdar-94049113
[CSClosure] Perform syntactic checks on rewritten statements
2 parents 01cfba9 + 953095a commit a6ffb09

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,17 @@ class SyntacticElementSolutionApplication
12081208
return nullptr;
12091209
}
12101210

1211+
ASTNode visit(Stmt *S) {
1212+
auto rewritten = ASTVisitor::visit(S);
1213+
if (!rewritten)
1214+
return {};
1215+
1216+
if (auto *stmt = getAsStmt(rewritten))
1217+
performStmtDiagnostics(stmt, context.getAsDeclContext());
1218+
1219+
return rewritten;
1220+
}
1221+
12111222
void visitDecl(Decl *decl) {
12121223
if (isa<IfConfigDecl>(decl))
12131224
return;

test/expr/closure/multi_statement.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,36 @@ func test_type_finder_doesnt_walk_into_inner_closures() {
405405
}
406406
}
407407

408+
// rdar://94049113 - compiler accepts non-optional `guard let` in a closure
409+
func test_non_optional_guard_let_is_diagnosed() {
410+
func fn(_: (Int) -> Void) {}
411+
412+
fn {
413+
if true {
414+
guard let v = $0 else { // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
415+
return
416+
}
417+
418+
print(v)
419+
}
420+
}
421+
422+
fn {
423+
switch $0 {
424+
case (let val):
425+
fn {
426+
guard let x = val else { // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
427+
return
428+
}
429+
430+
print($0 + x)
431+
}
432+
433+
default: break
434+
}
435+
}
436+
}
437+
408438
// rdar://93796211 (issue#59035) - crash during solution application to fallthrough statement
409439
func test_fallthrough_stmt() {
410440
{

0 commit comments

Comments
 (0)