You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[TypeChecker] Require a coercion if result of protocol member access would loose information
Accessing members on the protocol could result in existential opening and subsequence
result erasure, which requires explicit coercion if there is any loss of generic requirements.
// expected-note@-1 {{inferred result type '(x: Int, y: any P)' requires explicit coercion due to loss of generic requirements}} {{241:20-20=as (x: Int, y: any P)}}
236
+
// expected-note@-1 {{inferred result type '(x: Int, y: any P)' requires explicit coercion due to loss of generic requirements}} {{251:20-20=as (x: Int, y: any P)}}
227
237
func overloaded<T:P>(_:T)->Int{42}
228
238
// expected-note@-1 {{candidate requires that 'any B' conform to 'P' (requirement specified as 'T' : 'P')}}
229
239
@@ -240,7 +250,37 @@ func testExplicitCoercionRequirement(v: any B) {
240
250
241
251
_ =overloaded(v) // expected-error {{no exact matches in call to local function 'overloaded'}}
242
252
253
+
func acceptsAny<T>(_:T){}
254
+
255
+
acceptsAny(getC(v)) // expected-error {{inferred result type 'any P' requires explicit coercion due to loss of generic requirements}} {{21-21=as any P}}
256
+
acceptsAny(getC(v)asanyP) // Ok
257
+
258
+
acceptsAny(getComplex(v)) // expected-error {{inferred result type '([(x: (a: any P, b: Int), y: Int)], [Int : any P])' requires explicit coercion due to loss of generic requirements}} {{27-27=as ([(x: (a: any P, b: Int), y: Int)], [Int : any P])}}
_ =getAssocNoRequirements(v) // Ok, `D` doesn't have any requirements
264
+
265
+
// Test existential opening from protocol extension access
266
+
_ = v.getC() // expected-error {{inferred result type 'any P' requires explicit coercion due to loss of generic requirements}} {{13-13=as any P}}
267
+
_ = v.getC()asanyP // Ok
268
+
269
+
_ = v.testVar // expected-error {{inferred result type '(Int, [any P])' requires explicit coercion due to loss of generic requirements}} {{16-16=as (Int, [any P])}}
270
+
_ = v.testVar as(Int,[anyP])
271
+
272
+
func getE<T:D>(_:T)->T.E{fatalError()}
273
+
274
+
_ =getE(otherV) // Ok `E` doesn't have a `where` clause
275
+
276
+
func getSelf<T:B>(_:T)->T{fatalError()} // expected-note {{found this candidate}}
277
+
func getSelf<T:D>(_:T)->T{fatalError()} // expected-note {{found this candidate}}
278
+
279
+
_ =getSelf(v) // expected-error {{inferred result type 'any B' requires explicit coercion due to loss of generic requirements}} {{17-17=as any B}}
280
+
_ =getSelf(v)asanyB // Ok
281
+
_ =getSelf(otherV)asanyB&D // expected-error {{ambiguous use of 'getSelf'}}
282
+
283
+
func getBDSelf<T:D>(_:T)->T{fatalError()}
284
+
_ =getBDSelf(otherV) // expected-error {{inferred result type 'any B & D' requires explicit coercion due to loss of generic requirements}} {{24-24=as any B & D}}
Copy file name to clipboardExpand all lines: test/decl/protocol/existential_member_accesses_self_assoctype.swift
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -786,7 +786,8 @@ do {
786
786
_ = arg.method3 // expected-error {{member 'method3' cannot be used on value of type 'any ConcreteAssocTypes'; consider using a generic constraint instead}}
787
787
_ = arg.property1 // expected-error {{member 'property1' cannot be used on value of type 'any ConcreteAssocTypes'; consider using a generic constraint instead}}
788
788
// Covariant 'Self' erasure works in conjunction with concrete associated types.
789
-
let _:(Bool,anyConcreteAssocTypes)= arg.property2 // ok
789
+
let _:(Bool,anyConcreteAssocTypes)= arg.property2 // expected-error {{inferred result type '(Bool, any ConcreteAssocTypes)' requires explicit coercion due to loss of generic requirements}} {{58-58=as (Bool, any ConcreteAssocTypes)}}
790
+
let _:(Bool,anyConcreteAssocTypes)= arg.property2 as(Bool,anyConcreteAssocTypes) // Ok
790
791
_ = arg.property3 // expected-error {{member 'property3' cannot be used on value of type 'any ConcreteAssocTypes'; consider using a generic constraint instead}}
791
792
_ =arg[subscript1:false] // expected-error {{member 'subscript' cannot be used on value of type 'any ConcreteAssocTypes'; consider using a generic constraint instead}}
792
793
_ =arg[subscript2:false] // expected-error {{member 'subscript' cannot be used on value of type 'any ConcreteAssocTypes'; consider using a generic constraint instead}}
0 commit comments