@@ -3220,6 +3220,26 @@ void Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
3220
3220
}
3221
3221
}
3222
3222
3223
+ // / Return copy of \p existingNodes with \p newNode inserted in source order
3224
+ static SmallVector<ASTNode, 8 > insertInOrder (ArrayRef<ASTNode> existingNodes,
3225
+ const ASTNode newNode,
3226
+ SourceManager &SM) {
3227
+ SmallVector<ASTNode, 8 > nodesWithInsertion;
3228
+ bool didInsert = false ;
3229
+ auto newNodeStart = newNode.getStartLoc ();
3230
+ for (auto n : existingNodes) {
3231
+ if (SM.isBeforeInBuffer (newNodeStart, n.getStartLoc ())) {
3232
+ nodesWithInsertion.push_back (newNode);
3233
+ didInsert = true ;
3234
+ }
3235
+ nodesWithInsertion.push_back (n);
3236
+ }
3237
+ if (!didInsert)
3238
+ nodesWithInsertion.push_back (newNode);
3239
+
3240
+ return nodesWithInsertion;
3241
+ }
3242
+
3223
3243
void Parser::parseDeclDelayed () {
3224
3244
auto DelayedState = State->takeDelayedDeclState ();
3225
3245
assert (DelayedState.get () && " should have delayed state" );
@@ -3258,11 +3278,10 @@ void Parser::parseDeclDelayed() {
3258
3278
} else if (auto *SF = dyn_cast<SourceFile>(parent)) {
3259
3279
SF->Decls .push_back (D);
3260
3280
} else if (auto *CE = dyn_cast<ClosureExpr>(parent)) {
3261
- // Replace the closure body with one including the new Decl.
3262
- auto *body = CE->getBody ();
3263
- SmallVector<ASTNode, 8 > Elts (body->getElements ().begin (),
3264
- body->getElements ().end ());
3265
- Elts.push_back (ASTNode (D));
3281
+ // Replace the closure body with one including the new Decl, inserted in
3282
+ // order.
3283
+ auto *const body = CE->getBody ();
3284
+ auto Elts = insertInOrder (body->getElements (), D, SourceMgr);
3266
3285
auto *newBody =
3267
3286
BraceStmt::create (Context, body->getLBraceLoc (), Elts,
3268
3287
body->getRBraceLoc (), body->isImplicit ());
@@ -4878,9 +4897,7 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
4878
4897
// If we have an invalid case, bail out now.
4879
4898
if (!PrimaryVar) {
4880
4899
fillInAccessorTypeErrors (*this , accessors);
4881
- Decls.append (accessors.Accessors .begin (), accessors.Accessors .end ());
4882
- // Preserve the invariaent that accessor an can be found from its
4883
- // VarDecl.
4900
+ // Preserve the invariant that an accessor can be found from its VarDecl
4884
4901
accessors.record (*this , storage, Invalid, Decls);
4885
4902
return nullptr ;
4886
4903
}
0 commit comments