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 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
|
// RUN: %target-typecheck-verify-swift -disable-availability-checking
enum Either<T,U> {
case first(T)
case second(U)
}
// expected-note @+2 {{add 'buildArray(_:)' to the result builder 'TupleBuilder' to add support for 'for'..'in' loops}}
// expected-note @+1 2 {{struct 'TupleBuilder' declared here}}
@resultBuilder struct TupleBuilder {
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)
}
}
@resultBuilder
struct TupleBuilderWithoutIf { // expected-note 3{{struct 'TupleBuilderWithoutIf' declared here}}
// expected-note@-1{{add 'buildOptional(_:)' to the result builder 'TupleBuilderWithoutIf' to add support for 'if' statements without an 'else'}}
// expected-note@-2{{add 'buildEither(first:)' and 'buildEither(second:)' to the result builder 'TupleBuilderWithoutIf' to add support for 'if'-'else' and 'switch'}}
// expected-note@-3{{add 'buildArray(_:)' to the result builder 'TupleBuilderWithoutIf' to add support for 'for'..'in' loops}}
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) { // expected-note {{'tuplify(_:body:)' declared here}}
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 {{closure containing control flow statement cannot be used with result builder 'TupleBuilder'}}
// expected-error@-1 {{cannot find 'name' in scope}}
}
}
// Declarations
tuplify(true) { _ in
17
let x = 17
let y: Int // Ok
x + 25
}
tuplify(true) { _ in
17
let y: Int, z: String // Ok
y + 25
}
tuplify(true) { _ in
0
let x: Int = 0, y: String = "" // Multiple initialized pattern bindings are okay
x + 1
y
}
// Statements unsupported by the particular builder.
tuplifyWithoutIf(true) {
if $0 { // expected-error{{closure containing control flow statement cannot be used with result builder 'TupleBuilderWithoutIf'}}
"hello"
}
}
tuplifyWithoutIf(true) {
if $0 { // expected-error{{closure containing control flow statement cannot be used with result builder 'TupleBuilderWithoutIf'}}
"hello"
} else {
}
}
tuplifyWithoutIf(true) { a in
for x in 0..<100 { // expected-error{{closure containing control flow statement cannot be used with result builder 'TupleBuilderWithoutIf'}}
x
}
}
}
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)"
}
}
_ = overloadedTuplify(true) { cond in
if cond {
print(\"hello") // expected-error {{invalid component of Swift key path}}
}
}
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) {}
}
@resultBuilder
struct Builder {
static func buildBlock<S0, S1>(_ stmt1: S0, _ stmt2: S1)
-> 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 {
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#>>}}
})
}
// https://github.com/apple/swift/issues/53841
struct S_53841 {
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 result 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) { }
// https://github.com/apple/swift/issues/53751
// Crash due to improper recontextualization.
func erroneous_53751(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 result builder}}{{3-17=}}
if condition {
// expected-note@+1 {{remove 'return' statements to apply the result builder}}{{7-14=}}{{+2:12-19=}}
return 17 // expected-warning{{application of result builder 'TupleBuilder' disabled by explicit 'return' statement}}
} 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 result 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() {
@resultBuilder
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 result 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
switch getSomeEnumOverloaded(17) {
case .a: // expected-error{{type 'E2' has no member 'a'; did you mean 'b'?}}
"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) {
tuplify(true) { c in
"testSwitch"
switch e {
case .a(let x, let y),
.c(let x, let y):
// expected-error@-1 {{pattern variable bound to type 'String', expected type 'Int'}}
// expected-error@-2 {{pattern variable bound to type 'Int', expected type 'String'}}
x
y + "a"
}
}
}
// Test for buildFinalResult.
@resultBuilder
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 result builder doesn't compile
func rdar61347993() {
struct Result {}
@resultBuilder
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 // Ok
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; 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' must have at least one executable statement}}
case . // expected-error {{expected ':' after 'case'}}
} // expected-error {{expected identifier after '.' expression}}
}
@TupleBuilder var invalidCaseWithoutDot: some P {
switch Optional.some(1) {
case none: 42 // expected-error {{cannot find 'none' in scope}}
case .some(let x):
0
}
}
@TupleBuilder var invalidConversion: Int { // expected-error {{cannot convert return expression of type 'String' to return type 'Int'}}
""
}
}
// Make sure throwing result 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
}
}
struct TuplifiedStructWithInvalidClosure {
var condition: Bool?
@TupleBuilder var unknownParameter: some Any {
if let cond = condition {
let _ = { (arg: UnknownType) in // expected-error {{cannot find type 'UnknownType' in scope}}
}
42
} else {
0
}
}
@TupleBuilder var unknownResult: some Any {
if let _ = condition {
let _ = { () -> UnknownType in // expected-error {{cannot find type 'UnknownType' in scope}}
}
42
} else {
0
}
}
@TupleBuilder var multipleLevelsDeep: some Any {
if let _ = condition {
switch MyError.boom {
case .boom:
let _ = { () -> UnknownType in // expected-error {{cannot find type 'UnknownType' in scope}}
}
}
42
} else {
0
}
}
@TupleBuilder var errorsDiagnosedByParser: some Any {
if let _ = condition {
tuplify { _ in
self. // expected-error {{expected member name following '.'}}
}
42
}
}
@TupleBuilder var nestedErrorsDiagnosedByParser: some Any {
tuplify(true) { _ in
tuplify { _ in // expected-error {{missing argument for parameter #1 in call}}
self. // expected-error {{expected member name following '.'}}
}
42
}
}
}
// rdar://65667992 - invalid case in enum causes fallback diagnostic
func test_rdar65667992() {
@resultBuilder
struct Builder {
static func buildBlock<T>(_ t: T) -> T { t }
static func buildEither<T>(first: T) -> T { first }
static func buildEither<T>(second: T) -> T { second }
}
struct S {}
enum E {
case set(v: Int, choices: [Int])
case notSet(choices: [Int])
}
struct MyView {
var entry: E
@Builder var body: S {
switch entry {
case .set(_, _): S()
case .unset(_): S() // expected-error {{type 'E' has no member 'unset'}}
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
default: S()
}
}
}
}
func test_weak_with_nonoptional_type() {
class X {
func test() -> Int { 0 }
}
tuplify(true) { c in
weak var x: X = X() // expected-error {{'weak' variable should have optional type 'X?'}}
if let x = x {
x.test()
}
42
}
}
// rdar://80941497 - compiler fails to produce diagnostic when referencing missing member in optional context
func test_missing_member_in_optional_context() {
struct Test {
}
var test: Test? = nil
tuplify(true) { c in
if let prop = test?.prop { // expected-error {{value of type 'Test' has no member 'prop'}}
0
}
}
tuplify(true) { c in
if let method = test?.method() { // expected-error {{value of type 'Test' has no member 'method'}}
1
}
}
}
func test_redeclarations() {
tuplify(true) { c in
let foo = 0 // expected-note {{'foo' previously declared here}}
let foo = foo // expected-error {{invalid redeclaration of 'foo'}}
}
tuplify(true) { c in
let (foo, foo) = (5, 6) // expected-error {{invalid redeclaration of 'foo'}} expected-note {{'foo' previously declared here}}
}
}
func test_rdar89742267() {
@resultBuilder
struct Builder {
static func buildBlock<T>(_ t: T) -> T { t }
static func buildEither<T>(first: T) -> T { first }
static func buildEither<T>(second: T) -> T { second }
}
struct S {}
enum Hey {
case listen
}
struct MyView {
var entry: Hey
@Builder var body: S {
switch entry {
case .listen: S()
case nil: S() // expected-warning {{type 'Hey' is not optional, value can never be nil; this is an error in the Swift 6 language mode}}
default: S()
}
}
}
}
// https://github.com/apple/swift/issues/59390
func test_invalid_result_is_diagnosed() {
@resultBuilder
struct MyBuilder {
static func buildBlock<T1>(_ t1: T1) -> T1 {
return t1
}
}
struct S<T> {}
@MyBuilder
func test() -> S<String> { // expected-error {{conflicting arguments to generic parameter 'T1' ('S<Int>' vs. 'S<String>')}}
S<Int>()
}
}
func test_associated_values_dont_block_solver_when_unresolved() {
@resultBuilder
struct Builder {
static func buildBlock<T>(_ t: T) -> T { t }
static func buildEither<T>(first: T) -> T { first }
static func buildEither<T>(second: T) -> T { second }
}
struct Value {
enum Kind {
case a(String)
case b
}
var kind: Kind
}
struct Container {
var prop: Value? = nil
}
struct TestWithAny {
var container: Container
@Builder var body: String {
let v = container.prop.kind // expected-error {{value of optional type 'Value?' must be unwrapped to refer to member 'kind' of wrapped base type 'Value'}}
// expected-note@-1 {{chain the optional using '?' to access member 'kind' only for non-'nil' base values}}
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
switch v.kind {
case .a(_): "a"
case .b: "b"
}
}
@Builder var switchError: String {
let v = container.prop!.kind
switch v.kind { // expected-error {{value of type 'Value.Kind' has no member 'kind'}}
case .a(_): "a"
case .b: "b"
}
}
}
}
func test_dependent_member_with_unresolved_base_type() {
struct Wrapper<A, T: P> : P {
}
@resultBuilder
struct Builder<A> {
static func buildBlock(_ value: some P) -> some P {
value
}
}
func test<A, U: P>(data: KeyPath<A, [(Int, U.T)]>, // expected-note {{in call to function 'test(data:_:)'}}
@Builder<U.T> _: () -> U) -> Wrapper<A, U> { fatalError() }
struct Value : P {
typealias T = (Int, String)
}
struct Test : P {
struct T {
var values: [(Int, Value.T)]
}
var v: some P {
test(data: \T.values) { // expected-error {{generic parameter 'U' could not be inferred}}
Value()
}
}
}
}
// rdar://89880662 - incorrect error about unsupported control flow statement
func test_impact_of_control_flow_fix() {
@resultBuilder
struct BuilderA {
static func buildOptional<T>(_ value: T?) -> T? { return value }
static func buildBlock<T>(_ v1: T) -> T { v1 }
static func buildBlock<T, U>(_ v1: T, _ v2: U) -> (T, U) { (v1, v2) }
}
@resultBuilder
struct BuilderB {
static func buildBlock<T>(_ v1: T) -> T { v1 }
static func buildBlock<T, U>(_ v1: T, _ v2: U) -> (T, U) { (v1, v2) }
}
func fn<T>(@BuilderA _: () -> T) {}
func fn(@BuilderB _: () -> (Int?, Void)) {}
func test(_: Int) {}
fn {
if true {
0
}
test("") // expected-error {{cannot convert value of type 'String' to expected argument type 'Int'}}
}
}
protocol Q {}
func test_requirement_failure_in_buildBlock() {
struct A : P { typealias T = Int }
struct B : Q { typealias T = String }
struct Result<T> : P { typealias T = Int }
@resultBuilder
struct BuilderA {
static func buildBlock<T0: P, T1: P>(_: T0, _: T1) -> Result<(T0, T1)> { fatalError() }
// expected-note@-1 {{candidate requires that 'B' conform to 'P' (requirement specified as 'T1' : 'P')}}
}
@resultBuilder
struct BuilderB {
static func buildBlock<U0: Q, U1: Q>(_ v: U0, _: U1) -> some Q { v }
// expected-note@-1 {{candidate requires that 'A' conform to 'Q' (requirement specified as 'U0' : 'Q')}}
}
struct Test {
func fn<T: P>(@BuilderA _: () -> T) {}
func fn<T: Q>(@BuilderB _: () -> T) {}
func test() {
fn { // expected-error {{no exact matches in reference to static method 'buildBlock'}}
A()
B()
}
}
}
}
func test_partially_resolved_closure_params() {
struct S<T> {
var a: String = ""
}
func test<T>(@TupleBuilder _: (S<T>) -> T) { // expected-note {{in call to function 'test'}}
}
test { // expected-error {{generic parameter 'T' could not be inferred}}
$0.a
42
}
}
|