Skip to content

Commit 5beb6f0

Browse files
committed
Restructure invalid parameter diagnostic
Diagnose the fact that we're missing a colon, not the fact that we're missing a type since that's actually what has gone wrong.
1 parent d67057a commit 5beb6f0

File tree

5 files changed

+9
-22
lines changed

5 files changed

+9
-22
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,6 @@ ERROR(expected_parameter_colon,PointsToFirstBadToken,
870870
"expected ':' following argument label and parameter name", ())
871871
ERROR(expected_assignment_instead_of_comparison_operator,none,
872872
"expected '=' instead of '==' to assign default value for parameter", ())
873-
ERROR(missing_parameter_type,PointsToFirstBadToken,
874-
"parameter requires an explicit type", ())
875873
ERROR(multiple_parameter_ellipsis,none,
876874
"only a single variadic parameter '...' is permitted", ())
877875
ERROR(parameter_vararg_default,none,

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,8 +4969,7 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
49694969
// was invalid.
49704970
if (invalid) {
49714971
for (auto accessor : Accessors) {
4972-
if (accessor)
4973-
accessor->setInvalid();
4972+
accessor->setInvalid();
49744973
}
49754974
}
49764975

lib/Parse/ParsePattern.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
327327
// was invalid. Remember that.
328328
if (type.isParseError() && !type.hasCodeCompletion())
329329
param.isInvalid = true;
330+
} else if (paramContext != Parser::ParameterContextKind::Closure) {
331+
diagnose(Tok, diag::expected_parameter_colon);
332+
param.isInvalid = true;
330333
}
331334
} else {
332335
// Otherwise, we have invalid code. Check to see if this looks like a
@@ -478,12 +481,6 @@ mapParsedParameters(Parser &parser,
478481
parser.CurDeclContext);
479482
param->getAttrs() = paramInfo.Attrs;
480483

481-
auto setInvalid = [&]{
482-
if (param->isInvalid())
483-
return;
484-
param->setInvalid();
485-
};
486-
487484
bool parsingEnumElt
488485
= (paramContext == Parser::ParameterContextKind::EnumElement);
489486
// If we're not parsing an enum case, lack of a SourceLoc for both
@@ -493,7 +490,7 @@ mapParsedParameters(Parser &parser,
493490

494491
// If we diagnosed this parameter as a parse error, propagate to the decl.
495492
if (paramInfo.isInvalid)
496-
setInvalid();
493+
param->setInvalid();
497494

498495
// If a type was provided, create the type for the parameter.
499496
if (auto type = paramInfo.Type) {
@@ -524,14 +521,7 @@ mapParsedParameters(Parser &parser,
524521
// or typealias with underlying function type.
525522
param->setAutoClosure(attrs.has(TypeAttrKind::TAK_autoclosure));
526523
}
527-
} else if (paramContext != Parser::ParameterContextKind::Closure) {
528-
// Non-closure parameters require a type.
529-
if (!param->isInvalid())
530-
parser.diagnose(param->getLoc(), diag::missing_parameter_type);
531-
532-
param->setSpecifier(ParamSpecifier::Default);
533-
setInvalid();
534-
} else if (paramInfo.SpecifierLoc.isValid()) {
524+
} if (paramInfo.SpecifierLoc.isValid()) {
535525
StringRef specifier;
536526
switch (paramInfo.SpecifierKind) {
537527
case ParamDecl::Specifier::InOut:

test/Parse/invalid.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protocol Animal<Food> { // expected-error {{protocols do not allow generic para
7676
// SR-573 - Crash with invalid parameter declaration
7777
class Starfish {}
7878
struct Salmon {}
79-
func f573(s Starfish, // expected-error {{parameter requires an explicit type}}
79+
func f573(s Starfish, // expected-error {{expected ':' following argument label and parameter name}}
8080
_ ss: Salmon) -> [Int] {}
8181
func g573() { f573(Starfish(), Salmon()) }
8282

@@ -89,7 +89,7 @@ func SR979b(inout inout b: Int) {} // expected-error {{inout' before a parameter
8989
// expected-error@-1 {{parameter must not have multiple '__owned', 'inout', or '__shared' specifiers}} {{19-25=}}
9090
func SR979d(let let a: Int) {} // expected-warning {{'let' in this position is interpreted as an argument label}} {{13-16=`let`}}
9191
// expected-error @-1 {{expected ',' separator}} {{20-20=,}}
92-
// expected-error @-2 {{parameter requires an explicit type}}
92+
// expected-error @-2 {{expected ':' following argument label and parameter name}}
9393
// expected-warning @-3 {{extraneous duplicate parameter name; 'let' already has an argument label}} {{13-17=}}
9494
func SR979e(inout x: inout String) {} // expected-error {{parameter must not have multiple '__owned', 'inout', or '__shared' specifiers}} {{13-18=}}
9595
func SR979g(inout i: inout Int) {} // expected-error {{parameter must not have multiple '__owned', 'inout', or '__shared' specifiers}} {{13-18=}}

test/Sema/immutability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func testSelectorStyleArguments1(_ x: Int, bar y: Int) {
370370
func testSelectorStyleArguments2(let x: Int, // expected-warning {{'let' in this position is interpreted as an argument label}}{{34-37=`let`}}
371371
let bar y: Int) { // expected-warning {{'let' in this position is interpreted as an argument label}}{{34-37=`let`}}
372372
// expected-error @-1 {{expected ',' separator}}
373-
// expected-error @-2 {{parameter requires an explicit type}}
373+
// expected-error @-2 {{expected ':' following argument label and parameter name}}
374374
}
375375
func testSelectorStyleArguments3(_ x: Int, bar y: Int) {
376376
++x // expected-error {{cannot pass immutable value to mutating operator: 'x' is a 'let' constant}}

0 commit comments

Comments
 (0)