Skip to content

Commit 3705e2e

Browse files
committed
[Parse] Make parseDeclVar() always return the ParserResult for PatternBindingDecl
1 parent 65ec838 commit 3705e2e

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

include/swift/Parse/Parser.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,12 @@ class Parser {
743743
ParserResult<ClassDecl>
744744
parseDeclClass(SourceLoc ClassLoc,
745745
ParseDeclOptions Flags, DeclAttributes &Attributes);
746-
ParserStatus parseDeclVar(ParseDeclOptions Flags, DeclAttributes &Attributes,
747-
SmallVectorImpl<Decl *> &Decls,
748-
SourceLoc StaticLoc,
749-
StaticSpellingKind StaticSpelling,
750-
SourceLoc TryLoc);
746+
ParserResult<PatternBindingDecl>
747+
parseDeclVar(ParseDeclOptions Flags, DeclAttributes &Attributes,
748+
SmallVectorImpl<Decl *> &Decls,
749+
SourceLoc StaticLoc,
750+
StaticSpellingKind StaticSpelling,
751+
SourceLoc TryLoc);
751752

752753
void consumeGetSetBody(AbstractFunctionDecl *AFD, SourceLoc LBLoc);
753754

lib/Parse/ParseDecl.cpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,11 +2239,14 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
22392239
case tok::kw_let:
22402240
case tok::kw_var: {
22412241
llvm::SmallVector<Decl *, 4> Entries;
2242-
Status = parseDeclVar(Flags, Attributes, Entries, StaticLoc,
2243-
StaticSpelling, tryLoc);
2242+
DeclResult = parseDeclVar(Flags, Attributes, Entries, StaticLoc,
2243+
StaticSpelling, tryLoc);
2244+
Status = DeclResult;
22442245
StaticLoc = SourceLoc(); // we handled static if present.
22452246
MayNeedOverrideCompletion = true;
22462247
std::for_each(Entries.begin(), Entries.end(), InternalHandler);
2248+
if (auto *D = DeclResult.getPtrOrNull())
2249+
markWasHandled(D);
22472250
break;
22482251
}
22492252
case tok::kw_typealias:
@@ -4221,12 +4224,13 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
42214224

42224225

42234226
/// \brief Parse a 'var' or 'let' declaration, doing no token skipping on error.
4224-
ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4225-
DeclAttributes &Attributes,
4226-
SmallVectorImpl<Decl *> &Decls,
4227-
SourceLoc StaticLoc,
4228-
StaticSpellingKind StaticSpelling,
4229-
SourceLoc TryLoc) {
4227+
ParserResult<PatternBindingDecl>
4228+
Parser::parseDeclVar(ParseDeclOptions Flags,
4229+
DeclAttributes &Attributes,
4230+
SmallVectorImpl<Decl *> &Decls,
4231+
SourceLoc StaticLoc,
4232+
StaticSpellingKind StaticSpelling,
4233+
SourceLoc TryLoc) {
42304234
assert(StaticLoc.isInvalid() || StaticSpelling != StaticSpellingKind::None);
42314235

42324236
if (StaticLoc.isValid()) {
@@ -4266,19 +4270,22 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
42664270
// In var/let decl with multiple patterns, accumulate them all in this list
42674271
// so we can build our singular PatternBindingDecl at the end.
42684272
SmallVector<PatternBindingEntry, 4> PBDEntries;
4273+
auto BaseContext = CurDeclContext;
42694274

42704275
// No matter what error path we take, make sure the
42714276
// PatternBindingDecl/TopLevel code block are added.
4272-
SWIFT_DEFER {
4277+
auto makeResult =
4278+
[&](ParserStatus Status) -> ParserResult<PatternBindingDecl> {
4279+
42734280
// If we didn't parse any patterns, don't create the pattern binding decl.
42744281
if (PBDEntries.empty())
4275-
return;
4282+
return Status;
42764283

42774284
// Now that we've parsed all of our patterns, initializers and accessors, we
42784285
// can finally create our PatternBindingDecl to represent the
42794286
// pattern/initializer pairs.
42804287
auto PBD = PatternBindingDecl::create(Context, StaticLoc, StaticSpelling,
4281-
VarLoc, PBDEntries, CurDeclContext);
4288+
VarLoc, PBDEntries, BaseContext);
42824289

42834290
// Wire up any initializer contexts we needed.
42844291
for (unsigned i : indices(PBDEntries)) {
@@ -4295,13 +4302,16 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
42954302
topLevelDecl->setBody(BraceStmt::create(Context, range.Start,
42964303
ASTNode(PBD), range.End, true));
42974304
Decls.insert(Decls.begin()+NumDeclsInResult, topLevelDecl);
4298-
return;
4305+
return makeParserResult(Status, PBD);
42994306
}
43004307

43014308
// Otherwise return the PBD in "Decls" to the caller. We add it at a
43024309
// specific spot to get it in before any accessors, which SILGen seems to
43034310
// want.
43044311
Decls.insert(Decls.begin()+NumDeclsInResult, PBD);
4312+
4313+
// Always return the result for PBD.
4314+
return makeParserResult(Status, PBD);
43054315
};
43064316

43074317
do {
@@ -4313,9 +4323,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
43134323

43144324
auto patternRes = parseTypedPattern();
43154325
if (patternRes.hasCodeCompletion())
4316-
return makeParserCodeCompletionStatus();
4326+
return makeResult(makeParserCodeCompletionStatus());
43174327
if (patternRes.isNull())
4318-
return makeParserError();
4328+
return makeResult(makeParserError());
43194329

43204330
pattern = patternRes.get();
43214331
}
@@ -4404,12 +4414,13 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
44044414
if (init.hasCodeCompletion() && isCodeCompletionFirstPass()) {
44054415

44064416
// Register the end of the init as the end of the delayed parsing.
4407-
DelayedDeclEnd = init.getPtrOrNull() ? init.get()->getEndLoc() : SourceLoc();
4408-
return makeParserCodeCompletionStatus();
4417+
DelayedDeclEnd
4418+
= init.getPtrOrNull() ? init.get()->getEndLoc() : SourceLoc();
4419+
return makeResult(makeParserCodeCompletionStatus());
44094420
}
44104421

44114422
if (init.isNull())
4412-
return makeParserError();
4423+
return makeResult(makeParserError());
44134424
}
44144425

44154426
// Parse a behavior block if present.
@@ -4420,9 +4431,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
44204431
auto type = parseType(diag::expected_behavior_name,
44214432
/*handle completion*/ true);
44224433
if (type.isParseError())
4423-
return makeParserError();
4434+
return makeResult(makeParserError());
44244435
if (type.hasCodeCompletion())
4425-
return makeParserCodeCompletionStatus();
4436+
return makeResult(makeParserCodeCompletionStatus());
44264437

44274438
// Parse a following trailing closure argument.
44284439
// FIXME: Handle generalized parameters.
@@ -4464,9 +4475,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
44644475
PBDEntries.back().setInitContext(initContext);
44654476

44664477
if (closure.isParseError())
4467-
return makeParserError();
4478+
return makeResult(makeParserError());
44684479
if (closure.hasCodeCompletion())
4469-
return makeParserCodeCompletionStatus();
4480+
return makeResult(makeParserCodeCompletionStatus());
44704481
paramExpr = closure.get();
44714482
}
44724483

@@ -4546,10 +4557,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
45464557
}
45474558
}
45484559

4549-
// NOTE: At this point, the DoAtScopeExit object is destroyed and the PBD
4550-
// is added to the program.
4551-
4552-
return Status;
4560+
return makeResult(Status);
45534561
}
45544562

45554563
void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,

0 commit comments

Comments
 (0)