Skip to content

Commit 348f966

Browse files
committed
Add tests for -enable-testing silencing exhaustivity diagnostics
1 parent 3866535 commit 348f966

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// REQUIRES: plus_zero_runtime
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %target-swift-frontend -emit-module -enable-resilience -emit-module-path=%t/resilient_struct.swiftmodule -module-name=resilient_struct %S/../Inputs/resilient_struct.swift
5+
// RUN: %target-swift-frontend -emit-module -enable-resilience -emit-module-path=%t/resilient_enum.swiftmodule -module-name=resilient_enum -I %t %S/../Inputs/resilient_enum.swift -enable-testing
6+
// RUN: %target-swift-frontend -module-name enum_resilience_testable -I %t -enable-sil-ownership -emit-silgen -enable-nonfrozen-enum-exhaustivity-diagnostics -swift-version 5 %s | %FileCheck %s
7+
8+
// This is mostly testing the same things as enum_resilienc.swift, just in a
9+
// context where the user will never be forced to write a default case. It's the
10+
// same effect as -swift-version 4 and ignoring the exhaustivity warning,
11+
// though.
12+
13+
@testable import resilient_enum
14+
15+
// Resilient enums are always address-only, and switches must include
16+
// a default case
17+
18+
// CHECK-LABEL: sil hidden @$S24enum_resilience_testable15resilientSwitchyy0d1_A06MediumOF : $@convention(thin) (@in_guaranteed Medium) -> ()
19+
// CHECK: [[BOX:%.*]] = alloc_stack $Medium
20+
// CHECK-NEXT: copy_addr %0 to [initialization] [[BOX]]
21+
// CHECK-NEXT: switch_enum_addr [[BOX]] : $*Medium, case #Medium.Paper!enumelt: bb1, case #Medium.Canvas!enumelt: bb2, case #Medium.Pamphlet!enumelt.1: bb3, case #Medium.Postcard!enumelt.1: bb4, default bb5
22+
// CHECK: bb1:
23+
// CHECK-NEXT: dealloc_stack [[BOX]]
24+
// CHECK-NEXT: br bb6
25+
// CHECK: bb2:
26+
// CHECK-NEXT: dealloc_stack [[BOX]]
27+
// CHECK-NEXT: br bb6
28+
// CHECK: bb3:
29+
// CHECK-NEXT: [[INDIRECT_ADDR:%.*]] = unchecked_take_enum_data_addr [[BOX]]
30+
// CHECK-NEXT: [[INDIRECT:%.*]] = load [take] [[INDIRECT_ADDR]]
31+
// CHECK-NEXT: [[PAYLOAD:%.*]] = project_box [[INDIRECT]]
32+
// CHECK-NEXT: destroy_value [[INDIRECT]]
33+
// CHECK-NEXT: dealloc_stack [[BOX]]
34+
// CHECK-NEXT: br bb6
35+
// CHECK: bb4:
36+
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = unchecked_take_enum_data_addr [[BOX]]
37+
// CHECK-NEXT: destroy_addr [[PAYLOAD_ADDR]]
38+
// CHECK-NEXT: dealloc_stack [[BOX]]
39+
// CHECK-NEXT: br bb6
40+
// CHECK: bb5:
41+
// CHECK-NEXT: builtin "int_trap"()
42+
// CHECK-NEXT: unreachable
43+
// CHECK: bb6:
44+
// CHECK-NOT: destroy_addr %0
45+
// CHECK-NEXT: [[RESULT:%.*]] = tuple ()
46+
// CHECK-NEXT: return [[RESULT]]
47+
48+
func resilientSwitch(_ m: Medium) {
49+
switch m {
50+
case .Paper: ()
51+
case .Canvas: ()
52+
case .Pamphlet: ()
53+
case .Postcard: ()
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@_frozen public enum FrozenEnum {
2+
case a, b, c
3+
}
4+
5+
/*non-frozen*/public enum NonFrozenEnum {
6+
case a, b, c
7+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -swift-version 5 -enable-resilience -enable-testing %S/Inputs/exhaustive_switch_testable_helper.swift -emit-module -o %t
3+
// RUN: %target-swift-frontend -typecheck %s -swift-version 5 -enable-nonfrozen-enum-exhaustivity-diagnostics -I %t -DTESTABLE -verify
4+
// RUN: not %target-swift-frontend -typecheck %s -swift-version 5 -enable-nonfrozen-enum-exhaustivity-diagnostics -I %t 2>&1 | %FileCheck -check-prefix=VERIFY-NON-FROZEN %s
5+
6+
#if TESTABLE
7+
@testable import exhaustive_switch_testable_helper
8+
#else
9+
import exhaustive_switch_testable_helper
10+
#endif
11+
12+
func testFrozen(_ e: FrozenEnum) -> Int {
13+
switch e {
14+
case .a: return 1
15+
case .b, .c: return 2
16+
}
17+
}
18+
19+
func testNonFrozen(_ e: NonFrozenEnum) -> Int {
20+
// VERIFY-NON-FROZEN: exhaustive_switch_testable.swift:[[@LINE+1]]:{{[0-9]+}}: error: switch must be exhaustive
21+
switch e {
22+
case .a: return 1
23+
case .b, .c: return 2
24+
}
25+
}

0 commit comments

Comments
 (0)