Skip to content

Commit 0cac079

Browse files
committed
[TypeChecker] Diagnose empty switch statements in function builder bodies
If there are no 'case' statements in the body let's try to diagnose this situation via limited exhaustiveness check before failing a builder transform, otherwise type-checker might end up without any diagnostics which leads to crashes in SILGen. Resolves: rdar://problem/65983237
1 parent a0426df commit 0cac079

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,18 @@ class BuilderClosureVisitor
699699
if (!cs)
700700
return nullptr;
701701

702+
// If there are no 'case' statements in the body let's try
703+
// to diagnose this situation via limited exhaustiveness check
704+
// before failing a builder transform, otherwise type-checker
705+
// might end up without any diagnostics which leads to crashes
706+
// in SILGen.
707+
if (capturedCaseVars.empty()) {
708+
TypeChecker::checkSwitchExhaustiveness(switchStmt, dc,
709+
/*limitChecking=*/true);
710+
hadError = true;
711+
return nullptr;
712+
}
713+
702714
// Form the expressions that inject the result of each case into the
703715
// appropriate
704716
llvm::TinyPtrVector<Expr *> injectedCaseExprs;

test/Constraints/function_builder_diags.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,9 @@ struct MyView {
584584
// expected-note@-1 {{opaque return type declared here}}
585585
DoesNotConform()
586586
}
587+
588+
@TupleBuilder var emptySwitch: some P {
589+
switch Optional.some(1) { // expected-error {{'switch' statement body must have at least one 'case' or 'default' block; do you want to add a default case?}}
590+
}
591+
}
587592
}

0 commit comments

Comments
 (0)