Skip to content

[CSClosure] Fix crash in fallthrough statement checking #59059

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 25, 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
19 changes: 16 additions & 3 deletions lib/Sema/CSClosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,15 @@ class SyntacticElementSolutionApplication
}

auto caseStmt = cast<CaseStmt>(rawCase.get<Stmt *>());
visitCaseStmt(caseStmt);
// Body of the `case` statement can contain a `fallthrough`
// statement that requires both source and destination
// `case` preambles to be type-checked, so bodies of `case`
// statements should be visited after preambles.
visitCaseStmtPreamble(caseStmt);
}

for (auto *caseStmt : switchStmt->getCases()) {
visitCaseStmtBody(caseStmt);

// Check restrictions on '@unknown'.
if (caseStmt->hasUnknownAttr()) {
Expand All @@ -1436,7 +1444,7 @@ class SyntacticElementSolutionApplication
return doStmt;
}

ASTNode visitCaseStmt(CaseStmt *caseStmt) {
void visitCaseStmtPreamble(CaseStmt *caseStmt) {
// Translate the patterns and guard expressions for each case label item.
for (auto &caseItem : caseStmt->getMutableCaseLabelItems()) {
SolutionApplicationTarget caseTarget(&caseItem,
Expand All @@ -1453,11 +1461,16 @@ class SyntacticElementSolutionApplication
solution.getType(prev)->mapTypeOutOfContext());
expected->setInterfaceType(type);
}
}

// Translate the body.
void visitCaseStmtBody(CaseStmt *caseStmt) {
auto *newBody = visit(caseStmt->getBody()).get<Stmt *>();
caseStmt->setBody(cast<BraceStmt>(newBody));
}

ASTNode visitCaseStmt(CaseStmt *caseStmt) {
visitCaseStmtPreamble(caseStmt);
visitCaseStmtBody(caseStmt);
return caseStmt;
}

Expand Down
20 changes: 20 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,23 @@ func test_type_finder_doesnt_walk_into_inner_closures() {
return x
}
}

// rdar://93796211 (issue#59035) - crash during solution application to fallthrough statement
func test_fallthrough_stmt() {
{
var collector: [Void] = []
for _: Void in [] {
switch (() as Void?, ()) {
case (let a?, let b):
// expected-warning@-1 {{constant 'b' inferred to have type '()', which may be unexpected}}
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
collector.append(a)
fallthrough
case (nil, let b):
// expected-warning@-1 {{constant 'b' inferred to have type '()', which may be unexpected}}
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
collector.append(b)
}
}
}()
}