Skip to content

Commit 8ee83b9

Browse files
authored
Merge pull request #19972 from compnerd/removing-syntax
Syntax: correct implementation for `removing`
2 parents fe59d99 + e07c00c commit 8ee83b9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

include/swift/Syntax/SyntaxCollection.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "swift/Syntax/Syntax.h"
1717

18+
#include <iterator>
19+
1820
namespace swift {
1921
namespace syntax {
2022

@@ -173,8 +175,11 @@ class SyntaxCollection : public Syntax {
173175

174176
/// Return a new collection with the element removed at index i.
175177
SyntaxCollection<CollectionKind, Element> removing(size_t i) const {
176-
auto NewLayout = getRaw()->Layout;
177-
NewLayout.erase(NewLayout.begin() + i);
178+
assert(i <= size());
179+
std::vector<RC<RawSyntax>> NewLayout = getRaw()->getLayout();
180+
auto iterator = NewLayout.begin();
181+
std::advance(iterator, i);
182+
NewLayout.erase(iterator);
178183
auto Raw = RawSyntax::make(CollectionKind, NewLayout, getRaw()->getPresence());
179184
return Data->replaceSelf<SyntaxCollection<CollectionKind, Element>>(Raw);
180185
}

unittests/Syntax/SyntaxCollectionTests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,20 @@ TEST(SyntaxCollectionTests, Iteration) {
257257
List.print(OS);
258258
ASSERT_EQ(OS.str().str(), IteratedOS.str().str());
259259
}
260+
261+
TEST(SyntaxCollectionTests, Removing) {
262+
auto Arg = getCannedArgument();
263+
auto List = SyntaxFactory::makeBlankFunctionCallArgumentList()
264+
.appending(Arg)
265+
.appending(Arg.withLabel(SyntaxFactory::makeIdentifier("first", {}, {})))
266+
.appending(Arg)
267+
.removing(1);
268+
269+
ASSERT_EQ(List.size(), static_cast<size_t>(2));
270+
271+
SmallString<48> Scratch;
272+
llvm::raw_svector_ostream OS(Scratch);
273+
List.print(OS);
274+
ASSERT_EQ(OS.str().str(), "x: foo, x: foo, ");
275+
}
276+

0 commit comments

Comments
 (0)