-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCollectionDiagnostics.swift
123 lines (98 loc) · 4.32 KB
/
CollectionDiagnostics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// RUN: %target-parse-verify-swift
import StdlibUnittest
import StdlibCollectionUnittest
//
// Check that Collection.SubSequence is constrained to Collection.
//
// expected-error@+3 {{type 'CollectionWithBadSubSequence' does not conform to protocol 'Collection'}}
// expected-error@+2 {{type 'CollectionWithBadSubSequence' does not conform to protocol 'IndexableBase'}}
// expected-error@+1 {{type 'CollectionWithBadSubSequence' does not conform to protocol 'Sequence'}}
struct CollectionWithBadSubSequence : Collection {
var startIndex: MinimalIndex {
fatalError("unreachable")
}
var endIndex: MinimalIndex {
fatalError("unreachable")
}
subscript(i: MinimalIndex) -> OpaqueValue<Int> {
fatalError("unreachable")
}
// expected-note@+3 {{possibly intended match 'SubSequence' (aka 'OpaqueValue<Int8>') does not conform to 'IndexableBase'}}
// expected-note@+2 {{possibly intended match}}
// expected-note@+1 {{possibly intended match}}
typealias SubSequence = OpaqueValue<Int8>
}
func useCollectionTypeSubSequenceIndex<
C : Collection
where
C.SubSequence.Index == C.Index
>(_ c: C) {}
func useCollectionTypeSubSequenceGeneratorElement<
C : Collection
where
C.SubSequence.Iterator.Element == C.Iterator.Element
>(_ c: C) {}
func sortResultIgnored<
S : Sequence, MC : MutableCollection
where
S.Iterator.Element : Comparable,
MC.Iterator.Element : Comparable
>(
_ sequence: S,
mutableCollection: MC,
array: [Int]
) {
var sequence = sequence // expected-warning {{variable 'sequence' was never mutated; consider changing to 'let' constant}}
var mutableCollection = mutableCollection // expected-warning {{variable 'mutableCollection' was never mutated; consider changing to 'let' constant}}
var array = array // expected-warning {{variable 'array' was never mutated; consider changing to 'let' constant}}
sequence.sorted() // expected-warning {{result of call to 'sorted()' is unused}}
sequence.sorted { $0 < $1 } // expected-warning {{result of call to 'sorted(isOrderedBefore:)' is unused}}
mutableCollection.sorted() // expected-warning {{result of call to 'sorted()' is unused}}
mutableCollection.sorted { $0 < $1 } // expected-warning {{result of call to 'sorted(isOrderedBefore:)' is unused}}
array.sorted() // expected-warning {{result of call to 'sorted()' is unused}}
array.sorted { $0 < $1 } // expected-warning {{result of call to 'sorted(isOrderedBefore:)' is unused}}
}
struct GoodIndexable : Indexable {
func index(after i: Int) -> Int { return i + 1 }
var startIndex: Int { return 0 }
var endIndex: Int { return 0 }
subscript(pos: Int) -> Int { return 0 }
subscript(bounds: Range<Int>) -> [Int] { return [] }
}
// expected-error@+1 {{type 'BadIndexable1' does not conform to protocol 'IndexableBase'}}
struct BadIndexable1 : Indexable {
func index(after i: Int) -> Int { return i + 1 }
var startIndex: Int { return 0 }
var endIndex: Int { return 0 }
subscript(pos: Int) -> Int { return 0 }
// Missing 'subscript(_:) -> SubSequence'.
}
// expected-error@+1 {{type 'BadIndexable2' does not conform to protocol 'IndexableBase'}}
struct BadIndexable2 : Indexable {
var startIndex: Int { return 0 }
var endIndex: Int { return 0 }
subscript(pos: Int) -> Int { return 0 }
subscript(bounds: Range<Int>) -> [Int] { return [] }
// Missing index(after:) -> Int
}
struct GoodBidirectionalIndexable1 : BidirectionalIndexable {
var startIndex: Int { return 0 }
var endIndex: Int { return 0 }
func index(after i: Int) -> Int { return i + 1 }
func index(before i: Int) -> Int { return i - 1 }
subscript(pos: Int) -> Int { return 0 }
subscript(bounds: Range<Int>) -> [Int] { return [] }
}
// We'd like to see: {{type 'BadBidirectionalIndexable' does not conform to protocol 'BidirectionalIndexable'}}
// But the compiler doesn't generate that error.
struct BadBidirectionalIndexable : BidirectionalIndexable {
var startIndex: Int { return 0 }
var endIndex: Int { return 0 }
subscript(pos: Int) -> Int { return 0 }
subscript(bounds: Range<Int>) -> [Int] { return [] }
// This is a poor error message; it would be better to get a message
// that index(before:) was missing.
//
// expected-error@+1 {{'index(after:)' has different argument names from those required by protocol 'BidirectionalIndexable' ('index(before:)'}}
func index(after i: Int) -> Int { return 0 }
}