@@ -2239,11 +2239,14 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
2239
2239
case tok::kw_let:
2240
2240
case tok::kw_var: {
2241
2241
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;
2244
2245
StaticLoc = SourceLoc (); // we handled static if present.
2245
2246
MayNeedOverrideCompletion = true ;
2246
2247
std::for_each (Entries.begin (), Entries.end (), InternalHandler);
2248
+ if (auto *D = DeclResult.getPtrOrNull ())
2249
+ markWasHandled (D);
2247
2250
break ;
2248
2251
}
2249
2252
case tok::kw_typealias:
@@ -4221,12 +4224,13 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
4221
4224
4222
4225
4223
4226
// / \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) {
4230
4234
assert (StaticLoc.isInvalid () || StaticSpelling != StaticSpellingKind::None);
4231
4235
4232
4236
if (StaticLoc.isValid ()) {
@@ -4266,19 +4270,22 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4266
4270
// In var/let decl with multiple patterns, accumulate them all in this list
4267
4271
// so we can build our singular PatternBindingDecl at the end.
4268
4272
SmallVector<PatternBindingEntry, 4 > PBDEntries;
4273
+ auto BaseContext = CurDeclContext;
4269
4274
4270
4275
// No matter what error path we take, make sure the
4271
4276
// PatternBindingDecl/TopLevel code block are added.
4272
- SWIFT_DEFER {
4277
+ auto makeResult =
4278
+ [&](ParserStatus Status) -> ParserResult<PatternBindingDecl> {
4279
+
4273
4280
// If we didn't parse any patterns, don't create the pattern binding decl.
4274
4281
if (PBDEntries.empty ())
4275
- return ;
4282
+ return Status ;
4276
4283
4277
4284
// Now that we've parsed all of our patterns, initializers and accessors, we
4278
4285
// can finally create our PatternBindingDecl to represent the
4279
4286
// pattern/initializer pairs.
4280
4287
auto PBD = PatternBindingDecl::create (Context, StaticLoc, StaticSpelling,
4281
- VarLoc, PBDEntries, CurDeclContext );
4288
+ VarLoc, PBDEntries, BaseContext );
4282
4289
4283
4290
// Wire up any initializer contexts we needed.
4284
4291
for (unsigned i : indices (PBDEntries)) {
@@ -4295,13 +4302,16 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4295
4302
topLevelDecl->setBody (BraceStmt::create (Context, range.Start ,
4296
4303
ASTNode (PBD), range.End , true ));
4297
4304
Decls.insert (Decls.begin ()+NumDeclsInResult, topLevelDecl);
4298
- return ;
4305
+ return makeParserResult (Status, PBD) ;
4299
4306
}
4300
4307
4301
4308
// Otherwise return the PBD in "Decls" to the caller. We add it at a
4302
4309
// specific spot to get it in before any accessors, which SILGen seems to
4303
4310
// want.
4304
4311
Decls.insert (Decls.begin ()+NumDeclsInResult, PBD);
4312
+
4313
+ // Always return the result for PBD.
4314
+ return makeParserResult (Status, PBD);
4305
4315
};
4306
4316
4307
4317
do {
@@ -4313,9 +4323,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4313
4323
4314
4324
auto patternRes = parseTypedPattern ();
4315
4325
if (patternRes.hasCodeCompletion ())
4316
- return makeParserCodeCompletionStatus ();
4326
+ return makeResult ( makeParserCodeCompletionStatus () );
4317
4327
if (patternRes.isNull ())
4318
- return makeParserError ();
4328
+ return makeResult ( makeParserError () );
4319
4329
4320
4330
pattern = patternRes.get ();
4321
4331
}
@@ -4404,12 +4414,13 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4404
4414
if (init.hasCodeCompletion () && isCodeCompletionFirstPass ()) {
4405
4415
4406
4416
// 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 ());
4409
4420
}
4410
4421
4411
4422
if (init.isNull ())
4412
- return makeParserError ();
4423
+ return makeResult ( makeParserError () );
4413
4424
}
4414
4425
4415
4426
// Parse a behavior block if present.
@@ -4420,9 +4431,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4420
4431
auto type = parseType (diag::expected_behavior_name,
4421
4432
/* handle completion*/ true );
4422
4433
if (type.isParseError ())
4423
- return makeParserError ();
4434
+ return makeResult ( makeParserError () );
4424
4435
if (type.hasCodeCompletion ())
4425
- return makeParserCodeCompletionStatus ();
4436
+ return makeResult ( makeParserCodeCompletionStatus () );
4426
4437
4427
4438
// Parse a following trailing closure argument.
4428
4439
// FIXME: Handle generalized parameters.
@@ -4464,9 +4475,9 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4464
4475
PBDEntries.back ().setInitContext (initContext);
4465
4476
4466
4477
if (closure.isParseError ())
4467
- return makeParserError ();
4478
+ return makeResult ( makeParserError () );
4468
4479
if (closure.hasCodeCompletion ())
4469
- return makeParserCodeCompletionStatus ();
4480
+ return makeResult ( makeParserCodeCompletionStatus () );
4470
4481
paramExpr = closure.get ();
4471
4482
}
4472
4483
@@ -4546,10 +4557,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
4546
4557
}
4547
4558
}
4548
4559
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);
4553
4561
}
4554
4562
4555
4563
void Parser::consumeAbstractFunctionBody (AbstractFunctionDecl *AFD,
0 commit comments