File: diag_dictionary_keys_duplicated.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (246 lines) | stat: -rw-r--r-- 7,773 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// RUN: %target-typecheck-verify-swift

class CustomDict: ExpressibleByDictionaryLiteral {
  typealias Key = String
  typealias Value = String
  
  required init(dictionaryLiteral elements: (String, String)...) {}
}
func fDict(_ d: [String: String]) {}
func fCustomDict(_ d: CustomDict) {}
func fDictGeneric<D>(_ d: D) where D: ExpressibleByDictionaryLiteral {}

// Duplicated 
let _ = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "A": "B" // expected-note{{duplicate key declared here}} {{3-12=}} {{16:11-12=}}
]

let _: [String: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "A": "B" // expected-note{{duplicate key declared here}} {{3-12=}} {{22:11-12=}}
]

let _: [String: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-15=}}{{15-16=}}
  (("A")): "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "A": "B" // expected-note{{duplicate key declared here}} {{3-12=}} {{28:15-16=}}
]

let _: [String: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "B": "B",
  "C": "C", 
  "A": "D" // expected-note{{duplicate key declared here}} {{3-12=}} {{36:11-12=}}
]

// Number literal key
let _: [Int: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-9=}} {{9-10=}}
  1: "1", // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for integer literal key '1'}}
  2: "2",
  1: "3" // expected-note{{duplicate key declared here}} {{3-10=}} {{44:9-10=}}
]

let _: [Double: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-12=}} {{12-13=}}
  1.01: "1", // expected-warning{{dictionary literal of type '[Double : String]' has duplicate entries for floating-point literal key '1.01'}}
  2: "2",
  1.01: "3" // expected-note{{duplicate key declared here}} {{3-13=}} {{51:9-10=}}
]

// Boolean literal
let _: [Bool: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-12=}} {{12-13=}}
  true: "1", // expected-warning{{dictionary literal of type '[Bool : String]' has duplicate entries for boolean literal key 'true'}}
  false: "2",
  true: "3" // expected-note{{duplicate key declared here}} {{3-13=}} {{59:13-14=}}
]

// nil literal 
let _: [Int?: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  nil: "1", // expected-warning{{dictionary literal of type '[Int? : String]' has duplicate entries for nil literal key}}
  2: "2",
  nil: "3" // expected-note{{duplicate key declared here}} {{3-12=}} {{67:9-10=}}
]

// Nested
let _: [String: [String: String]] = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": [:], // expected-warning{{dictionary literal of type '[String : [String : String]]' has duplicate entries for string literal key 'A'}}
  "B": [:],
  "C": [:], 
  "A": [:] // expected-note{{duplicate key declared here}} {{3-12=}} {{76:11-12=}}
]

let _: [String: [String: String]] = [
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": [:], // expected-warning{{dictionary literal of type '[String : [String : String]]' has duplicate entries for string literal key 'A'}}
  "B": [:],
  "C": [:], 
  "A": ["a": "", "a": ""] // expected-note{{duplicate key declared here}} {{3-27=}} {{84:11-12=}}
  // expected-warning@-1{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'a'}}
  // expected-note@-2{{duplicate key declared here}} {{9-16=}} {{16-17=}}
  // expected-note@-3{{duplicate key declared here}} {{18-25=}} {{16-17=}}
]

// Parent OK, nested duplicated
let _: [String: [String: String]] = [
  "A": [:], 
  "B": [:],
  "C": [:], 
  // expected-note@+1{{duplicate key declared here}} {{9-16=}} {{16-17=}}
  "D": ["a": "", "a": ""] // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'a'}}
  // expected-note@-1{{duplicate key declared here}}{{18-25=}} {{18-25=}} {{16-17=}}
]

// Ok, because custom implementations of ExpressibleByDictionaryLiteral can allow duplicated keys.
let _: CustomDict = [
  "A": "A", 
  "A": "B"
]

fDict([
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "A": "B" // expected-note{{duplicate key declared here}} {{3-12=}} {{109:11-12=}}
])
fCustomDict([
  "A": "A", 
  "A": "B"
])
fDictGeneric([
  // expected-note@+1{{duplicate key declared here}} {{3-11=}} {{11-12=}}
  "A": "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for string literal key 'A'}}
  "A": "B" // expected-note{{duplicate key declared here}} {{3-12=}} {{118:11-12=}}
])

// Magic literals
let _: [String: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-13=}} {{13-14=}}
  #file: "A", // expected-warning{{dictionary literal of type '[String : String]' has duplicate entries for #file literal key}}
  #file: "B" // expected-note{{duplicate key declared here}} {{3-14=}} {{125:13-14=}}
]


// OK
let _ = [
  "A": "A", 
  "B": "B"
]
let _: [String: String] = [
  "A": "A", 
  "B": "B"
]
let _: CustomDict = [
  "A": "A", 
  "B": "B"
]

// Number literal key
let _: [Int: String] = [
  1: "1",
  2: "2",
  3: "3"
]

let _: [Double: String] = [
  1.01: "1",
  2: "2",
  1.02: "3"
]

// Boolean literal
let _: [Bool: String] = [
  true: "1",
  false: "2"
]

// nil literal 
let _: [Int?: String] = [
  nil: "1",
  2: "2"
]

// Key as the same variable is OK, we only diagnose literals
let a = "A"

let _: [String: String] = [
  a: "A",
  "B": "B",
  "a": "C", 
  a: "D"
]

let _: [String: [String: String]] = [
  "A": [:],
  "B": [:],
  "C": [:], 
  "D": ["a": "", "b": ""]
]

fDict([
  "A": "A", 
  "B": "B"
])
fCustomDict([
  "A": "A", 
  "B": "B"
])
fDictGeneric([
  "A": "A", 
  "B": "B"
])

func magicFn() {
  let _: [String: String] = [
    #file: "A", 
    #function: "B"
  ]
}

// Interpolated literals
let _: [String: String] = [
  "\(a)": "A", 
  "\(a)": "B",
  "\(1)": "C"
]

// https://github.com/apple/swift/issues/60873
let _: [Int: String] = [
  #line: "A",
  #line: "B"
]

let _: [Int: String] = [#line: "A", #line: "B"] // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for #line literal key}}
// expected-note@-1{{duplicate key declared here}} {{25-35=}} {{35-36=}}
// expected-note@-2{{duplicate key declared here}} {{37-47=}} {{35-36=}}

let _: [Int: String] = [#column: "A", #column: "B"] // OK

let _: [Int: String] = [
  // expected-note@+1{{duplicate key declared here}} {{3-15=}} {{15-16=}} 
  #column: "A", // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for #column literal key}}
  #column: "B"  // expected-note{{duplicate key declared here}} {{3-16=}} {{227:15-16=}}
]

// https://github.com/apple/swift/issues/62117
_ = [
  -1: "",
  1: "",
]

_ = [
  -1.0: "",
  1.0: "",
]

_ = [
  // expected-note@+1{{duplicate key declared}} {{3-9=}} {{9-10=}} 
  -1: "", // expected-warning{{dictionary literal of type '[Int : String]' has duplicate entries for integer literal key '-1'}}
  -1: "", // expected-note{{duplicate key declared}} {{3-9=}} {{9-10=}}
]