Skip to content

Commit 15bce77

Browse files
authored
Merge pull request #71021 from bnbarham/unnamed-warn-to-6
[Parse] Update error if closure has unnamed parameter to warning
2 parents a5ebc21 + 8bb1ac9 commit 15bce77

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,27 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
416416
// Mark current parameter type as invalid so it is possible
417417
// to diagnose it as destructuring of the closure parameter list.
418418
param.isPotentiallyDestructured = true;
419-
// Unnamed parameters must be written as "_: Type".
420-
diagnose(typeStartLoc, diag::parameter_unnamed)
421-
.fixItInsert(typeStartLoc, "_: ");
419+
if (!isClosure) {
420+
// Unnamed parameters must be written as "_: Type".
421+
diagnose(typeStartLoc, diag::parameter_unnamed)
422+
.fixItInsert(typeStartLoc, "_: ");
423+
} else {
424+
// Unnamed parameters were accidentally possibly accepted after
425+
// SE-110 depending on the kind of declaration. We now need to
426+
// warn about the misuse of this syntax and offer to
427+
// fix it.
428+
// An exception to this rule is when the type is declared with type sugar
429+
// Reference: https://github.com/apple/swift/issues/54133
430+
if (isa<OptionalTypeRepr>(param.Type)
431+
|| isa<ImplicitlyUnwrappedOptionalTypeRepr>(param.Type)) {
432+
diagnose(typeStartLoc, diag::parameter_unnamed)
433+
.fixItInsert(typeStartLoc, "_: ");
434+
} else {
435+
diagnose(typeStartLoc, diag::parameter_unnamed)
436+
.warnUntilSwiftVersion(6)
437+
.fixItInsert(typeStartLoc, "_: ");
438+
}
439+
}
422440
}
423441
} else {
424442
// Otherwise, we're not sure what is going on, but this doesn't smell

test/Constraints/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ func rdar_59741308() {
10261026
func r60074136() {
10271027
func takesClosure(_ closure: ((Int) -> Void) -> Void) {}
10281028

1029-
takesClosure { ((Int) -> Void) -> Void in // expected-error {{unnamed parameters must be written with the empty name '_'}}
1029+
takesClosure { ((Int) -> Void) -> Void in // expected-warning {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}}
10301030
}
10311031
}
10321032

test/Constraints/tuple_arguments.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ do {
14901490
let tuple = (1, (2, 3))
14911491
[tuple].map { (x, (y, z)) -> Int in x + y + z } // expected-note 2 {{'x' declared here}}
14921492
// expected-error@-1 {{closure tuple parameter does not support destructuring}} {{21-27=arg1}} {{39-39=let (y, z) = arg1; }}
1493-
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{21-21=_: }}
1493+
// expected-warning@-2 {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{21-21=_: }}
14941494
// expected-error@-3 {{cannot find 'y' in scope; did you mean 'x'?}}
14951495
// expected-error@-4 {{cannot find 'z' in scope; did you mean 'x'?}}
14961496
}
@@ -1501,7 +1501,7 @@ r31892961_1.forEach { (k, v) in print(k + v) }
15011501

15021502
let r31892961_2 = [1, 2, 3]
15031503
// expected-error@+2 {{closure tuple parameter does not support destructuring}} {{48-60=arg0}} {{+1:3-3=\n let (index, val) = arg0\n }}
1504-
// expected-error@+1 {{unnamed parameters must be written with the empty name '_'}} {{48-48=_: }}
1504+
// expected-warning@+1 {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{48-48=_: }}
15051505
let _: [Int] = r31892961_2.enumerated().map { ((index, val)) in
15061506
val + 1
15071507
// expected-error@-1 {{cannot find 'val' in scope}}
@@ -1518,13 +1518,13 @@ _ = [r31892961_4].map { x, y in x + y }
15181518
let r31892961_5 = (x: 1, (y: 2, (w: 3, z: 4)))
15191519
[r31892961_5].map { (x: Int, (y: Int, (w: Int, z: Int))) in x + y } // expected-note {{'x' declared here}}
15201520
// expected-error@-1 {{closure tuple parameter does not support destructuring}} {{30-56=arg1}} {{61-61=let (y, (w, z)) = arg1; }}
1521-
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{30-30=_: }}
1521+
// expected-warning@-2 {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{30-30=_: }}
15221522
// expected-error@-3{{cannot find 'y' in scope; did you mean 'x'?}}
15231523

15241524
let r31892961_6 = (x: 1, (y: 2, z: 4))
15251525
[r31892961_6].map { (x: Int, (y: Int, z: Int)) in x + y } // expected-note {{'x' declared here}}
15261526
// expected-error@-1 {{closure tuple parameter does not support destructuring}} {{30-46=arg1}} {{51-51=let (y, z) = arg1; }}
1527-
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{30-30=_: }}
1527+
// expected-warning@-2 {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{30-30=_: }}
15281528
// expected-error@-3{{cannot find 'y' in scope; did you mean 'x'?}}
15291529

15301530
// rdar://problem/32214649 -- these regressed in Swift 4 mode

test/decl/func/functions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ func bogusDestructuring() {
192192
func registerCallback(_ callback: @escaping (Bar?) -> Void) {}
193193
}
194194

195-
Foo().registerCallback { ([Bar]) in } // expected-error {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
196-
Foo().registerCallback { ([String: Bar]) in }// expected-error {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
195+
Foo().registerCallback { ([Bar]) in } // expected-warning {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{29-29=_: }}
196+
Foo().registerCallback { ([String: Bar]) in }// expected-warning {{unnamed parameters must be written with the empty name '_'; this is an error in Swift 6}} {{29-29=_: }}
197197
Foo().registerCallback { (Bar?) in } // expected-error {{unnamed parameters must be written with the empty name '_'}}
198198

199199
}

0 commit comments

Comments
 (0)