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
[Sema] Swift3 compatibility: Fix ambiguous protocol composition production
Fixes: https://bugs.swift.org/browse/SR-2843
'P1 & P2.Type' is mistakingly accepted and parsed as (meatatype (composition P1, P2))
in swift3. Now, we parse it as (composition P1, (metatype P2))
For source compatibility, reconstruct it as Swift3.
Also, this solves inconsistent behavior between type and type-expression in Swift3.
typealias T1 = P1 & P2? // was accepted as '(P1 & P2)?'
let T2 = (P1 & P2?).self // was silently accepted as 'P1' in Swift3.0
// Warning for mitakingly accepted protocol composition production.
17
+
func foo(x:P1&Any&P2.Type?){
18
+
// expected-warning @-1 {{protocol composition with postfix '.Type' is ambiguous and will be rejected in future version of Swift}} {{13-13=(}} {{26-26=)}}
19
+
let _:(P1&P2).Type?= x
20
+
let _:(P1&P2).Type = x!
21
+
let _:Int= x!.p1()
22
+
let _:Int?= x?.p2
23
+
}
24
+
25
+
// Type expression
26
+
func bar()->((P1&P2)?).Type {
27
+
letx=(P1 & P2?).self
28
+
// expected-warning @-1 {{protocol composition with postfix '?' is ambiguous and will be rejected in future version of Swift}} {{12-12=(}} {{19-19=)}}
29
+
return x
30
+
}
31
+
32
+
// Non-ident type at non-last position are rejected anyway.
33
+
typealiasA1=P1.Type&P2 // expected-error {{type 'P1.Type' cannot be used within a protocol composition}}
34
+
35
+
// BEGIN swift4.swift
36
+
37
+
func foo(x:P1&Any&P2.Type?){ // expected-error {{non-protocol type 'P2.Type?' cannot be used within a protocol composition}}
38
+
let _:(P1&P2).Type?= x // expected-error {{cannot convert value of type 'P1' to specified type '(P1 & P2).Type?'}}
39
+
let _:(P1&P2).Type = x! // expected-error {{cannot force unwrap value of non-optional type 'P1'}}
40
+
let _:Int= x!.p1() // expected-error {{cannot force unwrap value of non-optional type 'P1'}}
41
+
let _:Int?= x?.p2 // expected-error {{cannot use optional chaining on non-optional value of type 'P1'}}
42
+
}
43
+
44
+
func bar()->((P1&P2)?).Type {
45
+
letx=(P1 & P2?).self // expected-error {{non-protocol type 'P2?' cannot be used within a protocol composition}}
46
+
return x // expected-error {{cannot convert return expression}}
47
+
}
48
+
49
+
typealiasA1=P1.Type&P2 // expected-error {{type 'P1.Type' cannot be used within a protocol composition}}
0 commit comments