Skip to content

Commit e342267

Browse files
authored
Merge pull request #77477 from DougGregor/result-builder-empty-case-crash
[Result builder transform] Don't abort when we encounter a case with no statements
2 parents 72b8d22 + f892189 commit e342267

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -630,19 +630,6 @@ class ResultBuilderTransform
630630
transformCase(CaseStmt *caseStmt) {
631631
auto *body = caseStmt->getBody();
632632

633-
// Explicitly disallow `case` statements with empty bodies
634-
// since that helps to diagnose other issues with switch
635-
// statements by excluding invalid cases.
636-
if (auto *BS = dyn_cast<BraceStmt>(body)) {
637-
if (BS->getNumElements() == 0) {
638-
// HACK: still allow empty bodies if typechecking for code
639-
// completion. Code completion ignores diagnostics
640-
// and won't get any types if we fail.
641-
if (!ctx.SourceMgr.hasIDEInspectionTargetBuffer())
642-
return std::nullopt;
643-
}
644-
}
645-
646633
NullablePtr<Expr> caseVarRef;
647634
std::optional<UnsupportedElt> unsupported;
648635
SmallVector<ASTNode, 4> newBody;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %target-swift-emit-silgen %s -verify | %FileCheck %s
2+
3+
// Tests for a crash that occurred when the result builder transform encountered
4+
// an empty case statement.
5+
protocol V { }
6+
7+
struct EV: V { }
8+
9+
@resultBuilder
10+
struct VB {
11+
static func buildBlock(_ components: any V...) -> any V { EV() }
12+
static func buildEither(first: any V) -> any V { first }
13+
static func buildEither(second: any V) -> any V { second }
14+
}
15+
16+
extension String: V { }
17+
18+
enum E {
19+
case a(Int)
20+
case b(String)
21+
}
22+
23+
struct S {
24+
var flag: E
25+
26+
// CHECK-LABEL: sil hidden [ossa] @$s25result_builder_empty_case1SV4testAA1V_pyF
27+
// CHECK: switch_enum
28+
@VB
29+
func test() -> any V {
30+
switch flag {
31+
case .a:
32+
// When NOT_DEFINED is... not defined... this ends up being an empty case.
33+
// We permit this
34+
#if NOT_DEFINED
35+
EV()
36+
#endif
37+
case .b:
38+
EV()
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)