Skip to content

Commit 881de88

Browse files
Add codegen test cases to make sure that patching patterns works.
There is a small chance that codegen and everything works fine, but the generated code is wrong because of mismatched expectations on two sides, so we have some tests to catch that.
1 parent 25e5b15 commit 881de88

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// RUN: %target-run-simple-swift | %FileCheck %s
2+
3+
// Even though we test that type-checking and exhaustiveness checking work fine
4+
// in the presence of implicit tupling/untupling in exhaustive_switch.swift,
5+
// make sure that the "patched" patterns do not lead to incorrect codegen.
6+
7+
enum Untupled {
8+
case upair(Int, Int)
9+
}
10+
11+
let u_ex = Untupled.upair(1, 2)
12+
13+
func sr11212_content_untupled_pattern_tupled1(u: Untupled) -> (Int, Int) {
14+
switch u { case .upair((let x, let y)): return (x, y) }
15+
}
16+
print(sr11212_content_untupled_pattern_tupled1(u: u_ex))
17+
// CHECK: (1, 2)
18+
19+
func sr11212_content_untupled_pattern_tupled2(u: Untupled) -> (Int, Int) {
20+
switch u { case .upair(let (x, y)): return (x, y) }
21+
}
22+
print(sr11212_content_untupled_pattern_tupled2(u: u_ex))
23+
// CHECK: (1, 2)
24+
25+
func sr11212_content_untupled_pattern_tupled3(u: Untupled) -> (Int, Int) {
26+
switch u { case let .upair((x, y)): return (x, y) }
27+
}
28+
print(sr11212_content_untupled_pattern_tupled3(u: u_ex))
29+
// CHECK: (1, 2)
30+
31+
func sr11212_content_untupled_pattern_untupled1(u: Untupled) -> (Int, Int) {
32+
switch u { case .upair(let x, let y): return (x, y) }
33+
}
34+
print(sr11212_content_untupled_pattern_untupled1(u: u_ex))
35+
// CHECK: (1, 2)
36+
37+
func sr11212_content_untupled_pattern_untupled2(u: Untupled) -> (Int, Int) {
38+
switch u { case let .upair(x, y): return (x, y) }
39+
}
40+
print(sr11212_content_untupled_pattern_untupled2(u: u_ex))
41+
// CHECK: (1, 2)
42+
43+
func sr11212_content_untupled_pattern_ambiguous1(u: Untupled) -> (Int, Int) {
44+
switch u { case .upair(let u_): return u_ }
45+
}
46+
print(sr11212_content_untupled_pattern_ambiguous1(u: u_ex))
47+
// CHECK: (1, 2)
48+
49+
func sr11212_content_untupled_pattern_ambiguous2(u: Untupled) -> (Int, Int) {
50+
switch u { case let .upair(u_): return u_ }
51+
}
52+
print(sr11212_content_untupled_pattern_ambiguous2(u: u_ex))
53+
// CHECK: (1, 2)
54+
55+
enum Tupled {
56+
case tpair((Int, Int))
57+
}
58+
59+
let t_ex = Tupled.tpair((1, 2))
60+
61+
func sr11212_content_tupled_pattern_tupled1(t: Tupled) -> (Int, Int) {
62+
switch t { case .tpair((let x, let y)): return (x, y) }
63+
}
64+
print(sr11212_content_tupled_pattern_tupled1(t: t_ex))
65+
// CHECK: (1, 2)
66+
67+
func sr11212_content_tupled_pattern_tupled2(t: Tupled) -> (Int, Int) {
68+
switch t { case .tpair(let (x, y)): return (x, y) }
69+
}
70+
print(sr11212_content_tupled_pattern_tupled2(t: t_ex))
71+
// CHECK: (1, 2)
72+
73+
func sr11212_content_tupled_pattern_tupled3(t: Tupled) -> (Int, Int) {
74+
switch t { case let .tpair((x, y)): return (x, y) }
75+
}
76+
print(sr11212_content_tupled_pattern_tupled3(t: t_ex))
77+
// CHECK: (1, 2)
78+
79+
func sr11212_content_tupled_pattern_untupled1(t: Tupled) -> (Int, Int) {
80+
switch t { case .tpair(let x, let y): return (x, y) }
81+
}
82+
print(sr11212_content_tupled_pattern_untupled1(t: t_ex))
83+
// CHECK: (1, 2)
84+
85+
func sr11212_content_tupled_pattern_untupled2(t: Tupled) -> (Int, Int) {
86+
switch t { case let .tpair(x, y): return (x, y) }
87+
}
88+
print(sr11212_content_tupled_pattern_untupled2(t: t_ex))
89+
// CHECK: (1, 2)
90+
91+
func sr11212_content_tupled_pattern_ambiguous1(t: Tupled) -> (Int, Int) {
92+
switch t { case .tpair(let t_): return t_ }
93+
}
94+
print(sr11212_content_tupled_pattern_ambiguous1(t: t_ex))
95+
// CHECK: (1, 2)
96+
97+
func sr11212_content_tupled_pattern_ambiguous2(t: Tupled) -> (Int, Int) {
98+
switch t { case let .tpair(t_): return t_ }
99+
}
100+
print(sr11212_content_tupled_pattern_ambiguous2(t: t_ex))
101+
// CHECK: (1, 2)
102+
103+
enum Box<T> {
104+
case box(T)
105+
}
106+
107+
let b_ex : Box<(Int, Int)> = Box.box((1, 2))
108+
109+
func sr11212_content_generic_pattern_tupled1(b: Box<(Int, Int)>) -> (Int, Int) {
110+
switch b { case .box((let x, let y)): return (x, y) }
111+
}
112+
print(sr11212_content_generic_pattern_tupled1(b: b_ex))
113+
// CHECK: (1, 2)
114+
115+
func sr11212_content_generic_pattern_tupled2(b: Box<(Int, Int)>) -> (Int, Int) {
116+
switch b { case .box(let (x, y)): return (x, y) }
117+
}
118+
print(sr11212_content_generic_pattern_tupled2(b: b_ex))
119+
// CHECK: (1, 2)
120+
121+
func sr11212_content_generic_pattern_tupled3(b: Box<(Int, Int)>) -> (Int, Int) {
122+
switch b { case let .box((x, y)): return (x, y) }
123+
}
124+
print(sr11212_content_generic_pattern_tupled3(b: b_ex))
125+
// CHECK: (1, 2)
126+
127+
func sr11212_content_generic_pattern_untupled1(b: Box<(Int, Int)>) -> (Int, Int) {
128+
switch b { case .box(let x, let y): return (x, y) }
129+
}
130+
print(sr11212_content_generic_pattern_untupled1(b: b_ex))
131+
// CHECK: (1, 2)
132+
133+
func sr11212_content_generic_pattern_untupled2(b: Box<(Int, Int)>) -> (Int, Int) {
134+
switch b { case let .box(x, y): return (x, y) }
135+
}
136+
print(sr11212_content_generic_pattern_untupled2(b: b_ex))
137+
// CHECK: (1, 2)
138+
139+
func sr11212_content_generic_pattern_ambiguous1(b: Box<(Int, Int)>) -> (Int, Int) {
140+
switch b { case .box(let b_): return b_ }
141+
}
142+
print(sr11212_content_generic_pattern_ambiguous1(b: b_ex))
143+
// CHECK: (1, 2)
144+
145+
func sr11212_content_generic_pattern_ambiguous2(b: Box<(Int, Int)>) -> (Int, Int) {
146+
switch b { case let .box(b_): return b_ }
147+
}
148+
print(sr11212_content_generic_pattern_ambiguous2(b: b_ex))
149+
// CHECK: (1, 2)

test/Sema/exhaustive_switch.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ func sr11160_extra() {
12161216

12171217
// SR-11212 tests: Some of the tests here rely on compiler bugs related to
12181218
// implicit (un)tupling in patterns.
1219+
//
1220+
// Related codegen test: Compatibility/implicit_tupling_untupling_codegen.swift
12191221
enum SR11212Tests {
12201222

12211223
enum Untupled {

0 commit comments

Comments
 (0)