Skip to content

Commit 3076912

Browse files
author
David Ungar
committed
Insert in order and other fix
1 parent 9f60bb4 commit 3076912

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,26 @@ void Parser::parseDeclListDelayed(IterableDeclContext *IDC) {
32203220
}
32213221
}
32223222

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+
32233243
void Parser::parseDeclDelayed() {
32243244
auto DelayedState = State->takeDelayedDeclState();
32253245
assert(DelayedState.get() && "should have delayed state");
@@ -3258,11 +3278,10 @@ void Parser::parseDeclDelayed() {
32583278
} else if (auto *SF = dyn_cast<SourceFile>(parent)) {
32593279
SF->Decls.push_back(D);
32603280
} 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);
32663285
auto *newBody =
32673286
BraceStmt::create(Context, body->getLBraceLoc(), Elts,
32683287
body->getRBraceLoc(), body->isImplicit());
@@ -4878,9 +4897,7 @@ Parser::parseDeclVarGetSet(Pattern *pattern, ParseDeclOptions Flags,
48784897
// If we have an invalid case, bail out now.
48794898
if (!PrimaryVar) {
48804899
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
48844901
accessors.record(*this, storage, Invalid, Decls);
48854902
return nullptr;
48864903
}

0 commit comments

Comments
 (0)