Skip to content

Commit 05ef088

Browse files
author
Nathan Hawes
authored
Revert "Remove The Last Vestiges of isInvalid from Parse"
1 parent c559655 commit 05ef088

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,8 @@ 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", ())
873875
ERROR(multiple_parameter_ellipsis,none,
874876
"only a single variadic parameter '...' is permitted", ())
875877
ERROR(parameter_vararg_default,none,

lib/Parse/ParseDecl.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4933,6 +4933,12 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
49334933
storage->setAccessors(LBLoc, Accessors, RBLoc);
49344934
}
49354935

4936+
static void flagInvalidAccessor(AccessorDecl *func) {
4937+
if (func) {
4938+
func->setInvalid();
4939+
}
4940+
}
4941+
49364942
static void diagnoseConflictingAccessors(Parser &P, AccessorDecl *first,
49374943
AccessorDecl *&second) {
49384944
if (!second) return;
@@ -4943,7 +4949,7 @@ static void diagnoseConflictingAccessors(Parser &P, AccessorDecl *first,
49434949
P.diagnose(first->getLoc(), diag::previous_accessor,
49444950
getAccessorNameForDiagnostic(first, /*article*/ false),
49454951
/*already*/ false);
4946-
second->setInvalid();
4952+
flagInvalidAccessor(second);
49474953
}
49484954

49494955
template <class... DiagArgs>
@@ -4953,11 +4959,11 @@ static void diagnoseAndIgnoreObservers(Parser &P,
49534959
typename std::enable_if<true, DiagArgs>::type... args) {
49544960
if (auto &accessor = accessors.WillSet) {
49554961
P.diagnose(accessor->getLoc(), diagnostic, /*willSet*/ 0, args...);
4956-
accessor->setInvalid();
4962+
flagInvalidAccessor(accessor);
49574963
}
49584964
if (auto &accessor = accessors.DidSet) {
49594965
P.diagnose(accessor->getLoc(), diagnostic, /*didSet*/ 1, args...);
4960-
accessor->setInvalid();
4966+
flagInvalidAccessor(accessor);
49614967
}
49624968
}
49634969

@@ -4969,7 +4975,7 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
49694975
// was invalid.
49704976
if (invalid) {
49714977
for (auto accessor : Accessors) {
4972-
accessor->setInvalid();
4978+
flagInvalidAccessor(accessor);
49734979
}
49744980
}
49754981

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,8 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26562656
// to detect any tuple splat or destructuring as early as
26572657
// possible and give a proper fix-it. See SE-0110 for more details.
26582658
auto isTupleDestructuring = [](ParamDecl *param) -> bool {
2659+
if (!param->isInvalid())
2660+
return false;
26592661
if (auto *typeRepr = param->getTypeRepr())
26602662
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
26612663
return false;

lib/Parse/ParsePattern.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,6 @@ 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;
333330
}
334331
} else {
335332
// Otherwise, we have invalid code. Check to see if this looks like a
@@ -361,6 +358,8 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
361358
// on is most likely argument destructuring, we are going
362359
// to diagnose that after all of the parameters are parsed.
363360
if (param.Type) {
361+
// Mark current parameter as invalid so it is possible
362+
// to diagnose it as destructuring of the closure parameter list.
364363
param.isInvalid = true;
365364
if (!isClosure) {
366365
// Unnamed parameters must be written as "_: Type".
@@ -479,6 +478,12 @@ mapParsedParameters(Parser &parser,
479478
parser.CurDeclContext);
480479
param->getAttrs() = paramInfo.Attrs;
481480

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

489494
// If we diagnosed this parameter as a parse error, propagate to the decl.
490495
if (paramInfo.isInvalid)
491-
param->setInvalid();
496+
setInvalid();
492497

493498
// If a type was provided, create the type for the parameter.
494499
if (auto type = paramInfo.Type) {
@@ -519,6 +524,13 @@ mapParsedParameters(Parser &parser,
519524
// or typealias with underlying function type.
520525
param->setAutoClosure(attrs.has(TypeAttrKind::TAK_autoclosure));
521526
}
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();
522534
} else if (paramInfo.SpecifierLoc.isValid()) {
523535
StringRef specifier;
524536
switch (paramInfo.SpecifierKind) {

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 {{expected ':' following argument label and parameter name}}
79+
func f573(s Starfish, // expected-error {{parameter requires an explicit type}}
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 {{expected ':' following argument label and parameter name}}
92+
// expected-error @-2 {{parameter requires an explicit type}}
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 {{expected ':' following argument label and parameter name}}
373+
// expected-error @-2 {{parameter requires an explicit type}}
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)