Skip to content

Commit ad8bd43

Browse files
authored
Merge pull request #3901 from CodaFi/dolla-dolla-bills-yall
[Lex] Reject standalone dollars as identifiers
2 parents e5c8092 + 389f779 commit ad8bd43

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

lib/Parse/Lexer.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ void Lexer::lexOperatorIdentifier() {
805805
return formToken(leftBound ? tok::oper_postfix : tok::oper_prefix, TokStart);
806806
}
807807

808-
/// lexDollarIdent - Match $[0-9a-zA-Z_$]*
808+
/// lexDollarIdent - Match $[0-9a-zA-Z_$]+
809809
void Lexer::lexDollarIdent() {
810810
const char *tokStart = CurPtr-1;
811811
assert(*tokStart == '$');
@@ -826,10 +826,15 @@ void Lexer::lexDollarIdent() {
826826
}
827827
}
828828

829-
// It's always an error to see a standalone $, and we reserve
830-
// $nonNumeric for persistent bindings in the debugger.
831-
if (CurPtr == tokStart + 1 || !isAllDigits) {
832-
if (!isAllDigits && !LangOpts.EnableDollarIdentifiers)
829+
// It's always an error to see a standalone $
830+
if (CurPtr == tokStart + 1) {
831+
diagnose(tokStart, diag::expected_dollar_numeric);
832+
return formToken(tok::unknown, tokStart);
833+
}
834+
835+
// We reserve $nonNumeric for persistent bindings in the debugger.
836+
if (!isAllDigits) {
837+
if (!LangOpts.EnableDollarIdentifiers)
833838
diagnose(tokStart, diag::expected_dollar_numeric);
834839

835840
// Even if we diagnose, we go ahead and form an identifier token,

test/Parse/ConditionalCompilation/compiler_version.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#if !_compiler_version("777.*.7")
3333
// This shouldn't emit any diagnostics.
34-
$#%^*&
34+
%#^*&
3535
#endif
3636

3737
#if _compiler_version("700a.*.10") // expected-error {{version component contains non-numeric characters}}

test/Parse/ConditionalCompilation/language_version.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#if !swift(>=1.0)
3333
// This shouldn't emit any diagnostics.
34-
$#%^*&
34+
%#^*&
3535
#endif
3636

3737
#if swift(">=7.1") // expected-error {{unexpected platform condition argument: expected a unary comparison, such as '>=2.2'}}
@@ -62,7 +62,7 @@ protocol P {
6262

6363
// There should be no error here.
6464
adsf asdf asdf
65-
$#%^*&
65+
%#^*&
6666
func foo(sdfsdfdsf adsf adsf asdf adsf adsf)
6767
#endif
6868
}

test/Parse/dollar_identifier.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
// SR-1661: Dollar was accidentally allowed as an identifier and identifier head.
4+
5+
func dollarVar() {
6+
var $ : Int = 42 // expected-error {{expected numeric value following '$'}} expected-error {{expected pattern}}
7+
}
8+
func dollarLet() {
9+
let $ = 42 // expected-error {{expected numeric value following '$'}} expected-error {{expected pattern}}
10+
}
11+
func dollarClass() {
12+
class $ {} // expected-error {{expected numeric value following '$'}}
13+
// expected-error@-1 {{expression resolves to an unused function}}
14+
// expected-error@-2 {{expected identifier in class declaration}}
15+
// expected-error@-3 {{braced block of statements is an unused closure}}
16+
}
17+
func dollarEnum() {
18+
enum $ {} // expected-error {{expected numeric value following '$'}}
19+
// expected-error@-1 {{expected identifier in enum declaration}}
20+
// expected-error@-2 {{expression resolves to an unused function}}
21+
// expected-error@-3 {{braced block of statements is an unused closure}}
22+
}
23+
func dollarStruct() {
24+
struct $ {} // expected-error {{expected numeric value following '$'}}
25+
// expected-error@-1 {{expected identifier in struct declaration}}
26+
// expected-error@-2 {{braced block of statements is an unused closure}}
27+
// expected-error@-3 {{expression resolves to an unused function}}
28+
}
29+

0 commit comments

Comments
 (0)