Skip to content

Commit fce55a6

Browse files
tapthakerCodaFi
authored andcommitted
[Parse] Provide a diagnostic when a closure parameter is declared with type sugar (#28315)
* diagnostic when a closure parameter is declared with type sugar * Use a test that was already commmited for SR-11724 i * Use isa<T> instead of asking for the kind directly * Fix nit: Remove a whitespace
1 parent 49993c5 commit fce55a6

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ static bool startsParameterName(Parser &parser, bool isClosure) {
165165
if (nextTok.is(tok::colon) || nextTok.canBeArgumentLabel())
166166
return true;
167167

168+
if (parser.isOptionalToken(nextTok)
169+
|| parser.isImplicitlyUnwrappedOptionalToken(nextTok))
170+
return false;
171+
168172
// The identifier could be a name or it could be a type. In a closure, we
169173
// assume it's a name, because the type can be inferred. Elsewhere, we
170174
// assume it's a type.
@@ -375,8 +379,16 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
375379
// SE-110 depending on the kind of declaration. We now need to
376380
// warn about the misuse of this syntax and offer to
377381
// fix it.
378-
diagnose(typeStartLoc, diag::parameter_unnamed_warn)
379-
.fixItInsert(typeStartLoc, "_: ");
382+
// An exception to this rule is when the type is declared with type sugar
383+
// Reference: SR-11724
384+
if (isa<OptionalTypeRepr>(param.Type)
385+
|| isa<ImplicitlyUnwrappedOptionalTypeRepr>(param.Type)) {
386+
diagnose(typeStartLoc, diag::parameter_unnamed)
387+
.fixItInsert(typeStartLoc, "_: ");
388+
} else {
389+
diagnose(typeStartLoc, diag::parameter_unnamed_warn)
390+
.fixItInsert(typeStartLoc, "_: ");
391+
}
380392
}
381393
}
382394
} else {

test/decl/func/functions.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ func recover_colon_arrow_7() :Int { } // expected-error {{expected '->' after f
5858

5959
func recover_missing_body_1() // expected-error {{expected '{' in body of function declaration}}
6060
func recover_missing_body_2() // expected-error {{expected '{' in body of function declaration}}
61-
-> Int
61+
-> Int
6262

6363
// Ensure that we don't skip over the 'func g' over to the right paren in
6464
// function g, while recovering from parse error in f() parameter tuple. We
6565
// should produce the error about missing right paren.
6666
//
6767
// FIXME: The errors are awful. We should produce just the error about paren.
68-
func f_recover_missing_tuple_paren(_ a: Int // expected-note {{to match this opening '('}} expected-error{{expected '{' in body of function declaration}} expected-error {{expected ')' in parameter}}
68+
func f_recover_missing_tuple_paren(_ a: Int // expected-note {{to match this opening '('}} expected-error{{expected '{' in body of function declaration}} expected-error {{expected ')' in parameter}}
6969
func g_recover_missing_tuple_paren(_ b: Int) {
7070
}
7171

@@ -187,14 +187,13 @@ func bogusDestructuring() {
187187
struct Bar {}
188188

189189
struct Foo {
190-
func registerCallback(_ callback: @escaping ([Bar]) -> Void) {} // expected-note {{found this candidate}}
191-
func registerCallback(_ callback: @escaping ([String: Bar]) -> Void) {} // expected-note {{found this candidate}}
192-
func registerCallback(_ callback: @escaping (Bar?) -> Void) {} // expected-note {{found this candidate}}
190+
func registerCallback(_ callback: @escaping ([Bar]) -> Void) {}
191+
func registerCallback(_ callback: @escaping ([String: Bar]) -> Void) {}
192+
func registerCallback(_ callback: @escaping (Bar?) -> Void) {}
193193
}
194194

195195
Foo().registerCallback { ([Bar]) in } // expected-warning {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
196196
Foo().registerCallback { ([String: Bar]) in }// expected-warning {{unnamed parameters must be written with the empty name '_'}} {{29-29=_: }}
197-
Foo().registerCallback { (Bar?) in } // expected-error {{ambiguous use of 'registerCallback'}}
198-
// expected-error@-1 {{expected parameter name followed by ':'}}
199-
// expected-error@-2 {{expected ',' separator}}
197+
Foo().registerCallback { (Bar?) in } // expected-error {{unnamed parameters must be written with the empty name '_'}}
198+
200199
}

0 commit comments

Comments
 (0)