|
| 1 | +// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ExistentialAny |
| 2 | + |
| 3 | +protocol HasSelfRequirements { |
| 4 | + func foo(_ x: Self) |
| 5 | + |
| 6 | + func returnsOwnProtocol() -> any HasSelfRequirements |
| 7 | +} |
| 8 | +protocol Bar { |
| 9 | + init() |
| 10 | + |
| 11 | + func bar() -> any Bar |
| 12 | +} |
| 13 | + |
| 14 | +class Bistro{ |
| 15 | + convenience init(_: Bar){ self.init()} // expected-error{{use of protocol 'Bar' as a type must be written 'any Bar'}}{{25-28=any Bar}} |
| 16 | + class func returnBar() -> Bar {} // expected-error {{use of protocol 'Bar' as a type must be written 'any Bar'}}{{31-34=any Bar}} |
| 17 | +} |
| 18 | + |
| 19 | +func useBarAsType(_ x: any Bar) {} |
| 20 | + |
| 21 | +protocol Pub : Bar { } |
| 22 | + |
| 23 | +func refinementErasure(_ p: any Pub) { |
| 24 | + useBarAsType(p) |
| 25 | +} |
| 26 | + |
| 27 | +typealias Compo = HasSelfRequirements & Bar |
| 28 | + |
| 29 | +struct CompoAssocType { |
| 30 | + typealias Compo = HasSelfRequirements & Bar |
| 31 | +} |
| 32 | + |
| 33 | +func useAsRequirement<T: HasSelfRequirements>(_ x: T) { } |
| 34 | +func useCompoAsRequirement<T: HasSelfRequirements & Bar>(_ x: T) { } |
| 35 | +func useCompoAliasAsRequirement<T: Compo>(_ x: T) { } |
| 36 | +func useNestedCompoAliasAsRequirement<T: CompoAssocType.Compo>(_ x: T) { } |
| 37 | + |
| 38 | +func useAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements {} |
| 39 | +func useCompoAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements & Bar {} |
| 40 | +func useCompoAliasAsWhereRequirement<T>(_ x: T) where T: Compo {} |
| 41 | +func useNestedCompoAliasAsWhereRequirement<T>(_ x: T) where T: CompoAssocType.Compo {} |
| 42 | + |
| 43 | +func useAsType(_: any HasSelfRequirements, |
| 44 | + _: any HasSelfRequirements & Bar, |
| 45 | + _: any Compo, |
| 46 | + _: any CompoAssocType.Compo) { } |
| 47 | + |
| 48 | +struct TypeRequirement<T: HasSelfRequirements> {} |
| 49 | +struct CompoTypeRequirement<T: HasSelfRequirements & Bar> {} |
| 50 | +struct CompoAliasTypeRequirement<T: Compo> {} |
| 51 | +struct NestedCompoAliasTypeRequirement<T: CompoAssocType.Compo> {} |
| 52 | + |
| 53 | +struct CompoTypeWhereRequirement<T> where T: HasSelfRequirements & Bar {} |
| 54 | +struct CompoAliasTypeWhereRequirement<T> where T: Compo {} |
| 55 | +struct NestedCompoAliasTypeWhereRequirement<T> where T: CompoAssocType.Compo {} |
| 56 | + |
| 57 | +struct Struct1<T> { } |
| 58 | + |
| 59 | +typealias T1 = Pub & Bar |
| 60 | +typealias T2 = any Pub & Bar |
| 61 | + |
| 62 | +protocol HasAssoc { |
| 63 | + associatedtype Assoc |
| 64 | + func foo() |
| 65 | +} |
| 66 | +struct foo { |
| 67 | + func existentialArray() -> [HasAssoc] {} |
| 68 | + func existentialcSequence() -> Sequence<HasAssoc> {} |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +do { |
| 73 | + enum MyError: Error { |
| 74 | + case bad(Any) |
| 75 | + } |
| 76 | + |
| 77 | + func checkIt(_ js: Any) throws { |
| 78 | + switch js { |
| 79 | + case let dbl as any HasAssoc: |
| 80 | + throw MyError.bad(dbl) |
| 81 | + |
| 82 | + default: |
| 83 | + fatalError("wrong") |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func testHasAssoc(_ x: Any, _: any HasAssoc) { |
| 89 | + if let p = x as? any HasAssoc { |
| 90 | + p.foo() |
| 91 | + } |
| 92 | + |
| 93 | + struct ConformingType : HasAssoc { |
| 94 | + typealias Assoc = Int |
| 95 | + func foo() {} |
| 96 | + |
| 97 | + func method() -> any HasAssoc {} |
| 98 | + func existentialArray() -> [any HasAssoc] {} |
| 99 | + func existentialcSequence() -> any Sequence<HasAssoc> {} |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +var b: any HasAssoc |
| 104 | + |
| 105 | +protocol P {} |
| 106 | +typealias MoreHasAssoc = HasAssoc & P |
| 107 | +func testHasMoreAssoc(_ x: Any) { |
| 108 | + if let p = x as? any MoreHasAssoc { |
| 109 | + p.foo() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +typealias X = Struct1<any Pub & Bar> |
| 114 | +_ = Struct1<any Pub & Bar>.self |
| 115 | + |
| 116 | +typealias AliasWhere<T> = T |
| 117 | +where T: HasAssoc, T.Assoc == any HasAssoc |
| 118 | + |
| 119 | +struct StructWhere<T> |
| 120 | +where T: HasAssoc, |
| 121 | + T.Assoc == any HasAssoc {} |
| 122 | + |
| 123 | +protocol ProtocolWhere where T == any HasAssoc { |
| 124 | + associatedtype T |
| 125 | + |
| 126 | + associatedtype U: HasAssoc |
| 127 | + where U.Assoc == any HasAssoc |
| 128 | +} |
| 129 | + |
| 130 | +extension HasAssoc where Assoc == any HasAssoc {} |
| 131 | + |
| 132 | +func FunctionWhere<T>(_: T) |
| 133 | +where T : HasAssoc, |
| 134 | + T.Assoc == any HasAssoc {} |
| 135 | + |
| 136 | +struct SubscriptWhere { |
| 137 | + subscript<T>(_: T) -> Int |
| 138 | + where T : HasAssoc, |
| 139 | + T.Assoc == any HasAssoc { |
| 140 | + get {} |
| 141 | + set {} |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +struct OuterGeneric<T> { |
| 146 | + func contextuallyGenericMethod() where T == any HasAssoc {} |
| 147 | +} |
| 148 | + |
| 149 | +protocol Collection<T> { |
| 150 | + associatedtype T |
| 151 | +} |
| 152 | + |
| 153 | +func acceptAny(_: Collection<Int>) {} |
| 154 | +// expected-error@-1 {{use of protocol 'Collection' as a type must be written 'any Collection'}} |
| 155 | +func returnsAny() -> Collection<Int> {} |
| 156 | +// expected-error@-1 {{use of protocol 'Collection' as a type must be written 'any Collection'}} |
| 157 | + |
| 158 | +func testInvalidAny() { |
| 159 | + struct S: HasAssoc { |
| 160 | + typealias Assoc = Int |
| 161 | + func foo() {} |
| 162 | + } |
| 163 | + let _: any S = S() // expected-error{{'any' has no effect on concrete type 'S'}} |
| 164 | + |
| 165 | + func generic<T: HasAssoc>(t: T) { |
| 166 | + let _: any T = t // expected-error{{'any' has no effect on type parameter 'T'}} |
| 167 | + let _: any T.Assoc // expected-error {{'any' has no effect on type parameter 'T.Assoc'}} |
| 168 | + } |
| 169 | + |
| 170 | + let _: any ((S) -> Void) = generic // expected-error{{'any' has no effect on concrete type '(S) -> Void'}} |
| 171 | +} |
| 172 | + |
| 173 | +func anyAny() { |
| 174 | + let _: any Any |
| 175 | + let _: any AnyObject |
| 176 | +} |
| 177 | + |
| 178 | +protocol P1 {} |
| 179 | +protocol P2 {} |
| 180 | +protocol P3 {} |
| 181 | +do { |
| 182 | + // Test that we don't accidentally misparse an 'any' type as a 'some' type |
| 183 | + // and vice versa. |
| 184 | + let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{15-19=}} {{10-10=any }} |
| 185 | + let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{19-23=}} |
| 186 | + let _: any P1 & P2 & any P3 // expected-error {{'any' should appear at the beginning of a composition}} {{24-28=}} |
| 187 | + let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}} {{19-24=}} |
| 188 | + let _: some P1 & any P2 |
| 189 | + // expected-error@-1 {{'some' type can only be declared on a single property declaration}} |
| 190 | + // expected-error@-2 {{'any' should appear at the beginning of a composition}} {{20-24=}} |
| 191 | +} |
| 192 | + |
| 193 | +struct ConcreteComposition: P1, P2 {} |
| 194 | + |
| 195 | +func testMetatypes() { |
| 196 | + let _: any P1.Type = ConcreteComposition.self |
| 197 | + let _: any (P1 & P2).Type = ConcreteComposition.self |
| 198 | +} |
| 199 | + |
| 200 | +func generic<T: any P1>(_ t: T) {} // expected-error {{type 'T' constrained to non-protocol, non-class type 'any P1'}} |
| 201 | + |
| 202 | +protocol RawRepresentable { |
| 203 | + associatedtype RawValue |
| 204 | + var rawValue: RawValue { get } |
| 205 | +} |
| 206 | + |
| 207 | +enum E1: RawRepresentable { |
| 208 | + typealias RawValue = P1 |
| 209 | + |
| 210 | + var rawValue: P1 { |
| 211 | + return ConcreteComposition() |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +enum E2: RawRepresentable { |
| 216 | + typealias RawValue = any P1 |
| 217 | + |
| 218 | + var rawValue: any P1 { |
| 219 | + return ConcreteComposition() |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +public protocol MyError {} |
| 224 | + |
| 225 | +extension MyError { |
| 226 | + static func ~=(lhs: any Error, rhs: Self) -> Bool { |
| 227 | + return true |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +struct Wrapper { |
| 232 | + typealias E = Error |
| 233 | +} |
| 234 | + |
| 235 | +func typealiasMemberReferences(metatype: Wrapper.Type) { |
| 236 | + let _: Wrapper.E.Protocol = metatype.E.self |
| 237 | + let _: (any Wrapper.E).Type = metatype.E.self |
| 238 | +} |
| 239 | + |
| 240 | +func testAnyTypeExpr() { |
| 241 | + let _: (any P).Type = (any P).self |
| 242 | + |
| 243 | + func test(_: (any P).Type) {} |
| 244 | + test((any P).self) |
| 245 | + |
| 246 | + // expected-error@+2 {{expected member name or constructor call after type name}} |
| 247 | + // expected-note@+1 {{use '.self' to reference the type object}} |
| 248 | + let invalid = any P |
| 249 | + test(invalid) |
| 250 | + |
| 251 | + // Make sure 'any' followed by an identifier |
| 252 | + // on the next line isn't parsed as a type. |
| 253 | + func doSomething() {} |
| 254 | + |
| 255 | + let any = 10 |
| 256 | + let _ = any |
| 257 | + doSomething() |
| 258 | +} |
| 259 | + |
| 260 | +func hasInvalidExistential(_: any DoesNotExistIHope) {} |
| 261 | +// expected-error@-1 {{cannot find type 'DoesNotExistIHope' in scope}} |
| 262 | + |
| 263 | +protocol Input { |
| 264 | + associatedtype A |
| 265 | +} |
| 266 | +protocol InputB { |
| 267 | + associatedtype B |
| 268 | +} |
| 269 | + |
| 270 | +protocol Output { |
| 271 | + associatedtype A |
| 272 | +} |
| 273 | + |
| 274 | +// expected-error@+2{{use of protocol 'Input' as a type must be written 'any Input'}}{{30-35=any Input}} |
| 275 | +// expected-error@+1{{use of protocol 'Output' as a type must be written 'any Output'}}{{40-46=any Output}} |
| 276 | +typealias InvalidFunction = (Input) -> Output |
| 277 | +func testInvalidFunctionAlias(fn: InvalidFunction) {} |
| 278 | + |
| 279 | +typealias ExistentialFunction = (any Input) -> any Output |
| 280 | +func testFunctionAlias(fn: ExistentialFunction) {} |
| 281 | + |
| 282 | +typealias Constraint = Input |
| 283 | +typealias ConstraintB = Input & InputB |
| 284 | + |
| 285 | +// expected-error@+2{{use of 'Constraint' (aka 'Input') as a type must be written 'any Constraint'}}{{29-39=any Constraint}} |
| 286 | +// expected-error@+1{{use of 'ConstraintB' (aka 'InputB & Input') as a type must be written 'any ConstraintB' (aka 'any InputB & Input')'}}{{44-55=any Constraint}} |
| 287 | +func testConstraintAlias(x: Constraint, y: ConstraintB) {} |
| 288 | + |
| 289 | +typealias Existential = any Input |
| 290 | +func testExistentialAlias(x: Existential, y: any Constraint) {} |
| 291 | + |
| 292 | +// Reject explicit existential types in inheritance clauses |
| 293 | +protocol Empty {} |
| 294 | + |
| 295 | +struct S : any Empty {} // expected-error {{inheritance from non-protocol type 'any Empty'}} |
| 296 | +class C : any Empty {} // expected-error {{inheritance from non-protocol, non-class type 'any Empty'}} |
| 297 | + |
| 298 | +// FIXME: Diagnostics are not great in the enum case because we confuse this with a raw type |
| 299 | + |
| 300 | +enum E : any Empty { // expected-error {{raw type 'any Empty' is not expressible by a string, integer, or floating-point literal}} |
| 301 | +// expected-error@-1 {{'E' declares raw type 'any Empty', but does not conform to RawRepresentable and conformance could not be synthesized}} |
| 302 | +// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'any Empty' is not Equatable}} |
| 303 | + case hack |
| 304 | +} |
| 305 | + |
| 306 | +enum EE : Equatable, any Empty { // expected-error {{raw type 'any Empty' is not expressible by a string, integer, or floating-point literal}} |
| 307 | +// expected-error@-1 {{'EE' declares raw type 'any Empty', but does not conform to RawRepresentable and conformance could not be synthesized}} |
| 308 | +// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'any Empty' is not Equatable}} |
| 309 | +// expected-error@-3 {{raw type 'any Empty' must appear first in the enum inheritance clause}} |
| 310 | + case hack |
| 311 | +} |
| 312 | + |
| 313 | + |
| 314 | +func testAnyFixIt() { |
| 315 | + struct ConformingType : HasAssoc { |
| 316 | + typealias Assoc = Int |
| 317 | + func foo() {} |
| 318 | + |
| 319 | + func method() -> any HasAssoc {} |
| 320 | + } |
| 321 | + |
| 322 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=any HasAssoc}} |
| 323 | + let _: HasAssoc = ConformingType() |
| 324 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{19-27=any HasAssoc}} |
| 325 | + let _: Optional<HasAssoc> = nil |
| 326 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=any HasAssoc.Type}} |
| 327 | + let _: HasAssoc.Type = ConformingType.self |
| 328 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-25=any (HasAssoc).Type}} |
| 329 | + let _: (HasAssoc).Type = ConformingType.self |
| 330 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-27=any ((HasAssoc)).Type}} |
| 331 | + let _: ((HasAssoc)).Type = ConformingType.self |
| 332 | + // expected-error@+2 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}} |
| 333 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{30-38=(any HasAssoc)}} |
| 334 | + let _: HasAssoc.Protocol = HasAssoc.self |
| 335 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{11-19=any HasAssoc}} |
| 336 | + let _: (HasAssoc).Protocol = (any HasAssoc).self |
| 337 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}} |
| 338 | + let _: HasAssoc? = ConformingType() |
| 339 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=(any HasAssoc.Type)}} |
| 340 | + let _: HasAssoc.Type? = ConformingType.self |
| 341 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}} |
| 342 | + let _: HasAssoc.Protocol? = (any HasAssoc).self |
| 343 | + |
| 344 | + // expected-error@+1 {{optional 'any' type must be written '(any HasAssoc)?'}}{{10-23=(any HasAssoc)?}} |
| 345 | + let _: any HasAssoc? = nil |
| 346 | + // expected-error@+1 {{optional 'any' type must be written '(any HasAssoc.Type)?'}}{{10-28=(any HasAssoc.Type)?}} |
| 347 | + let _: any HasAssoc.Type? = nil |
| 348 | +} |
| 349 | + |
| 350 | +func testNestedMetatype() { |
| 351 | + let _: (any P.Type).Type = (any P.Type).self |
| 352 | + let _: (any (P.Type)).Type = (any P.Type).self |
| 353 | + let _: ((any (P.Type))).Type = (any P.Type).self |
| 354 | +} |
| 355 | + |
| 356 | +func testEnumAssociatedValue() { |
| 357 | + enum E { |
| 358 | + case c1((any HasAssoc) -> Void) |
| 359 | + // expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}} |
| 360 | + case c2((HasAssoc) -> Void) |
| 361 | + case c3((P) -> Void) |
| 362 | + } |
| 363 | +} |
| 364 | + |
| 365 | +// https://github.com/apple/swift/issues/58920 |
| 366 | +typealias Iterator = any IteratorProtocol |
| 367 | +var example: any Iterator = 5 // expected-error{{redundant 'any' has no effect on existential type 'Iterator' (aka 'any IteratorProtocol')}} {{14-18=}} |
| 368 | +// expected-error@-1{{value of type 'Int' does not conform to specified type 'IteratorProtocol'}} |
| 369 | +var example1: any (any IteratorProtocol) = 5 // expected-error{{redundant 'any' has no effect on existential type 'any IteratorProtocol'}} {{15-19=}} |
| 370 | +// expected-error@-1{{value of type 'Int' does not conform to specified type 'IteratorProtocol'}} |
| 371 | + |
| 372 | +protocol PP {} |
| 373 | +struct A : PP {} |
| 374 | +let _: any PP = A() // Ok |
| 375 | +let _: any (any PP) = A() // expected-error{{redundant 'any' has no effect on existential type 'any PP'}} {{8-12=}} |
0 commit comments