Skip to content

Commit 509db74

Browse files
authored
[Lexer] Disallow '$' as a start of identifier, special handle '$' (#6004)
Allowing 'let `$0` = 1', introduced in 6accc59 was not intentional.
1 parent 4166e2b commit 509db74

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/Parse/Lexer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static bool isValidIdentifierContinuationCodePoint(uint32_t c) {
472472
static bool isValidIdentifierStartCodePoint(uint32_t c) {
473473
if (!isValidIdentifierContinuationCodePoint(c))
474474
return false;
475-
if (c < 0x80 && isDigit(c))
475+
if (c < 0x80 && (isDigit(c) || c == '$'))
476476
return false;
477477

478478
// N1518: Recommendations for extended identifier characters for C and C++
@@ -1385,6 +1385,14 @@ void Lexer::lexEscapedIdentifier() {
13851385
}
13861386
}
13871387

1388+
// Special case; allow '`$`'.
1389+
if (Quote[1] == '$' && Quote[2] == '`') {
1390+
CurPtr = Quote + 3;
1391+
formToken(tok::identifier, Quote);
1392+
NextToken.setEscapedIdentifier(true);
1393+
return;
1394+
}
1395+
13881396
// The backtick is punctuation.
13891397
CurPtr = IdentifierStart;
13901398
formToken(tok::backtick, Quote);

test/Parse/dollar_identifier.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ func escapedDollarFunc() {
5454
func `$`(`$`: Int) {} // no error
5555
`$`(`$`: 25) // no error
5656
}
57+
58+
func escapedDollarAnd() {
59+
// FIXME: Bad diagnostics.
60+
`$0` = 1 // expected-error {{expected expression}}
61+
`$$` = 2 // expected-error {{expected numeric value following '$'}}
62+
`$abc` = 3 // expected-error {{expected numeric value following '$'}}
63+
}

0 commit comments

Comments
 (0)