|
| 1 | +// RUN: %target-typecheck-verify-swift -disable-availability-checking -warn-redundant-requirements -enable-experimental-feature ImplicitSome |
| 2 | + |
| 3 | +protocol Foo { } |
| 4 | + |
| 5 | +var x: any Foo |
| 6 | + |
| 7 | +protocol SelfAsType { |
| 8 | + var x: Self { get } // expected-note{{protocol requires property 'x' with type 'U'; do you want to add a stub?}} |
| 9 | +} |
| 10 | + |
| 11 | +struct U : SelfAsType { // expected-error{{type 'U' does not conform to protocol 'SelfAsType'}} |
| 12 | + var x: any SelfAsType { self } // expected-note {{candidate has non-matching type 'any SelfAsType'}} |
| 13 | +} |
| 14 | + |
| 15 | +protocol HasSelfRequirements { |
| 16 | + func foo(_ x: Self) |
| 17 | + |
| 18 | + func returnsOwnProtocol() -> any HasSelfRequirements |
| 19 | +} |
| 20 | + |
| 21 | +protocol Bar { |
| 22 | + init() |
| 23 | + |
| 24 | + func bar() -> any Bar |
| 25 | +} |
| 26 | + |
| 27 | +func useBarAsType(_ x: any Bar) {} |
| 28 | + |
| 29 | +protocol Pub : Bar { } |
| 30 | + |
| 31 | +func refinementErasure(_ p: any Pub) { |
| 32 | + useBarAsType(p) |
| 33 | +} |
| 34 | + |
| 35 | +struct Struct1<T> { } |
| 36 | + |
| 37 | +typealias T1 = Pub & Bar |
| 38 | +typealias T2 = any Pub & Bar |
| 39 | + |
| 40 | +protocol HasAssoc { |
| 41 | + associatedtype Assoc |
| 42 | + func foo() |
| 43 | +} |
| 44 | + |
| 45 | +do { |
| 46 | + enum MyError: Error { |
| 47 | + case bad(Any) |
| 48 | + } |
| 49 | + |
| 50 | + func checkIt(_ js: Any) throws { |
| 51 | + switch js { |
| 52 | + case let dbl as any HasAssoc: |
| 53 | + throw MyError.bad(dbl) |
| 54 | + |
| 55 | + default: |
| 56 | + fatalError("wrong") |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func testHasAssoc(_ x: Any, _: any HasAssoc) { |
| 62 | + if let p = x as? any HasAssoc { |
| 63 | + p.foo() |
| 64 | + } |
| 65 | + |
| 66 | + struct ConformingType : HasAssoc { |
| 67 | + typealias Assoc = Int |
| 68 | + func foo() {} |
| 69 | + |
| 70 | + func method() -> any HasAssoc {} |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +var b: any HasAssoc |
| 75 | + |
| 76 | +protocol P {} |
| 77 | +typealias MoreHasAssoc = HasAssoc & P |
| 78 | +func testHasMoreAssoc(_ x: Any) { |
| 79 | + if let p = x as? any MoreHasAssoc { |
| 80 | + p.foo() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +typealias X = Struct1<any Pub & Bar> |
| 85 | +_ = Struct1<any Pub & Bar>.self |
| 86 | + |
| 87 | +typealias AliasWhere<T> = T |
| 88 | +where T: HasAssoc, T.Assoc == any HasAssoc |
| 89 | + |
| 90 | +struct StructWhere<T> |
| 91 | +where T: HasAssoc, |
| 92 | + T.Assoc == any HasAssoc {} |
| 93 | + |
| 94 | +protocol ProtocolWhere where T == any HasAssoc { |
| 95 | + associatedtype T |
| 96 | + |
| 97 | + associatedtype U: HasAssoc |
| 98 | + where U.Assoc == any HasAssoc |
| 99 | +} |
| 100 | + |
| 101 | +extension HasAssoc where Assoc == any HasAssoc {} |
| 102 | + |
| 103 | +func FunctionWhere<T>(_: T) |
| 104 | +where T : HasAssoc, |
| 105 | + T.Assoc == any HasAssoc {} |
| 106 | + |
| 107 | +struct SubscriptWhere { |
| 108 | + subscript<T>(_: T) -> Int |
| 109 | + where T : HasAssoc, |
| 110 | + T.Assoc == any HasAssoc { |
| 111 | + get {} |
| 112 | + set {} |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct OuterGeneric<T> { |
| 117 | + func contextuallyGenericMethod() where T == any HasAssoc {} |
| 118 | +} |
| 119 | + |
| 120 | +func testInvalidAny() { |
| 121 | + struct S: HasAssoc { |
| 122 | + typealias Assoc = Int |
| 123 | + func foo() {} |
| 124 | + } |
| 125 | + let _: any S = S() // expected-error{{'any' has no effect on concrete type 'S'}} |
| 126 | + |
| 127 | + func generic<T: HasAssoc>(t: T) { |
| 128 | + let _: any T = t // expected-error{{'any' has no effect on type parameter 'T'}} |
| 129 | + let _: any T.Assoc // expected-error {{'any' has no effect on type parameter 'T.Assoc'}} |
| 130 | + } |
| 131 | + |
| 132 | + let _: any ((S) -> Void) = generic // expected-error{{'any' has no effect on concrete type '(S) -> Void'}} |
| 133 | +} |
| 134 | + |
| 135 | +func anyAny() { |
| 136 | + let _: any Any |
| 137 | + let _: any AnyObject |
| 138 | +} |
| 139 | + |
| 140 | +protocol P1 {} |
| 141 | +protocol P2 {} |
| 142 | +protocol P3 {} |
| 143 | +do { |
| 144 | + // Test that we don't accidentally misparse an 'any' type as a 'some' type |
| 145 | + // and vice versa. |
| 146 | + let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{15-19=}} {{10-10=any }} |
| 147 | + let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{19-23=}} |
| 148 | + let _: any P1 & P2 & any P3 // expected-error {{'any' should appear at the beginning of a composition}} {{24-28=}} |
| 149 | + let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}} {{19-24=}} |
| 150 | + let _: some P1 & any P2 |
| 151 | + // expected-error@-1 {{'some' type can only be declared on a single property declaration}} |
| 152 | + // expected-error@-2 {{'any' should appear at the beginning of a composition}} {{20-24=}} |
| 153 | +} |
| 154 | + |
| 155 | +struct ConcreteComposition: P1, P2 {} |
| 156 | + |
| 157 | +func testMetatypes() { |
| 158 | + let _: any P1.Type = ConcreteComposition.self |
| 159 | + let _: any (P1 & P2).Type = ConcreteComposition.self |
| 160 | +} |
| 161 | + |
| 162 | +func generic<T: any P1>(_ t: T) {} // expected-error {{type 'T' constrained to non-protocol, non-class type 'any P1'}} |
| 163 | + |
| 164 | +public protocol MyError {} |
| 165 | + |
| 166 | +extension MyError { |
| 167 | + static func ~=(lhs: any Error, rhs: Self) -> Bool { |
| 168 | + return true |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +struct Wrapper { |
| 173 | + typealias E = Error |
| 174 | +} |
| 175 | + |
| 176 | +func typealiasMemberReferences(metatype: Wrapper.Type) { |
| 177 | + let _: any Wrapper.E.Protocol = metatype.E.self // expected-error{{'any' has no effect on concrete type '(any Wrapper.E).Type' (aka '(any Error).Type')}} |
| 178 | + let _: (any Wrapper.E).Type = metatype.E.self |
| 179 | +} |
| 180 | + |
| 181 | +func testAnyTypeExpr() { |
| 182 | + let _: (any P).Type = (any P).self |
| 183 | + |
| 184 | + func test(_: (any P).Type) {} |
| 185 | + test((any P).self) |
| 186 | + |
| 187 | + // expected-error@+2 {{expected member name or constructor call after type name}} |
| 188 | + // expected-note@+1 {{use '.self' to reference the type object}} |
| 189 | + let invalid = any P |
| 190 | + test(invalid) |
| 191 | + |
| 192 | + // Make sure 'any' followed by an identifier |
| 193 | + // on the next line isn't parsed as a type. |
| 194 | + func doSomething() {} |
| 195 | + |
| 196 | + let any = 10 |
| 197 | + let _ = any |
| 198 | + doSomething() |
| 199 | +} |
| 200 | + |
| 201 | +func hasInvalidExistential(_: any DoesNotExistIHope) {} |
| 202 | +// expected-error@-1 {{cannot find type 'DoesNotExistIHope' in scope}} |
| 203 | + |
| 204 | +protocol Input { |
| 205 | + associatedtype A |
| 206 | +} |
| 207 | +protocol Output { |
| 208 | + associatedtype A |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +// NOT SURE IF THIS WILL WORK |
| 214 | + |
| 215 | +// ImplicitSome feature always expects explicit 'any' and plain protocols now imply 'some' |
| 216 | +// Verify existential_requires_any error is no longer produced |
| 217 | +typealias OpaqueFunction = (Input) -> Output |
| 218 | +func testOpaqueFunctionAlias(fn: OpaqueFunction) {} |
| 219 | + |
| 220 | +typealias ExistentialFunction = (any Input) -> any Output |
| 221 | +func testFunctionAlias(fn: ExistentialFunction) {} |
| 222 | + |
| 223 | +typealias Constraint = Input |
| 224 | +func testConstraintAlias(x: Constraint) {} |
| 225 | + |
| 226 | +typealias Existential = any Input |
| 227 | +func testExistentialAlias(x: Existential, y: any Constraint) {} |
| 228 | + |
| 229 | +// Reject explicit existential types in inheritance clauses |
| 230 | +protocol Empty {} |
| 231 | + |
| 232 | +struct S : any Empty {} // expected-error {{inheritance from non-protocol type 'any Empty'}} |
| 233 | +class C : any Empty {} // expected-error {{inheritance from non-protocol, non-class type 'any Empty'}} |
| 234 | + |
| 235 | +// FIXME: Diagnostics are not great in the enum case because we confuse this with a raw type |
| 236 | + |
| 237 | +enum E : any Empty { // expected-error {{raw type 'any Empty' is not expressible by a string, integer, or floating-point literal}} |
| 238 | +// expected-error@-1 {{'E' declares raw type 'any Empty', but does not conform to RawRepresentable and conformance could not be synthesized}} |
| 239 | +// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'any Empty' is not Equatable}} |
| 240 | + case hack |
| 241 | +} |
| 242 | + |
| 243 | +enum EE : Equatable, any Empty { // expected-error {{raw type 'any Empty' is not expressible by a string, integer, or floating-point literal}} |
| 244 | +// expected-error@-1 {{'EE' declares raw type 'any Empty', but does not conform to RawRepresentable and conformance could not be synthesized}} |
| 245 | +// expected-error@-2 {{RawRepresentable conformance cannot be synthesized because raw type 'any Empty' is not Equatable}} |
| 246 | +// expected-error@-3 {{raw type 'any Empty' must appear first in the enum inheritance clause}} |
| 247 | + case hack |
| 248 | +} |
| 249 | + |
| 250 | +func testAnyFixIt() { |
| 251 | + struct ConformingType : HasAssoc { |
| 252 | + typealias Assoc = Int |
| 253 | + func foo() {} |
| 254 | + |
| 255 | + func method() -> any HasAssoc {} |
| 256 | + } |
| 257 | + |
| 258 | + let _: any HasAssoc = ConformingType() |
| 259 | + let _: Optional<any HasAssoc> = nil |
| 260 | + let _: any HasAssoc.Type = ConformingType.self |
| 261 | + let _: (any HasAssoc.Type) = ConformingType.self |
| 262 | + let _: ((any HasAssoc.Type)) = ConformingType.self |
| 263 | + let _: (any HasAssoc).Protocol = (any HasAssoc).self |
| 264 | + let _: (any HasAssoc)? = ConformingType() |
| 265 | + let _: (any HasAssoc.Type)? = ConformingType.self |
| 266 | + let _: (any HasAssoc).Protocol? = (any HasAssoc).self |
| 267 | + |
| 268 | + // expected-error@+1 {{optional 'any' type must be written '(any HasAssoc)?'}}{{10-23=(any HasAssoc)?}} |
| 269 | + let _: any HasAssoc? = nil |
| 270 | + // expected-error@+1 {{optional 'any' type must be written '(any HasAssoc.Type)?'}}{{10-28=(any HasAssoc.Type)?}} |
| 271 | + let _: any HasAssoc.Type? = nil |
| 272 | +} |
| 273 | + |
| 274 | +func testNestedMetatype() { |
| 275 | + let _: (any P.Type).Type = (any P.Type).self |
| 276 | + let _: (any (P.Type)).Type = (any P.Type).self |
| 277 | + let _: ((any (P.Type))).Type = (any P.Type).self |
| 278 | +} |
| 279 | + |
| 280 | +func testEnumAssociatedValue() { |
| 281 | + enum E { |
| 282 | + case c1((any HasAssoc) -> Void) |
| 283 | + } |
| 284 | +} |
| 285 | + |
| 286 | +// https://github.com/apple/swift/issues/58920 |
| 287 | +typealias Iterator = any IteratorProtocol |
| 288 | +var example: any Iterator = 5 // expected-error{{redundant 'any' in type 'any Iterator' (aka 'any any IteratorProtocol')}} {{14-18=}} |
| 289 | +// expected-error@-1{{value of type 'Int' does not conform to specified type 'IteratorProtocol'}} |
| 290 | +var example1: any (any IteratorProtocol) = 5 // expected-error{{redundant 'any' in type 'any (any IteratorProtocol)'}} {{15-19=}} |
| 291 | +// expected-error@-1{{value of type 'Int' does not conform to specified type 'IteratorProtocol'}} |
| 292 | + |
| 293 | +protocol PP {} |
| 294 | +struct A : PP {} |
| 295 | +let _: any PP = A() // Ok |
| 296 | +let _: any (any PP) = A() // expected-error{{redundant 'any' in type 'any (any PP)'}} {{8-12=}} |
| 297 | + |
| 298 | + |
0 commit comments