Skip to content

[Lex] Reject standalone dollars as identifiers #3901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ void Lexer::lexOperatorIdentifier() {
return formToken(leftBound ? tok::oper_postfix : tok::oper_prefix, TokStart);
}

/// lexDollarIdent - Match $[0-9a-zA-Z_$]*
/// lexDollarIdent - Match $[0-9a-zA-Z_$]+
void Lexer::lexDollarIdent() {
const char *tokStart = CurPtr-1;
assert(*tokStart == '$');
Expand All @@ -826,10 +826,15 @@ void Lexer::lexDollarIdent() {
}
}

// It's always an error to see a standalone $, and we reserve
// $nonNumeric for persistent bindings in the debugger.
if (CurPtr == tokStart + 1 || !isAllDigits) {
if (!isAllDigits && !LangOpts.EnableDollarIdentifiers)
// It's always an error to see a standalone $
if (CurPtr == tokStart + 1) {
diagnose(tokStart, diag::expected_dollar_numeric);
return formToken(tok::unknown, tokStart);
}

// We reserve $nonNumeric for persistent bindings in the debugger.
if (!isAllDigits) {
if (!LangOpts.EnableDollarIdentifiers)
diagnose(tokStart, diag::expected_dollar_numeric);

// Even if we diagnose, we go ahead and form an identifier token,
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/compiler_version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#if !_compiler_version("777.*.7")
// This shouldn't emit any diagnostics.
$#%^*&
%#^*&
#endif

#if _compiler_version("700a.*.10") // expected-error {{version component contains non-numeric characters}}
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/ConditionalCompilation/language_version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#if !swift(>=1.0)
// This shouldn't emit any diagnostics.
$#%^*&
%#^*&
#endif

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

// There should be no error here.
adsf asdf asdf
$#%^*&
%#^*&
func foo(sdfsdfdsf adsf adsf asdf adsf adsf)
#endif
}
Expand Down
29 changes: 29 additions & 0 deletions test/Parse/dollar_identifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-parse-verify-swift

// SR-1661: Dollar was accidentally allowed as an identifier and identifier head.

func dollarVar() {
var $ : Int = 42 // expected-error {{expected numeric value following '$'}} expected-error {{expected pattern}}
}
func dollarLet() {
let $ = 42 // expected-error {{expected numeric value following '$'}} expected-error {{expected pattern}}
}
func dollarClass() {
class $ {} // expected-error {{expected numeric value following '$'}}
// expected-error@-1 {{expression resolves to an unused function}}
// expected-error@-2 {{expected identifier in class declaration}}
// expected-error@-3 {{braced block of statements is an unused closure}}
}
func dollarEnum() {
enum $ {} // expected-error {{expected numeric value following '$'}}
// expected-error@-1 {{expected identifier in enum declaration}}
// expected-error@-2 {{expression resolves to an unused function}}
// expected-error@-3 {{braced block of statements is an unused closure}}
}
func dollarStruct() {
struct $ {} // expected-error {{expected numeric value following '$'}}
// expected-error@-1 {{expected identifier in struct declaration}}
// expected-error@-2 {{braced block of statements is an unused closure}}
// expected-error@-3 {{expression resolves to an unused function}}
}