Skip to content

Parse: Only diagnose dollar-prefixed identifiers that are Swift declarations #34620

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
Dec 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
50 changes: 21 additions & 29 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,14 +553,13 @@ class Parser {
return Tok.getLoc().getAdvancedLoc(-LeadingTrivia.getLength());
}

SourceLoc consumeIdentifier(Identifier *Result = nullptr,
bool allowDollarIdentifier = false) {
SourceLoc consumeIdentifier(Identifier &Result, bool diagnoseDollarPrefix) {
assert(Tok.isAny(tok::identifier, tok::kw_self, tok::kw_Self));
if (Result)
*Result = Context.getIdentifier(Tok.getText());
assert(Result.empty());
Result = Context.getIdentifier(Tok.getText());

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

return consumeToken();
}
Expand All @@ -573,15 +572,17 @@ class Parser {
Result = Context.getIdentifier(Tok.getText());

if (Tok.getText()[0] == '$')
diagnoseDollarIdentifier(Tok);
diagnoseDollarIdentifier(Tok, /*diagnoseDollarPrefix=*/true);
}
return consumeToken();
}

/// When we have a token that is an identifier starting with '$',
/// diagnose it if not permitted in this mode.
/// \param diagnoseDollarPrefix Whether to diagnose dollar-prefixed
/// identifiers in addition to a standalone '$'.
void diagnoseDollarIdentifier(const Token &tok,
bool allowDollarIdentifier = false) {
bool diagnoseDollarPrefix) {
assert(tok.getText()[0] == '$');

// If '$' is not guarded by backticks, offer
Expand All @@ -592,7 +593,7 @@ class Parser {
return;
}

if (allowDollarIdentifier)
if (!diagnoseDollarPrefix)
return;

if (tok.getText().size() == 1 || Context.LangOpts.EnableDollarIdentifiers ||
Expand Down Expand Up @@ -790,24 +791,20 @@ class Parser {
/// its name in \p Result. Otherwise, emit an error.
///
/// \returns false on success, true on error.
bool parseIdentifier(Identifier &Result, SourceLoc &Loc, const Diagnostic &D);

bool parseIdentifier(Identifier &Result, SourceLoc &Loc, const Diagnostic &D,
bool diagnoseDollarPrefix);

/// Consume an identifier with a specific expected name. This is useful for
/// contextually sensitive keywords that must always be present.
bool parseSpecificIdentifier(StringRef expected, SourceLoc &Loc,
const Diagnostic &D);

template<typename ...DiagArgTypes, typename ...ArgTypes>
bool parseIdentifier(Identifier &Result, Diag<DiagArgTypes...> ID,
ArgTypes... Args) {
SourceLoc L;
return parseIdentifier(Result, L, Diagnostic(ID, Args...));
}

template<typename ...DiagArgTypes, typename ...ArgTypes>
bool parseIdentifier(Identifier &Result, SourceLoc &L,
Diag<DiagArgTypes...> ID, ArgTypes... Args) {
return parseIdentifier(Result, L, Diagnostic(ID, Args...));
bool diagnoseDollarPrefix, Diag<DiagArgTypes...> ID,
ArgTypes... Args) {
return parseIdentifier(Result, L, Diagnostic(ID, Args...),
diagnoseDollarPrefix);
}

template<typename ...DiagArgTypes, typename ...ArgTypes>
Expand All @@ -820,19 +817,14 @@ class Parser {
/// Consume an identifier or operator if present and return its name
/// in \p Result. Otherwise, emit an error and return true.
bool parseAnyIdentifier(Identifier &Result, SourceLoc &Loc,
const Diagnostic &D);
const Diagnostic &D, bool diagnoseDollarPrefix);

template<typename ...DiagArgTypes, typename ...ArgTypes>
bool parseAnyIdentifier(Identifier &Result, Diag<DiagArgTypes...> ID,
ArgTypes... Args) {
SourceLoc L;
return parseAnyIdentifier(Result, L, Diagnostic(ID, Args...));
}

template<typename ...DiagArgTypes, typename ...ArgTypes>
bool parseAnyIdentifier(Identifier &Result, SourceLoc &L,
bool parseAnyIdentifier(Identifier &Result, bool diagnoseDollarPrefix,
Diag<DiagArgTypes...> ID, ArgTypes... Args) {
return parseAnyIdentifier(Result, L, Diagnostic(ID, Args...));
SourceLoc L;
return parseAnyIdentifier(Result, L, Diagnostic(ID, Args...),
diagnoseDollarPrefix);
}

/// \brief Parse an unsigned integer and returns it in \p Result. On failure
Expand Down
27 changes: 14 additions & 13 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,7 @@ bool Parser::parseDifferentiabilityParametersClause(
return true;
}
Identifier paramName;
if (parseIdentifier(paramName, paramLoc,
diag::diff_params_clause_expected_parameter))
return true;
paramLoc = consumeIdentifier(paramName, /*diagnoseDollarPrefix=*/false);
parameters.push_back(
ParsedAutoDiffParameter::getNamedParameter(paramLoc, paramName));
break;
Expand Down Expand Up @@ -1966,8 +1964,8 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
}

Identifier name;
consumeIdentifier(&name);
consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);

auto range = SourceRange(Loc, Tok.getRange().getStart());

if (!consumeIf(tok::r_paren)) {
Expand Down Expand Up @@ -2545,7 +2543,7 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
}

Identifier name;
consumeIdentifier(&name, /*allowDollarIdentifier=*/true);
consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);

auto range = SourceRange(Loc, Tok.getRange().getStart());

Expand Down Expand Up @@ -4418,6 +4416,7 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
}
importPath.push_back(Identifier(), Tok.getLoc());
if (parseAnyIdentifier(importPath.back().Item,
/*diagnoseDollarPrefix=*/false,
diag::expected_identifier_in_decl, "import"))
return nullptr;
HasNext = consumeIf(tok::period);
Expand Down Expand Up @@ -4543,7 +4542,7 @@ parseIdentifierDeclName(Parser &P, Identifier &Result, SourceLoc &Loc,
StringRef DeclKindName,
llvm::function_ref<bool(const Token &)> canRecover) {
if (P.Tok.is(tok::identifier)) {
Loc = P.consumeIdentifier(&Result);
Loc = P.consumeIdentifier(Result, /*diagnoseDollarPrefix=*/true);

// We parsed an identifier for the declaration. If we see another
// identifier, it might've been a single identifier that got broken by a
Expand Down Expand Up @@ -5457,7 +5456,7 @@ static ParameterList *parseOptionalAccessorArgument(SourceLoc SpecifierLoc,
EndLoc = StartLoc;
} else {
// We have a name.
NameLoc = P.consumeIdentifier(&Name);
NameLoc = P.consumeIdentifier(Name, /*diagnoseDollarPrefix=*/true);

auto DiagID =
Kind == AccessorKind::Set ? diag::expected_rparen_set_name :
Expand Down Expand Up @@ -7737,15 +7736,15 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
SyntaxKind::IdentifierList);

Identifier name;
auto loc = consumeIdentifier(&name);
auto loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
identifiers.emplace_back(name, loc);

while (Tok.is(tok::comma)) {
auto comma = consumeToken();

if (Tok.is(tok::identifier)) {
Identifier name;
auto loc = consumeIdentifier(&name);
auto loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
identifiers.emplace_back(name, loc);
} else {
if (Tok.isNot(tok::eof)) {
Expand All @@ -7762,7 +7761,7 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
SyntaxKind::IdentifierList);

Identifier name;
auto nameLoc = consumeIdentifier(&name);
auto nameLoc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
identifiers.emplace_back(name, nameLoc);

if (isPrefix || isPostfix) {
Expand Down Expand Up @@ -7833,7 +7832,8 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags,

Identifier name;
SourceLoc nameLoc;
if (parseIdentifier(name, nameLoc, diag::expected_precedencegroup_name)) {
if (parseIdentifier(name, nameLoc, /*diagnoseDollarPrefix=*/true,
diag::expected_precedencegroup_name)) {
// If the identifier is missing or a keyword or something, try to
// skip the entire body.
if (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof) &&
Expand Down Expand Up @@ -8039,7 +8039,8 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags,
return abortBody();
}
Identifier name;
SourceLoc nameLoc = consumeIdentifier(&name);
SourceLoc nameLoc = consumeIdentifier(name,
/*diagnoseDollarPrefix=*/false);
relations.push_back({nameLoc, name, nullptr});

if (skipUnspacedCodeCompleteToken())
Expand Down
12 changes: 6 additions & 6 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
peekToken().isNot(tok::period, tok::period_prefix, tok::l_paren)) {
DeferringContextRAII Deferring(*SyntaxContext);
Identifier name;
SourceLoc loc = consumeIdentifier(&name, /*allowDollarIdentifier=*/true);
SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/false);
auto introducer = (InVarOrLetPattern != IVOLP_InVar
? VarDecl::Introducer::Let
: VarDecl::Introducer::Var);
Expand Down Expand Up @@ -2114,8 +2114,7 @@ DeclNameRef Parser::parseDeclNameRef(DeclNameLoc &loc,
SourceLoc baseNameLoc;
if (Tok.isAny(tok::identifier, tok::kw_Self, tok::kw_self)) {
Identifier baseNameId;
baseNameLoc = consumeIdentifier(
&baseNameId, /*allowDollarIdentifier=*/true);
baseNameLoc = consumeIdentifier(baseNameId, /*diagnoseDollarPrefix=*/false);
baseName = baseNameId;
} else if (flags.contains(DeclNameFlag::AllowOperators) &&
Tok.isAnyOperator()) {
Expand Down Expand Up @@ -2490,7 +2489,7 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,

} else {
// Otherwise, the name is a new declaration.
consumeIdentifier(&name);
consumeIdentifier(name, /*diagnoseDollarPrefix=*/true);
equalLoc = consumeToken(tok::equal);

auto ExprResult = parseExpr(diag::expected_init_capture_specifier);
Expand Down Expand Up @@ -2569,7 +2568,7 @@ parseClosureSignatureIfPresent(SourceRange &bracketRange,
Identifier name;
SourceLoc nameLoc;
if (Tok.is(tok::identifier)) {
nameLoc = consumeIdentifier(&name);
nameLoc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/true);
} else {
nameLoc = consumeToken(tok::kw__);
}
Expand Down Expand Up @@ -3233,7 +3232,7 @@ ParserResult<Expr> Parser::parseExprPoundUnknown(SourceLoc LSquareLoc) {
PoundLoc.getAdvancedLoc(1) == Tok.getLoc());

Identifier Name;
SourceLoc NameLoc = consumeIdentifier(&Name);
SourceLoc NameLoc = consumeIdentifier(Name, /*diagnoseDollarPrefix=*/false);

// Parse arguments if exist.
SourceLoc LParenLoc, RParenLoc;
Expand Down Expand Up @@ -3670,6 +3669,7 @@ Parser::parsePlatformVersionConstraintSpec() {
}

if (parseIdentifier(PlatformIdentifier, PlatformLoc,
/*diagnoseDollarPrefix=*/false,
diag::avail_query_expected_platform_name)) {
return nullptr;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Parse/ParseGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
// Parse the name of the parameter.
Identifier Name;
SourceLoc NameLoc;
if (parseIdentifier(Name, NameLoc,
if (parseIdentifier(Name, NameLoc, /*diagnoseDollarPrefix=*/true,
diag::expected_generics_parameter_name)) {
Result.setIsParseError();
break;
Expand Down Expand Up @@ -307,7 +307,8 @@ ParserStatus Parser::parseGenericWhereClause(
->isKnownLayout()) {
// Parse a layout constraint.
Identifier LayoutName;
auto LayoutLoc = consumeIdentifier(&LayoutName);
auto LayoutLoc = consumeIdentifier(LayoutName,
/*diagnoseDollarPrefix=*/false);
auto LayoutInfo = parseLayoutConstraint(LayoutName);
if (!LayoutInfo->isKnownLayout()) {
// There was a bug in the layout constraint.
Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ ParserResult<Pattern> Parser::parsePattern() {
case tok::identifier: {
PatternCtx.setCreateSyntax(SyntaxKind::IdentifierPattern);
Identifier name;
SourceLoc loc = consumeIdentifier(&name);
SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/true);
if (Tok.isIdentifierOrUnderscore() && !Tok.isContextualDeclKeyword())
diagnoseConsecutiveIDs(name.str(), loc,
introducer == VarDecl::Introducer::Let
Expand Down Expand Up @@ -1054,7 +1054,7 @@ Parser::parsePatternTupleElement() {

// If the tuple element has a label, parse it.
if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) {
LabelLoc = consumeIdentifier(&Label);
LabelLoc = consumeIdentifier(Label, /*diagnoseDollarPrefix=*/true);
consumeToken(tok::colon);
}

Expand Down
13 changes: 8 additions & 5 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static bool isAtStartOfSwitchCase(Parser &parser,
backtrack.emplace(parser);

parser.consumeToken(tok::at_sign);
parser.consumeIdentifier();
parser.consumeToken(tok::identifier);
if (parser.Tok.is(tok::l_paren))
parser.skipSingle();
}
Expand Down Expand Up @@ -536,7 +536,8 @@ ParserResult<Stmt> Parser::parseStmt() {
// If this is a label on a loop/switch statement, consume it and pass it into
// parsing logic below.
if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) {
LabelInfo.Loc = consumeIdentifier(&LabelInfo.Name);
LabelInfo.Loc = consumeIdentifier(LabelInfo.Name,
/*diagnoseDollarPrefix=*/true);
consumeToken(tok::colon);
}

Expand Down Expand Up @@ -687,7 +688,7 @@ static ParserStatus parseOptionalControlTransferTarget(Parser &P,
if (!P.Tok.isAtStartOfLine()) {
if (P.Tok.is(tok::identifier) && !P.isStartOfStmt() &&
!P.isStartOfSwiftDecl()) {
TargetLoc = P.consumeIdentifier(&Target);
TargetLoc = P.consumeIdentifier(Target, /*diagnoseDollarPrefix=*/false);
return makeParserSuccess();
} else if (P.Tok.is(tok::code_complete)) {
if (P.CodeCompletion)
Expand Down Expand Up @@ -2486,17 +2487,19 @@ ParserResult<CaseStmt> Parser::parseStmtCase(bool IsActive) {
diagnose(UnknownAttrLoc, diag::previous_attribute, false);
consumeToken(tok::at_sign);
}
consumeIdentifier();
consumeToken(tok::identifier);

SyntaxParsingContext Args(SyntaxContext, SyntaxKind::TokenList);
if (Tok.is(tok::l_paren)) {
diagnose(Tok, diag::unexpected_lparen_in_attribute, "unknown");
skipSingle();
}
} else {
assert(peekToken().is(tok::identifier) && "isAtStartOfSwitchCase() lied");

consumeToken(tok::at_sign);
diagnose(Tok, diag::unknown_attribute, Tok.getText());
consumeIdentifier();
consumeToken(tok::identifier);

SyntaxParsingContext Args(SyntaxContext, SyntaxKind::TokenList);
if (Tok.is(tok::l_paren))
Expand Down
9 changes: 5 additions & 4 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,12 +903,12 @@ bool Parser::StructureMarkerRAII::pushStructureMarker(
//===----------------------------------------------------------------------===//

bool Parser::parseIdentifier(Identifier &Result, SourceLoc &Loc,
const Diagnostic &D) {
const Diagnostic &D, bool diagnoseDollarPrefix) {
switch (Tok.getKind()) {
case tok::kw_self:
case tok::kw_Self:
case tok::identifier:
Loc = consumeIdentifier(&Result);
Loc = consumeIdentifier(Result, diagnoseDollarPrefix);
return false;
default:
checkForInputIncomplete();
Expand All @@ -930,9 +930,10 @@ bool Parser::parseSpecificIdentifier(StringRef expected, SourceLoc &loc,
/// parseAnyIdentifier - Consume an identifier or operator if present and return
/// its name in Result. Otherwise, emit an error and return true.
bool Parser::parseAnyIdentifier(Identifier &Result, SourceLoc &Loc,
const Diagnostic &D) {
const Diagnostic &D,
bool diagnoseDollarPrefix) {
if (Tok.is(tok::identifier)) {
Loc = consumeIdentifier(&Result);
Loc = consumeIdentifier(Result, diagnoseDollarPrefix);
return false;
}

Expand Down
Loading