-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathattr_marker_protocol.swift
65 lines (47 loc) · 1.93 KB
/
attr_marker_protocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RUN: %target-typecheck-verify-swift
// Marker protocol definition
@_marker protocol P1 {
func f() // expected-error{{marker protocol 'P1' cannot have any requirements}}
}
@_marker protocol P2 {
associatedtype AT // expected-error{{marker protocol 'P2' cannot have any requirements}}
}
@_marker protocol P3 {
typealias A = Int
}
protocol P4 { } // expected-note{{'P4' declared here}}
@_marker protocol P5: P4 { } // expected-error{{marker protocol 'P5' cannot inherit non-marker protocol 'P4'}}
// Legitimate uses of marker protocols.
extension P3 {
func f() { }
}
extension Int: P3 { }
extension Array: P3 where Element: P3 { } // expected-note{{requirement from conditional conformance of '[Double]' to 'P3'}}
protocol P6: P3 { } // okay
@_marker protocol P7: P3 { } // okay
func genericOk<T: P3>(_: T) { }
func testGenericOk(i: Int, arr: [Int], nope: [Double], p3: P3, p3array: [P3]) {
genericOk(i)
genericOk(arr)
genericOk(nope) // expected-error{{global function 'genericOk' requires that 'Double' conform to 'P3'}}
genericOk(p3)
genericOk(p3array)
}
// Incorrect uses of marker protocols in types.
func testNotOkay(a: Any) {
var mp1: P3 = 17
_ = mp1
mp1 = 17
if let mp2 = a as? P3 { _ = mp2 } // expected-error{{marker protocol 'P3' cannot be used in a conditional cast}}
if let mp3 = a as? AnyObject & P3 { _ = mp3 } // expected-error{{marker protocol 'P3' cannot be used in a conditional cast}}
if a is AnyObject & P3 { } // expected-error{{marker protocol 'P3' cannot be used in a conditional cast}}
func inner(p3: P3) { }
}
@_marker protocol P8 { }
protocol P9: P8 { }
// Implied conditional conformance to P8 is okay because P8 is a marker
// protocol.
extension Array: P9 where Element: P9 { }
protocol P10 { }
extension Array: P10 where Element: P10, Element: P8 { }
// expected-error@-1{{conditional conformance to non-marker protocol 'P10' cannot depend on conformance of 'Element' to non-marker protocol 'P8'}}