|
| 1 | +// RUN: %target-typecheck-verify-swift -enable-explicit-existential-types |
| 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 | +func useBarAsType(_ x: any Bar) {} |
| 15 | + |
| 16 | +protocol Pub : Bar { } |
| 17 | + |
| 18 | +func refinementErasure(_ p: any Pub) { |
| 19 | + useBarAsType(p) |
| 20 | +} |
| 21 | + |
| 22 | +typealias Compo = HasSelfRequirements & Bar |
| 23 | + |
| 24 | +struct CompoAssocType { |
| 25 | + typealias Compo = HasSelfRequirements & Bar |
| 26 | +} |
| 27 | + |
| 28 | +func useAsRequirement<T: HasSelfRequirements>(_ x: T) { } |
| 29 | +func useCompoAsRequirement<T: HasSelfRequirements & Bar>(_ x: T) { } |
| 30 | +func useCompoAliasAsRequirement<T: Compo>(_ x: T) { } |
| 31 | +func useNestedCompoAliasAsRequirement<T: CompoAssocType.Compo>(_ x: T) { } |
| 32 | + |
| 33 | +func useAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements {} |
| 34 | +func useCompoAsWhereRequirement<T>(_ x: T) where T: HasSelfRequirements & Bar {} |
| 35 | +func useCompoAliasAsWhereRequirement<T>(_ x: T) where T: Compo {} |
| 36 | +func useNestedCompoAliasAsWhereRequirement<T>(_ x: T) where T: CompoAssocType.Compo {} |
| 37 | + |
| 38 | +func useAsType(_: any HasSelfRequirements, |
| 39 | + _: any HasSelfRequirements & Bar, |
| 40 | + _: any Compo, |
| 41 | + _: any CompoAssocType.Compo) { } |
| 42 | + |
| 43 | +struct TypeRequirement<T: HasSelfRequirements> {} |
| 44 | +struct CompoTypeRequirement<T: HasSelfRequirements & Bar> {} |
| 45 | +struct CompoAliasTypeRequirement<T: Compo> {} |
| 46 | +struct NestedCompoAliasTypeRequirement<T: CompoAssocType.Compo> {} |
| 47 | + |
| 48 | +struct CompoTypeWhereRequirement<T> where T: HasSelfRequirements & Bar {} |
| 49 | +struct CompoAliasTypeWhereRequirement<T> where T: Compo {} |
| 50 | +struct NestedCompoAliasTypeWhereRequirement<T> where T: CompoAssocType.Compo {} |
| 51 | + |
| 52 | +struct Struct1<T> { } |
| 53 | + |
| 54 | +typealias T1 = Pub & Bar |
| 55 | +typealias T2 = any Pub & Bar |
| 56 | + |
| 57 | +protocol HasAssoc { |
| 58 | + associatedtype Assoc |
| 59 | + func foo() |
| 60 | +} |
| 61 | + |
| 62 | +do { |
| 63 | + enum MyError: Error { |
| 64 | + case bad(Any) |
| 65 | + } |
| 66 | + |
| 67 | + func checkIt(_ js: Any) throws { |
| 68 | + switch js { |
| 69 | + case let dbl as any HasAssoc: |
| 70 | + throw MyError.bad(dbl) |
| 71 | + |
| 72 | + default: |
| 73 | + fatalError("wrong") |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func testHasAssoc(_ x: Any, _: any HasAssoc) { |
| 79 | + if let p = x as? any HasAssoc { |
| 80 | + p.foo() |
| 81 | + } |
| 82 | + |
| 83 | + struct ConformingType : HasAssoc { |
| 84 | + typealias Assoc = Int |
| 85 | + func foo() {} |
| 86 | + |
| 87 | + func method() -> any HasAssoc {} |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +var b: any HasAssoc |
| 92 | + |
| 93 | +protocol P {} |
| 94 | +typealias MoreHasAssoc = HasAssoc & P |
| 95 | +func testHasMoreAssoc(_ x: Any) { |
| 96 | + if let p = x as? any MoreHasAssoc { |
| 97 | + p.foo() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +typealias X = Struct1<any Pub & Bar> |
| 102 | +_ = Struct1<any Pub & Bar>.self |
| 103 | + |
| 104 | +typealias AliasWhere<T> = T |
| 105 | +where T: HasAssoc, T.Assoc == any HasAssoc |
| 106 | + |
| 107 | +struct StructWhere<T> |
| 108 | +where T: HasAssoc, |
| 109 | + T.Assoc == any HasAssoc {} |
| 110 | + |
| 111 | +protocol ProtocolWhere where T == any HasAssoc { |
| 112 | + associatedtype T |
| 113 | + |
| 114 | + associatedtype U: HasAssoc |
| 115 | + where U.Assoc == any HasAssoc |
| 116 | +} |
| 117 | + |
| 118 | +extension HasAssoc where Assoc == any HasAssoc {} |
| 119 | + |
| 120 | +func FunctionWhere<T>(_: T) |
| 121 | +where T : HasAssoc, |
| 122 | + T.Assoc == any HasAssoc {} |
| 123 | + |
| 124 | +struct SubscriptWhere { |
| 125 | + subscript<T>(_: T) -> Int |
| 126 | + where T : HasAssoc, |
| 127 | + T.Assoc == any HasAssoc { |
| 128 | + get {} |
| 129 | + set {} |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +struct OuterGeneric<T> { |
| 134 | + func contextuallyGenericMethod() where T == any HasAssoc {} |
| 135 | +} |
| 136 | + |
| 137 | +func testInvalidAny() { |
| 138 | + struct S: HasAssoc { |
| 139 | + typealias Assoc = Int |
| 140 | + func foo() {} |
| 141 | + } |
| 142 | + let _: any S = S() // expected-error{{'any' has no effect on concrete type 'S'}} |
| 143 | + |
| 144 | + func generic<T: HasAssoc>(t: T) { |
| 145 | + let _: any T = t // expected-error{{'any' has no effect on type parameter 'T'}} |
| 146 | + let _: any T.Assoc // expected-error {{'any' has no effect on type parameter 'T.Assoc'}} |
| 147 | + } |
| 148 | + |
| 149 | + let _: any ((S) -> Void) = generic // expected-error{{'any' has no effect on concrete type '(S) -> Void'}} |
| 150 | +} |
| 151 | + |
| 152 | +func testRedundantAnyWarning() { |
| 153 | + let _: any Any // expected-warning {{'any' is redundant on type 'Any'}} |
| 154 | + let _: any AnyObject // expected-warning {{'any' is redundant on type 'AnyObject'}} |
| 155 | +} |
| 156 | + |
| 157 | +protocol P1 {} |
| 158 | +protocol P2 {} |
| 159 | +struct ConcreteComposition: P1, P2 {} |
| 160 | + |
| 161 | +func testMetatypes() { |
| 162 | + let _: any P1.Type = ConcreteComposition.self |
| 163 | + let _: any (P1 & P2).Type = ConcreteComposition.self |
| 164 | +} |
0 commit comments