-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathfunction_builder_diags.swift
637 lines (536 loc) · 16.4 KB
/
function_builder_diags.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// RUN: %target-typecheck-verify-swift -disable-availability-checking
enum Either<T,U> {
case first(T)
case second(U)
}
@_functionBuilder
struct TupleBuilder { // expected-note 2 {{struct 'TupleBuilder' declared here}}
static func buildBlock() -> () { }
static func buildBlock<T1>(_ t1: T1) -> T1 {
return t1
}
static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
return (t1, t2)
}
static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
-> (T1, T2, T3) {
return (t1, t2, t3)
}
static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
-> (T1, T2, T3, T4) {
return (t1, t2, t3, t4)
}
static func buildBlock<T1, T2, T3, T4, T5>(
_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
) -> (T1, T2, T3, T4, T5) {
return (t1, t2, t3, t4, t5)
}
static func buildDo<T>(_ value: T) -> T { return value }
static func buildIf<T>(_ value: T?) -> T? { return value }
static func buildEither<T,U>(first value: T) -> Either<T,U> {
return .first(value)
}
static func buildEither<T,U>(second value: U) -> Either<T,U> {
return .second(value)
}
}
@_functionBuilder
struct TupleBuilderWithoutIf { // expected-note {{struct 'TupleBuilderWithoutIf' declared here}}
static func buildBlock() -> () { }
static func buildBlock<T1>(_ t1: T1) -> T1 {
return t1
}
static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
return (t1, t2)
}
static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
-> (T1, T2, T3) {
return (t1, t2, t3)
}
static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
-> (T1, T2, T3, T4) {
return (t1, t2, t3, t4)
}
static func buildBlock<T1, T2, T3, T4, T5>(
_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
) -> (T1, T2, T3, T4, T5) {
return (t1, t2, t3, t4, t5)
}
static func buildDo<T>(_ value: T) -> T { return value }
}
func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
print(body(cond))
}
func tuplifyWithoutIf<T>(_ cond: Bool, @TupleBuilderWithoutIf body: (Bool) -> T) {
print(body(cond))
}
func testDiags() {
// For loop
tuplify(true) { _ in
17
for c in name {
// expected-error@-1 {{cannot find 'name' in scope}}
}
}
// Declarations
tuplify(true) { _ in
17
let x = 17
let y: Int // expected-error{{closure containing a declaration cannot be used with function builder 'TupleBuilder'}}
x + 25
}
// Statements unsupported by the particular builder.
tuplifyWithoutIf(true) {
if $0 { // expected-error{{closure containing control flow statement cannot be used with function builder 'TupleBuilderWithoutIf'}}
"hello"
}
}
}
struct A { }
struct B { }
func overloadedTuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) -> A { // expected-note {{found this candidate}}
return A()
}
func overloadedTuplify<T>(_ cond: Bool, @TupleBuilderWithoutIf body: (Bool) -> T) -> B { // expected-note {{found this candidate}}
return B()
}
func testOverloading(name: String) {
let a1 = overloadedTuplify(true) { b in
if b {
"Hello, \(name)"
}
}
let _: A = a1
_ = overloadedTuplify(true) { b in // expected-error {{ambiguous use of 'overloadedTuplify(_:body:)'}}
b ? "Hello, \(name)" : "Goodbye"
42
overloadedTuplify(false) {
$0 ? "Hello, \(name)" : "Goodbye"
42
if $0 {
"Hello, \(name)"
}
}
}
}
protocol P {
associatedtype T
}
struct AnyP : P {
typealias T = Any
init<T>(_: T) where T : P {}
}
struct TupleP<U> : P {
typealias T = U
init(_: U) {}
}
@_functionBuilder
struct Builder {
static func buildBlock<S0, S1>(_ stmt1: S0, _ stmt2: S1) // expected-note {{required by static method 'buildBlock' where 'S1' = 'Label<_>.Type'}}
-> TupleP<(S0, S1)> where S0: P, S1: P {
return TupleP((stmt1, stmt2))
}
}
struct G<C> : P where C : P {
typealias T = C
init(@Builder _: () -> C) {}
}
struct Text : P {
typealias T = String
init(_: T) {}
}
struct Label<L> : P where L : P { // expected-note 2 {{'L' declared as parameter to type 'Label'}}
typealias T = L
init(@Builder _: () -> L) {} // expected-note {{'init(_:)' declared here}}
}
func test_51167632() -> some P {
AnyP(G { // expected-error {{type 'Label<_>.Type' cannot conform to 'P'; only concrete types such as structs, enums and classes can conform to protocols}}
Text("hello")
Label // expected-error {{generic parameter 'L' could not be inferred}}
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}} {{10-10=<<#L: P#>>}}
})
}
func test_56221372() -> some P {
AnyP(G {
Text("hello")
Label() // expected-error {{generic parameter 'L' could not be inferred}}
// expected-error@-1 {{missing argument for parameter #1 in call}}
// expected-note@-2 {{explicitly specify the generic arguments to fix this issue}} {{10-10=<<#L: P#>>}}
})
}
struct SR11440 {
typealias ReturnsTuple<T> = () -> (T, T)
subscript<T, U>(@TupleBuilder x: ReturnsTuple<T>) -> (ReturnsTuple<U>) -> Void { //expected-note {{in call to 'subscript(_:)'}}
return { _ in }
}
func foo() {
// This is okay, we apply the function builder for the subscript arg.
self[{
5
5
}]({
(5, 5)
})
// But we shouldn't perform the transform for the argument to the call
// made on the function returned from the subscript.
self[{ // expected-error {{generic parameter 'U' could not be inferred}}
5
5
}]({
5
5
})
}
}
func acceptInt(_: Int, _: () -> Void) { }
// SR-11350 crash due to improper recontextualization.
func erroneousSR11350(x: Int) {
tuplify(true) { b in
17
x + 25
Optional(tuplify(false) { b in
if b {
acceptInt(0) { }
}
}).domap(0) // expected-error{{value of type '()?' has no member 'domap'}}
}
}
func extraArg() {
tuplify(true) { _ in
1
2
3
4
5
6 // expected-error {{extra argument in call}}
}
}
// rdar://problem/53209000 - use of #warning and #error
tuplify(true) { x in
1
#error("boom") // expected-error{{boom}}
"hello"
#warning("oops") // expected-warning{{oops}}
3.14159
}
struct MyTuplifiedStruct {
var condition: Bool
@TupleBuilder var computed: some Any { // expected-note{{remove the attribute to explicitly disable the function builder}}{{3-17=}}
if condition {
return 17 // expected-warning{{application of function builder 'TupleBuilder' disabled by explicit 'return' statement}}
// expected-note@-1{{remove 'return' statements to apply the function builder}}{{7-14=}}{{12-19=}}
} else {
return 42
}
}
}
// Check that we're performing syntactic use diagnostics.
func acceptMetatype<T>(_: T.Type) -> Bool { true }
func syntacticUses<T>(_: T) {
tuplify(true) { x in
if x && acceptMetatype(T) { // expected-error{{expected member name or constructor call after type name}}
// expected-note@-1{{use '.self' to reference the type object}}
acceptMetatype(T) // expected-error{{expected member name or constructor call after type name}}
// expected-note@-1{{use '.self' to reference the type object}}
}
}
}
// Check custom diagnostics within "if" conditions.
struct HasProperty {
var property: Bool = false
}
func checkConditions(cond: Bool) {
var x = HasProperty()
tuplify(cond) { value in
if x.property = value { // expected-error{{use of '=' in a boolean context, did you mean '=='?}}
"matched it"
}
}
}
// Check that a closure with a single "return" works with function builders.
func checkSingleReturn(cond: Bool) {
tuplify(cond) { value in
return (value, 17)
}
tuplify(cond) { value in
(value, 17)
}
tuplify(cond) {
($0, 17)
}
}
// rdar://problem/59116520
func checkImplicitSelfInClosure() {
@_functionBuilder
struct Builder {
static func buildBlock(_ children: String...) -> Element { Element() }
}
struct Element {
static func nonEscapingClosure(@Builder closure: (() -> Element)) {}
static func escapingClosure(@Builder closure: @escaping (() -> Element)) {}
}
class C {
let identifier: String = ""
func testImplicitSelf() {
Element.nonEscapingClosure {
identifier // okay
}
Element.escapingClosure { // expected-note {{capture 'self' explicitly to enable implicit 'self' in this closure}}
identifier // expected-error {{reference to property 'identifier' in closure requires explicit use of 'self' to make capture semantics explicit}}
// expected-note@-1 {{reference 'self.' explicitly}}
}
}
}
}
// rdar://problem/59239224 - crash because some nodes don't have type
// information during solution application.
struct X<T> {
init(_: T) { }
}
@TupleBuilder func foo(cond: Bool) -> some Any {
if cond {
tuplify(cond) { x in
X(x)
}
}
}
// switch statements don't allow fallthrough
enum E {
case a
case b(Int, String?)
}
func testSwitch(e: E) {
tuplify(true) { c in
"testSwitch"
switch e {
case .a:
"a"
case .b(let i, let s?):
i * 2
s + "!"
fallthrough // expected-error{{closure containing control flow statement cannot be used with function builder 'TupleBuilder'}}
case .b(let i, nil):
"just \(i)"
}
}
}
// Ensure that we don't back-propagate constraints to the subject
// expression. This is a potential avenue for future exploration, but
// is currently not supported by switch statements outside of function
// builders. It's better to be consistent for now.
enum E2 {
case b(Int, String?) // expected-note{{'b' declared here}}
}
func getSomeEnumOverloaded(_: Double) -> E { return .a }
func getSomeEnumOverloaded(_: Int) -> E2 { return .b(0, nil) }
func testOverloadedSwitch() {
tuplify(true) { c in
// FIXME: Bad source location.
switch getSomeEnumOverloaded(17) { // expected-error{{type 'E2' has no member 'a'; did you mean 'b'?}}
case .a:
"a"
default:
"default"
}
}
}
// Check exhaustivity.
func testNonExhaustiveSwitch(e: E) {
tuplify(true) { c in
"testSwitch"
switch e { // expected-error{{switch must be exhaustive}}
// expected-note @-1{{add missing case: '.b(_, .none)'}}
case .a:
"a"
case .b(let i, let s?):
i * 2
s + "!"
}
}
}
// rdar://problem/59856491
struct TestConstraintGenerationErrors {
@TupleBuilder var buildTupleFnBody: String {
let a = nil // There is no diagnostic here because next line fails to pre-check, so body is invalid
String(nothing) // expected-error {{cannot find 'nothing' in scope}}
}
@TupleBuilder var nilWithoutContext: String {
let a = nil // expected-error {{'nil' requires a contextual type}}
}
func buildTupleClosure() {
tuplify(true) { _ in
let a = nothing // expected-error {{cannot find 'nothing' in scope}}
String(nothing) // expected-error {{cannot find 'nothing' in scope}}
}
}
}
// Check @unknown
func testUnknownInSwitchSwitch(e: E) {
tuplify(true) { c in
"testSwitch"
switch e {
case .b(let i, let s?):
i * 2
s + "!"
default:
"nothing"
@unknown case .a: // expected-error{{'@unknown' is only supported for catch-all cases ("case _")}}
// expected-error@-1{{'case' blocks cannot appear after the 'default' block of a 'switch'}}
"a"
}
}
tuplify(true) { c in
"testSwitch"
switch e {
@unknown case _: // expected-error{{'@unknown' can only be applied to the last case in a switch}}
"a"
default:
"default"
}
}
}
// Check for mutability mismatches when there are multiple case items
// referring to same-named variables.
enum E3 {
case a(Int, String)
case b(String, Int)
case c(String, Int)
}
func testCaseMutabilityMismatches(e: E3) {
tuplify(true) { c in
"testSwitch"
switch e {
case .a(let x, var y),
.b(let y, // expected-error{{'let' pattern binding must match previous 'var' pattern binding}}
var x), // expected-error{{'var' pattern binding must match previous 'let' pattern binding}}
.c(let y, // expected-error{{'let' pattern binding must match previous 'var' pattern binding}}
var x): // expected-error{{'var' pattern binding must match previous 'let' pattern binding}}
x
y += "a"
default:
"default"
}
}
}
// Check for type equivalence among different case variables with the same name.
func testCaseVarTypes(e: E3) {
// FIXME: Terrible diagnostic
tuplify(true) { c in // expected-error{{type of expression is ambiguous without more context}}
"testSwitch"
switch e {
case .a(let x, let y),
.c(let x, let y):
x
y + "a"
}
}
}
// Test for buildFinalResult.
@_functionBuilder
struct WrapperBuilder {
static func buildBlock() -> () { }
static func buildBlock<T1>(_ t1: T1) -> T1 {
return t1
}
static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
return (t1, t2)
}
static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
-> (T1, T2, T3) {
return (t1, t2, t3)
}
static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
-> (T1, T2, T3, T4) {
return (t1, t2, t3, t4)
}
static func buildBlock<T1, T2, T3, T4, T5>(
_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
) -> (T1, T2, T3, T4, T5) {
return (t1, t2, t3, t4, t5)
}
static func buildDo<T>(_ value: T) -> T { return value }
static func buildIf<T>(_ value: T?) -> T? { return value }
static func buildEither<T,U>(first value: T) -> Either<T,U> {
return .first(value)
}
static func buildEither<T,U>(second value: U) -> Either<T,U> {
return .second(value)
}
static func buildFinalResult<T>(_ value: T) -> Wrapper<T> {
return Wrapper(value: value)
}
}
struct Wrapper<T> {
var value: T
}
func wrapperify<T>(_ cond: Bool, @WrapperBuilder body: (Bool) -> T) -> T{
return body(cond)
}
func testWrapperBuilder() {
let x = wrapperify(true) { c in
3.14159
"hello"
}
let _: Int = x // expected-error{{cannot convert value of type 'Wrapper<(Double, String)>' to specified type 'Int'}}
}
// rdar://problem/61347993 - empty function builder doesn't compile
func rdar61347993() {
struct Result {}
@_functionBuilder
struct Builder {
static func buildBlock() -> Result {
Result()
}
}
func test_builder<T>(@Builder _: () -> T) {}
test_builder {} // Ok
func test_closure(_: () -> Result) {}
test_closure {} // expected-error {{cannot convert value of type '()' to closure result type 'Result'}}
}
// One-way constraints through parameters.
func wrapperifyInfer<T, U>(_ cond: Bool, @WrapperBuilder body: (U) -> T) -> T {
fatalError("boom")
}
let intValue = 17
wrapperifyInfer(true) { x in // expected-error{{unable to infer type of a closure parameter 'x' in the current context}}
intValue + x
}
struct DoesNotConform {}
struct MyView {
@TupleBuilder var value: some P { // expected-error {{return type of property 'value' requires that 'DoesNotConform' conform to 'P'}}
// expected-note@-1 {{opaque return type declared here}}
DoesNotConform()
}
@TupleBuilder func test() -> some P { // expected-error {{return type of instance method 'test()' requires that 'DoesNotConform' conform to 'P'}}
// expected-note@-1 {{opaque return type declared here}}
DoesNotConform()
}
@TupleBuilder var emptySwitch: some P {
switch Optional.some(1) { // expected-error {{'switch' statement body must have at least one 'case' or 'default' block; do you want to add a default case?}}
}
}
@TupleBuilder var invalidSwitchOne: some P {
switch Optional.some(1) {
case . // expected-error {{expected ':' after 'case'}}
} // expected-error {{expected identifier after '.' expression}}
}
@TupleBuilder var invalidSwitchMultiple: some P {
switch Optional.some(1) {
case .none: // expected-error {{'case' label in a 'switch' should have at least one executable statement}}
case . // expected-error {{expected ':' after 'case'}}
} // expected-error {{expected identifier after '.' expression}}
}
@TupleBuilder var invalidConversion: Int { // expected-error {{cannot convert value of type 'String' to specified type 'Int'}}
""
}
}
// Make sure throwing function builder closures are implied.
enum MyError: Error {
case boom
}
do {
tuplify(true) { c in // expected-error{{invalid conversion from throwing function of type '(Bool) throws -> String' to non-throwing function type '(Bool) -> String'}}
"testThrow"
throw MyError.boom
}
}