@@ -121,32 +121,47 @@ class A { }
121
121
class B : A { }
122
122
class C : A { }
123
123
124
- /// Check for defaulting the element type to 'Any'.
124
+ /// Check for defaulting the element type to 'Any' / 'Any?' .
125
125
func defaultToAny( i: Int , s: String ) {
126
126
let a1 = [ 1 , " a " , 3.5 ]
127
127
// expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}}
128
128
let _: Int = a1 // expected-error{{value of type '[Any]'}}
129
129
130
130
let a2 : Array = [ 1 , " a " , 3.5 ]
131
131
// expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}}
132
-
133
132
let _: Int = a2 // expected-error{{value of type '[Any]'}}
134
-
135
- let a3 = [ ]
133
+
134
+ let a3 = [ 1 , " a " , nil , 3.5 ]
135
+ // expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}}
136
+ let _: Int = a3 // expected-error{{value of type '[Any?]'}}
137
+
138
+ let a4 : Array = [ 1 , " a " , nil , 3.5 ]
139
+ // expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}}
140
+ let _: Int = a4 // expected-error{{value of type '[Any?]'}}
141
+
142
+ let a5 = [ ]
136
143
// expected-error@-1{{empty collection literal requires an explicit type}}
137
-
138
- let _: Int = a3 // expected-error{{value of type '[Any]'}}
144
+ let _: Int = a5 // expected-error{{value of type '[Any]'}}
139
145
140
146
let _: [ Any ] = [ 1 , " a " , 3.5 ]
141
147
let _: [ Any ] = [ 1 , " a " , [ 3.5 , 3.7 , 3.9 ] ]
142
148
let _: [ Any ] = [ 1 , " a " , [ 3.5 , " b " , 3 ] ]
149
+
150
+ let _: [ Any ? ] = [ 1 , " a " , nil , 3.5 ]
151
+ let _: [ Any ? ] = [ 1 , " a " , nil , [ 3.5 , 3.7 , 3.9 ] ]
152
+ let _: [ Any ? ] = [ 1 , " a " , nil , [ 3.5 , " b " , nil ] ]
143
153
144
- let a4 = [ B ( ) , C ( ) ]
145
- let _: Int = a4 // expected-error{{value of type '[A]'}}
154
+ let a6 = [ B ( ) , C ( ) ]
155
+ let _: Int = a6 // expected-error{{value of type '[A]'}}
146
156
}
147
157
148
158
/// Check handling of 'nil'.
149
- func joinWithNil( s: String ) {
159
+ protocol Proto1 { }
160
+ protocol Proto2 { }
161
+ struct Nilable : ExpressibleByNilLiteral {
162
+ init ( nilLiteral: ( ) ) { }
163
+ }
164
+ func joinWithNil< T> ( s: String , a: Any , t: T , m: T . Type , p: Proto1 & Proto2 , arr: [ Int ] , opt: Int ? , iou: Int ! , n: Nilable ) {
150
165
let a1 = [ s, nil ]
151
166
let _: Int = a1 // expected-error{{value of type '[String?]'}}
152
167
@@ -158,6 +173,72 @@ func joinWithNil(s: String) {
158
173
159
174
let a4 = [ nil , " hello " ]
160
175
let _: Int = a4 // expected-error{{value of type '[String?]'}}
176
+
177
+ let a5 = [ ( s, s) , nil ]
178
+ let _: Int = a5 // expected-error{{value of type '[(String, String)?]'}}
179
+
180
+ let a6 = [ nil , ( s, s) ]
181
+ let _: Int = a6 // expected-error{{value of type '[(String, String)?]'}}
182
+
183
+ let a7 = [ ( " hello " , " world " ) , nil ]
184
+ let _: Int = a7 // expected-error{{value of type '[(String, String)?]'}}
185
+
186
+ let a8 = [ nil , ( " hello " , " world " ) ]
187
+ let _: Int = a8 // expected-error{{value of type '[(String, String)?]'}}
188
+
189
+ let a9 = [ { $0 * 2 } , nil ]
190
+ let _: Int = a9 // expected-error{{value of type '[((Int) -> Int)?]'}}
191
+
192
+ let a10 = [ nil , { $0 * 2 } ]
193
+ let _: Int = a10 // expected-error{{value of type '[((Int) -> Int)?]'}}
194
+
195
+ let a11 = [ a, nil ]
196
+ let _: Int = a11 // expected-error{{value of type '[Any?]'}}
197
+
198
+ let a12 = [ nil , a]
199
+ let _: Int = a12 // expected-error{{value of type '[Any?]'}}
200
+
201
+ let a13 = [ t, nil ]
202
+ let _: Int = a13 // expected-error{{value of type '[T?]'}}
203
+
204
+ let a14 = [ nil , t]
205
+ let _: Int = a14 // expected-error{{value of type '[T?]'}}
206
+
207
+ let a15 = [ m, nil ]
208
+ let _: Int = a15 // expected-error{{value of type '[T.Type?]'}}
209
+
210
+ let a16 = [ nil , m]
211
+ let _: Int = a16 // expected-error{{value of type '[T.Type?]'}}
212
+
213
+ let a17 = [ p, nil ]
214
+ let _: Int = a17 // expected-error{{value of type '[(Proto1 & Proto2)?]'}}
215
+
216
+ let a18 = [ nil , p]
217
+ let _: Int = a18 // expected-error{{value of type '[(Proto1 & Proto2)?]'}}
218
+
219
+ let a19 = [ arr, nil ]
220
+ let _: Int = a19 // expected-error{{value of type '[[Int]?]'}}
221
+
222
+ let a20 = [ nil , arr]
223
+ let _: Int = a20 // expected-error{{value of type '[[Int]?]'}}
224
+
225
+ let a21 = [ opt, nil ]
226
+ let _: Int = a21 // expected-error{{value of type '[Int?]'}}
227
+
228
+ let a22 = [ nil , opt]
229
+ let _: Int = a22 // expected-error{{value of type '[Int?]'}}
230
+
231
+ let a23 = [ iou, nil ]
232
+ let _: Int = a23 // expected-error{{value of type '[Int?]'}}
233
+
234
+ let a24 = [ nil , iou]
235
+ let _: Int = a24 // expected-error{{value of type '[Int?]'}}
236
+
237
+ let a25 = [ n, nil ]
238
+ let _: Int = a25 // expected-error{{value of type '[Nilable]'}}
239
+
240
+ let a26 = [ nil , n]
241
+ let _: Int = a26 // expected-error{{value of type '[Nilable]'}}
161
242
}
162
243
163
244
struct OptionSetLike : ExpressibleByArrayLiteral {
0 commit comments