Skip to content

Commit 31a7859

Browse files
committed
ASTMangler: Fix assertion when mangling invalid identifiers
We sometimes need to mangle invalid identifiers, for example when sorting declarations in EmittedMembersRequest. Make sure this doesn't assert.
1 parent ff2cc35 commit 31a7859

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

include/swift/Demangling/ManglingUtils.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ void mangleIdentifier(Mangler &M, StringRef ident) {
192192
// Mangle the sub-string up to the next word substitution (or to the end
193193
// of the identifier - that's why we added the dummy-word).
194194
// The first thing: we add the encoded sub-string length.
195+
bool first = true;
195196
M.Buffer << (Repl.StringPos - Pos);
196-
assert(!isDigit(ident[Pos]) &&
197-
"first char of sub-string may not be a digit");
198197
do {
199198
// Update the start position of new added words, so that they refer to
200199
// the begin of the whole mangled Buffer.
@@ -203,9 +202,16 @@ void mangleIdentifier(Mangler &M, StringRef ident) {
203202
M.Words[WordsInBuffer].start = M.getBufferStr().size();
204203
WordsInBuffer++;
205204
}
205+
// Error recovery. We sometimes need to mangle identifiers coming
206+
// from invalid code.
207+
if (first && isDigit(ident[Pos]))
208+
M.Buffer << 'X';
206209
// Add a literal character of the sub-string.
207-
M.Buffer << ident[Pos];
210+
else
211+
M.Buffer << ident[Pos];
212+
208213
Pos++;
214+
first = false;
209215
} while (Pos < Repl.StringPos);
210216
}
211217
// Is it a "real" word substitution (and not the dummy-word)?
+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol 0{class TextOutputStream

0 commit comments

Comments
 (0)