forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplicit_existential.swift
238 lines (182 loc) · 5.71 KB
/
explicit_existential.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// RUN: %target-typecheck-verify-swift -enable-explicit-existential-types
protocol HasSelfRequirements {
func foo(_ x: Self)
func returnsOwnProtocol() -> any HasSelfRequirements
}
protocol Bar {
init()
func bar() -> any Bar
}
func useBarAsType(_ x: any Bar) {}
protocol Pub : Bar { }
func refinementErasure(_ p: any Pub) {
useBarAsType(p)
}
typealias Compo = HasSelfRequirements & Bar
struct CompoAssocType {
typealias Compo = HasSelfRequirements & Bar
}
func useAsRequirement<T: HasSelfRequirements>(_ x: T) { }
func useCompoAsRequirement<T: HasSelfRequirements & Bar>(_ x: T) { }
func useCompoAliasAsRequirement<T: Compo>(_ x: T) { }
func useNestedCompoAliasAsRequirement<T: CompoAssocType.Compo>(_ x: T) { }
func useAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements {}
func useCompoAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements & Bar {}
func useCompoAliasAsWhereRequirement<T>(_ x: T) where T: Compo {}
func useNestedCompoAliasAsWhereRequirement<T>(_ x: T) where T: CompoAssocType.Compo {}
func useAsType(_: any HasSelfRequirements,
_: any HasSelfRequirements & Bar,
_: any Compo,
_: any CompoAssocType.Compo) { }
struct TypeRequirement<T: HasSelfRequirements> {}
struct CompoTypeRequirement<T: HasSelfRequirements & Bar> {}
struct CompoAliasTypeRequirement<T: Compo> {}
struct NestedCompoAliasTypeRequirement<T: CompoAssocType.Compo> {}
struct CompoTypeWhereRequirement<T> where T: HasSelfRequirements & Bar {}
struct CompoAliasTypeWhereRequirement<T> where T: Compo {}
struct NestedCompoAliasTypeWhereRequirement<T> where T: CompoAssocType.Compo {}
struct Struct1<T> { }
typealias T1 = Pub & Bar
typealias T2 = any Pub & Bar
protocol HasAssoc {
associatedtype Assoc
func foo()
}
do {
enum MyError: Error {
case bad(Any)
}
func checkIt(_ js: Any) throws {
switch js {
case let dbl as any HasAssoc:
throw MyError.bad(dbl)
default:
fatalError("wrong")
}
}
}
func testHasAssoc(_ x: Any, _: any HasAssoc) {
if let p = x as? any HasAssoc {
p.foo()
}
struct ConformingType : HasAssoc {
typealias Assoc = Int
func foo() {}
func method() -> any HasAssoc {}
}
}
var b: any HasAssoc
protocol P {}
typealias MoreHasAssoc = HasAssoc & P
func testHasMoreAssoc(_ x: Any) {
if let p = x as? any MoreHasAssoc {
p.foo()
}
}
typealias X = Struct1<any Pub & Bar>
_ = Struct1<any Pub & Bar>.self
typealias AliasWhere<T> = T
where T: HasAssoc, T.Assoc == any HasAssoc
struct StructWhere<T>
where T: HasAssoc,
T.Assoc == any HasAssoc {}
protocol ProtocolWhere where T == any HasAssoc {
associatedtype T
associatedtype U: HasAssoc
where U.Assoc == any HasAssoc
}
extension HasAssoc where Assoc == any HasAssoc {}
func FunctionWhere<T>(_: T)
where T : HasAssoc,
T.Assoc == any HasAssoc {}
struct SubscriptWhere {
subscript<T>(_: T) -> Int
where T : HasAssoc,
T.Assoc == any HasAssoc {
get {}
set {}
}
}
struct OuterGeneric<T> {
func contextuallyGenericMethod() where T == any HasAssoc {}
}
func testInvalidAny() {
struct S: HasAssoc {
typealias Assoc = Int
func foo() {}
}
let _: any S = S() // expected-error{{'any' has no effect on concrete type 'S'}}
func generic<T: HasAssoc>(t: T) {
let _: any T = t // expected-error{{'any' has no effect on type parameter 'T'}}
let _: any T.Assoc // expected-error {{'any' has no effect on type parameter 'T.Assoc'}}
}
let _: any ((S) -> Void) = generic // expected-error{{'any' has no effect on concrete type '(S) -> Void'}}
}
func anyAny() {
let _: any Any
let _: any AnyObject
}
protocol P1 {}
protocol P2 {}
do {
// Test that we don't accidentally misparse an 'any' type as a 'some' type
// and vice versa.
let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}}
let _: some P1 & any P2
// expected-error@-1 {{'some' type can only be declared on a single property declaration}}
// expected-error@-2 {{'any' should appear at the beginning of a composition}}
}
struct ConcreteComposition: P1, P2 {}
func testMetatypes() {
let _: any P1.Type = ConcreteComposition.self
let _: any (P1 & P2).Type = ConcreteComposition.self
}
func generic<T: any P1>(_ t: T) {} // expected-error {{type 'T' constrained to non-protocol, non-class type 'any P1'}}
protocol RawRepresentable {
associatedtype RawValue
var rawValue: RawValue { get }
}
enum E1: RawRepresentable {
typealias RawValue = P1
var rawValue: P1 {
return ConcreteComposition()
}
}
enum E2: RawRepresentable {
typealias RawValue = any P1
var rawValue: any P1 {
return ConcreteComposition()
}
}
public protocol MyError {}
extension MyError {
static func ~=(lhs: any Error, rhs: Self) -> Bool {
return true
}
}
struct Wrapper {
typealias E = Error
}
func typealiasMemberReferences(metatype: Wrapper.Type) {
let _: Wrapper.E.Protocol = metatype.E.self
let _: (any Wrapper.E).Type = metatype.E.self
}
func testAnyTypeExpr() {
let _: (any P).Type = (any P).self
func test(_: (any P).Type) {}
test((any P).self)
// expected-error@+2 {{expected member name or constructor call after type name}}
// expected-note@+1 {{use '.self' to reference the type object}}
let invalid = any P
test(invalid)
// Make sure 'any' followed by an identifier
// on the next line isn't parsed as a type.
func doSomething() {}
let any = 10
let _ = any
doSomething()
}
func hasInvalidExistential(_: any DoesNotExistIHope) {}
// expected-error@-1 {{cannot find type 'DoesNotExistIHope' in scope}}