Skip to content

[Parse] Move standalone_dollar_identifier diagnosis to Parser. #34534

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 1 commit into from
Nov 4, 2020
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
21 changes: 16 additions & 5 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ class Parser {
if (Result)
*Result = Context.getIdentifier(Tok.getText());

if (Tok.getText()[0] == '$' && !allowDollarIdentifier)
diagnoseDollarIdentifier(Tok);
if (Tok.getText()[0] == '$')
diagnoseDollarIdentifier(Tok, allowDollarIdentifier);

return consumeToken();
}
Expand All @@ -588,11 +588,22 @@ class Parser {

/// When we have a token that is an identifier starting with '$',
/// diagnose it if not permitted in this mode.
void diagnoseDollarIdentifier(const Token &tok) {
void diagnoseDollarIdentifier(const Token &tok,
bool allowDollarIdentifier = false) {
assert(tok.getText()[0] == '$');

if (tok.getText().size() == 1 ||
Context.LangOpts.EnableDollarIdentifiers ||
// If '$' is not guarded by backticks, offer
// to replace it with '`$`'.
if (Tok.getRawText() == "$") {
diagnose(Tok.getLoc(), diag::standalone_dollar_identifier)
.fixItReplace(Tok.getLoc(), "`$`");
return;
}

if (allowDollarIdentifier)
return;

if (tok.getText().size() == 1 || Context.LangOpts.EnableDollarIdentifiers ||
isInSILMode() || L->isSwiftInterface())
return;

Expand Down
4 changes: 1 addition & 3 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,8 @@ void Lexer::lexDollarIdent() {
break;
}

// If there is a standalone '$', treat it like an identifier.
if (CurPtr == tokStart + 1) {
// It is an error to see a standalone '$'. Offer to replace '$' with '`$`'.
diagnose(tokStart, diag::standalone_dollar_identifier)
.fixItReplaceChars(getSourceLoc(tokStart), getSourceLoc(CurPtr), "`$`");
return formToken(tok::identifier, tokStart);
}

Expand Down
12 changes: 7 additions & 5 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7719,11 +7719,13 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
bool AllowTopLevel = Flags.contains(PD_AllowTopLevel);

const auto maybeDiagnoseInvalidCharInOperatorName = [this](const Token &Tk) {
if (Tk.is(tok::identifier) &&
DeclAttribute::getAttrKindFromString(Tk.getText()) ==
DeclAttrKind::DAK_Count) {
diagnose(Tk, diag::identifier_within_operator_name, Tk.getText());
return true;
if (Tk.is(tok::identifier)) {
if (Tk.getText().equals("$") ||
DeclAttribute::getAttrKindFromString(Tk.getText()) ==
DeclAttrKind::DAK_Count) {
diagnose(Tk, diag::identifier_within_operator_name, Tk.getText());
return true;
}
} else if (Tk.isNot(tok::colon, tok::l_brace, tok::semi) &&
Tk.isPunctuation()) {
diagnose(Tk, diag::operator_name_invalid_char,
Expand Down
3 changes: 3 additions & 0 deletions test/Parse/dollar_identifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ struct S {
}

let _ = S().$café // Okay

infix operator $ // expected-error{{'$' is considered an identifier and must not appear within an operator name}} // SR-13092
infix operator `$` // expected-error{{'$' is considered an identifier and must not appear within an operator name}} // SR-13092