Skip to content

Commit 4ba4da4

Browse files
committed
[Parse] Error if closure has an unnamed parameter
We accepted unnamed closure parameters if the type was an array literal, dictionary literal, tuple or function (because the `[` or `(` starting the type was sufficient to disambiguate the type from the parameter’s name). This was never an accepted syntax and we should disallow it.
1 parent 9b10ab3 commit 4ba4da4

File tree

5 files changed

+10
-29
lines changed

5 files changed

+10
-29
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,8 +1029,6 @@ ERROR(parameter_operator_keyword_argument,none,
10291029

10301030
ERROR(parameter_unnamed,none,
10311031
"unnamed parameters must be written with the empty name '_'", ())
1032-
WARNING(parameter_unnamed_warn,none,
1033-
"unnamed parameters must be written with the empty name '_'", ())
10341032

10351033
ERROR(parameter_curry_syntax_removed,none,
10361034
"cannot have more than one parameter list", ())

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,9 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
404404
// Mark current parameter type as invalid so it is possible
405405
// to diagnose it as destructuring of the closure parameter list.
406406
param.isPotentiallyDestructured = true;
407-
if (!isClosure) {
408-
// Unnamed parameters must be written as "_: Type".
409-
diagnose(typeStartLoc, diag::parameter_unnamed)
410-
.fixItInsert(typeStartLoc, "_: ");
411-
} else {
412-
// Unnamed parameters were accidentally possibly accepted after
413-
// SE-110 depending on the kind of declaration. We now need to
414-
// warn about the misuse of this syntax and offer to
415-
// fix it.
416-
// An exception to this rule is when the type is declared with type sugar
417-
// Reference: https://github.com/apple/swift/issues/54133
418-
if (isa<OptionalTypeRepr>(param.Type)
419-
|| isa<ImplicitlyUnwrappedOptionalTypeRepr>(param.Type)) {
420-
diagnose(typeStartLoc, diag::parameter_unnamed)
421-
.fixItInsert(typeStartLoc, "_: ");
422-
} else {
423-
diagnose(typeStartLoc, diag::parameter_unnamed_warn)
424-
.fixItInsert(typeStartLoc, "_: ");
425-
}
426-
}
407+
// Unnamed parameters must be written as "_: Type".
408+
diagnose(typeStartLoc, diag::parameter_unnamed)
409+
.fixItInsert(typeStartLoc, "_: ");
427410
}
428411
} else {
429412
// 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-warning {{unnamed parameters must be written with the empty name '_'}}
1029+
takesClosure { ((Int) -> Void) -> Void in // expected-error {{unnamed parameters must be written with the empty name '_'}}
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-warning@-2 {{unnamed parameters must be written with the empty name '_'}} {{21-21=_: }}
1493+
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{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-warning@+1 {{unnamed parameters must be written with the empty name '_'}} {{48-48=_: }}
1504+
// expected-error@+1 {{unnamed parameters must be written with the empty name '_'}} {{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-warning@-2 {{unnamed parameters must be written with the empty name '_'}} {{30-30=_: }}
1521+
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{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-warning@-2 {{unnamed parameters must be written with the empty name '_'}} {{30-30=_: }}
1527+
// expected-error@-2 {{unnamed parameters must be written with the empty name '_'}} {{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-warning {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
196-
Foo().registerCallback { ([String: Bar]) in }// expected-warning {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
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=_: }}
197197
Foo().registerCallback { (Bar?) in } // expected-error {{unnamed parameters must be written with the empty name '_'}}
198198

199199
}

0 commit comments

Comments
 (0)