Skip to content

Commit 95197c2

Browse files
committed
[tests] Add more tests for placeholder types
1 parent 42b0427 commit 95197c2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test/Sema/placeholder_type.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,84 @@ let something: _! = getSomething()
122122

123123
extension Array where Element == Int {
124124
static var staticMember: Self { [] }
125+
static func staticFunc() -> Self { [] }
126+
127+
var member: Self { [] }
128+
func method() -> Self { [] }
129+
}
130+
131+
extension Array {
132+
static var otherStaticMember: Self { [] }
125133
}
126134

127135
let _ = [_].staticMember
136+
let _ = [_].staticFunc()
137+
let _ = [_].otherStaticMember.member
138+
let _ = [_].otherStaticMember.method()
139+
140+
func f(x: Any, arr: [Int]) {
141+
// FIXME: Better diagnostics here. Maybe we should suggest replacing placeholders with 'Any'?
142+
143+
if x is _ {} // expected-error {{type of expression is ambiguous without more context}}
144+
if x is [_] {} // expected-error {{type of expression is ambiguous without more context}}
145+
if x is () -> _ {} // expected-error {{type of expression is ambiguous without more context}}
146+
if let y = x as? _ {} // expected-error {{type of expression is ambiguous without more context}}
147+
if let y = x as? [_] {} // expected-error {{type of expression is ambiguous without more context}}
148+
if let y = x as? () -> _ {} // expected-error {{type of expression is ambiguous without more context}}
149+
let y1 = x as! _ // expected-error {{type of expression is ambiguous without more context}}
150+
let y2 = x as! [_] // expected-error {{type of expression is ambiguous without more context}}
151+
let y3 = x as! () -> _ // expected-error {{type of expression is ambiguous without more context}}
152+
153+
switch x {
154+
case is _: break // expected-error {{placeholder type not allowed here}}
155+
case is [_]: break // expected-error {{placeholder type not allowed here}}
156+
case is () -> _: break // expected-error {{placeholder type not allowed here}}
157+
case let y as _: break // expected-error {{placeholder type not allowed here}}
158+
case let y as [_]: break // expected-error {{placeholder type not allowed here}}
159+
case let y as () -> _: break // expected-error {{placeholder type not allowed here}}
160+
}
161+
162+
if arr is _ {} // expected-error {{type of expression is ambiguous without more context}}
163+
if arr is [_] {} // expected-error {{type of expression is ambiguous without more context}}
164+
if arr is () -> _ {} // expected-error {{type of expression is ambiguous without more context}}
165+
if let y = arr as? _ {} // expected-error {{type of expression is ambiguous without more context}}
166+
if let y = arr as? [_] {} // expected-error {{type of expression is ambiguous without more context}}
167+
if let y = arr as? () -> _ {} // expected-error {{type of expression is ambiguous without more context}}
168+
let y1 = arr as! _ // expected-error {{type of expression is ambiguous without more context}}
169+
let y2 = arr as! [_] // expected-error {{type of expression is ambiguous without more context}}
170+
let y3 = arr as! () -> _ // expected-error {{type of expression is ambiguous without more context}}
171+
172+
switch arr {
173+
case is _: break // expected-error {{placeholder type not allowed here}}
174+
case is [_]: break // expected-error {{placeholder type not allowed here}}
175+
case is () -> _: break // expected-error {{placeholder type not allowed here}}
176+
case let y as _: break // expected-error {{placeholder type not allowed here}}
177+
case let y as [_]: break // expected-error {{placeholder type not allowed here}}
178+
case let y as () -> _: break // expected-error {{placeholder type not allowed here}}
179+
}
180+
}
181+
182+
protocol Publisher {
183+
associatedtype Output
184+
associatedtype Failure
185+
}
186+
187+
struct Just<Output>: Publisher {
188+
typealias Failure = Never
189+
}
190+
191+
struct SetFailureType<Output, Failure>: Publisher {}
192+
193+
extension Publisher {
194+
func setFailureType<T>(to: T.Type) -> SetFailureType<Output, T> { // expected-note {{in call to function 'setFailureType(to:)'}}
195+
return .init()
196+
}
197+
}
198+
199+
let _: SetFailureType<Int, String> = Just<Int>().setFailureType(to: _.self)
200+
let _: SetFailureType<Int, [String]> = Just<Int>().setFailureType(to: [_].self)
201+
let _: SetFailureType<Int, (String) -> Double> = Just<Int>().setFailureType(to: ((_) -> _).self)
202+
let _: SetFailureType<Int, (String, Double)> = Just<Int>().setFailureType(to: (_, _).self)
203+
204+
// TODO: Better error message here? Would be nice if we could point to the placeholder...
205+
let _: SetFailureType<Int, String> = Just<Int>().setFailureType(to: _.self).setFailureType(to: String.self) // expected-error {{generic parameter 'T' could not be inferred}}

0 commit comments

Comments
 (0)