Skip to content

[CSClosure] Perform syntactic checks on rewritten statements #59134

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
May 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
11 changes: 11 additions & 0 deletions lib/Sema/CSClosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,17 @@ class SyntacticElementSolutionApplication
return nullptr;
}

ASTNode visit(Stmt *S) {
auto rewritten = ASTVisitor::visit(S);
if (!rewritten)
return {};

if (auto *stmt = getAsStmt(rewritten))
performStmtDiagnostics(stmt, context.getAsDeclContext());

return rewritten;
}

void visitDecl(Decl *decl) {
if (isa<IfConfigDecl>(decl))
return;
Expand Down
30 changes: 30 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ func test_type_finder_doesnt_walk_into_inner_closures() {
}
}

// rdar://94049113 - compiler accepts non-optional `guard let` in a closure
func test_non_optional_guard_let_is_diagnosed() {
func fn(_: (Int) -> Void) {}

fn {
if true {
guard let v = $0 else { // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
return
}

print(v)
}
}

fn {
switch $0 {
case (let val):
fn {
guard let x = val else { // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
return
}

print($0 + x)
}

default: break
}
}
}

// rdar://93796211 (issue#59035) - crash during solution application to fallthrough statement
func test_fallthrough_stmt() {
{
Expand Down