Skip to content

Commit 42b1a39

Browse files
committed
[TypeChecker] NFC: Add some test-cases for extended multi-statement closure inference
1 parent 2384eb2 commit 42b1a39

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 -experimental-multi-statement-closures -enable-experimental-static-assert
2+
3+
func isInt<T>(_ value: T) -> Bool {
4+
return value is Int
5+
}
6+
7+
func maybeGetValue<T>(_ value: T) -> T? {
8+
return value
9+
}
10+
11+
enum MyError: Error {
12+
case featureIsTooCool
13+
14+
func doIt() { }
15+
}
16+
17+
enum State {
18+
case suspended
19+
case partial(Int, Int)
20+
case finished
21+
}
22+
23+
func random(_: Int) -> Bool { return false }
24+
25+
func mightThrow() throws -> Bool { throw MyError.featureIsTooCool }
26+
27+
func mapWithMoreStatements(ints: [Int], state: State) throws {
28+
let _ = try ints.map { i in
29+
guard var actualValue = maybeGetValue(i) else {
30+
return String(0)
31+
}
32+
33+
let value = actualValue + 1
34+
do {
35+
if isInt(i) {
36+
print(value)
37+
} else if value == 17 {
38+
print("seventeen!")
39+
}
40+
}
41+
42+
while actualValue < 100 {
43+
actualValue += 1
44+
}
45+
46+
my_repeat:
47+
repeat {
48+
print("still here")
49+
50+
if i % 7 == 0 {
51+
break my_repeat
52+
}
53+
} while random(i)
54+
55+
defer {
56+
print("I am so done here")
57+
}
58+
59+
for j in 0..<i where j % 2 == 0 {
60+
if j % 7 == 0 {
61+
continue
62+
}
63+
64+
switch (state, j) {
65+
case (.suspended, 0):
66+
print("something")
67+
fallthrough
68+
case (.finished, 0):
69+
print("something else")
70+
71+
case (.partial(let current, let end), let j):
72+
print("\(current) of \(end): \(j)")
73+
74+
default:
75+
print("so, here we are")
76+
}
77+
print("even")
78+
throw MyError.featureIsTooCool
79+
}
80+
81+
#assert(true)
82+
83+
// expected-warning@+1{{danger zone}}
84+
#warning("danger zone")
85+
86+
#if false
87+
struct NothingHere { }
88+
#else
89+
struct NestedStruct {
90+
var x: Int
91+
}
92+
#endif
93+
94+
do {
95+
print(try mightThrow())
96+
} catch let e as MyError {
97+
e.doIt()
98+
} catch {
99+
print(error)
100+
}
101+
return String(value)
102+
}
103+
}
104+
105+
func acceptsWhateverClosure<T, R>(_ value: T, _ fn: (T) -> R) { }
106+
107+
func testReturnWithoutExpr(i: Int) {
108+
acceptsWhateverClosure(i) { i in
109+
print(i)
110+
return
111+
}
112+
}
113+
114+
// `withContiguousStorageIfAvailable` is overloaded, so let's make sure that
115+
// filtering works correctly.
116+
func test_overloaded_call(arr: [Int], body: (UnsafeBufferPointer<Int>) -> Void) -> Void {
117+
arr.withContiguousStorageIfAvailable { buffer in
118+
let _ = type(of: buffer)
119+
body(buffer) // ok
120+
}
121+
}
122+
123+
// Used to wrap closure in `FunctionConversionExpr` in this case,
124+
// but now solver would just inject return expression into optional where necessary.
125+
func test_result_optional_injection() {
126+
func fn<T>(_: () -> T?) -> [T] {
127+
[]
128+
}
129+
130+
_ = fn {
131+
if true {
132+
return // Ok
133+
}
134+
}
135+
}
136+
137+
let _ = {
138+
for i: Int8 in 0 ..< 20 { // Ok (pattern can inform a type of the sequence)
139+
print(i)
140+
}
141+
}

0 commit comments

Comments
 (0)