Skip to content

Commit 8529126

Browse files
committed
[Tests] Add initial tests for placeholder types
1 parent bbf8e6c commit 8529126

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3676,7 +3676,7 @@ NOTE(descriptive_generic_type_declared_here,none,
36763676
"%0 declared here", (StringRef))
36773677

36783678
ERROR(placeholder_type_not_allowed,none,
3679-
"you cannot use a placeholder type here", ())
3679+
"type placeholder not allowed here", ())
36803680

36813681
WARNING(use_of_void_pointer,none,
36823682
"Unsafe%0Pointer<Void> has been replaced by Unsafe%0RawPointer", (StringRef))

test/Sema/placeholder_type.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
let x: _ = 0
4+
let dict1: [_: Int] = ["hi": 0]
5+
let dict2: [Character: _] = ["h": 0]
6+
7+
let arr = [_](repeating: "hi", count: 3)
8+
9+
func foo(_ arr: [_] = [0]) {} // expected-error {{placeholder type not allowed here}}
10+
11+
let foo = _.foo // expected-error {{could not infer type for placeholder}}
12+
let zero: _ = .zero // expected-error {{cannot infer contextual base in reference to member 'zero'}}
13+
14+
struct S<T> {
15+
var x: T
16+
}
17+
18+
var s1: S<_> = .init(x: 0)
19+
var s2 = S<_>(x: 0)
20+
21+
let losslessStringConverter = Double.init as (String) -> _?
22+
23+
let optInt: _? = 0
24+
let implicitOptInt: _! = 0
25+
26+
let func1: (_) -> Double = { (x: Int) in 0.0 }
27+
let func2: (Int) -> _ = { x in 0.0 }
28+
let func3: (_) -> _ = { (x: Int) in 0.0 }
29+
let func4: (_, String) -> _ = { (x: Int, y: String) in 0.0 }
30+
let func5: (_, String) -> _ = { (x: Int, y: Double) in 0.0 } // expected-error {{cannot convert value of type '(Int, Double) -> _' to specified type '(_, String) -> _'}}
31+
32+
let type: _.Type = Int.self
33+
let type2: Int.Type.Type = _.Type.self
34+
35+
struct MyType1<T, U> {
36+
init(t: T, mt2: MyType2<T>) where U == MyType2<T> {}
37+
}
38+
39+
struct MyType2<T> {
40+
init(t: T) {}
41+
}
42+
43+
let _: MyType2<_> = .init(t: "c" as Character)
44+
let _: MyType1<_, MyType2<_>> = .init(t: "s" as Character, mt2: .init(t: "c" as Character))
45+
46+
func dictionary<K, V>(ofType: [K: V].Type) -> [K: V] { [:] }
47+
48+
let _: [String: _] = dictionary(ofType: [_: Int].self)
49+
let _: [_: _] = dictionary(ofType: [String: Int].self)
50+
let _: [String: Int] = dictionary(ofType: _.self)
51+

0 commit comments

Comments
 (0)