Skip to content

Commit 4962754

Browse files
committed
Use parseDeclNameRef in a few more places
1 parent ee9c0cc commit 4962754

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,5 +2183,9 @@ ERROR(nonisolated_nonsending_expected_rparen,PointsToFirstBadToken,
21832183
ERROR(nonisolated_nonsending_repeated,none,
21842184
"parameter may have at most one 'nonisolated(nonsending)' specifier", ())
21852185

2186+
ERROR(impossible_parse,none,
2187+
"parser entered impossible state; please file a bug report with this "
2188+
"source file", ())
2189+
21862190
#define UNDEFINE_DIAGNOSTIC_MACROS
21872191
#include "DefineDiagnosticMacros.h"

include/swift/Parse/Parser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,13 +1819,19 @@ class Parser {
18191819
/// cases this doesn't actually make sense but we need to accept them for
18201820
/// backwards compatibility.
18211821
AllowLowercaseAndUppercaseSelf = 1 << 6,
1822+
1823+
/// If passed, `$0` etc. are allowed.
1824+
AllowAnonymousParamNames = 1 << 7,
18221825
};
18231826
using DeclNameOptions = OptionSet<DeclNameFlag>;
18241827

18251828
friend DeclNameOptions operator|(DeclNameFlag flag1, DeclNameFlag flag2) {
18261829
return DeclNameOptions(flag1) | flag2;
18271830
}
18281831

1832+
/// Parse a declaration name that results in a `DeclNameRef` in the syntax
1833+
/// tree.
1834+
///
18291835
/// Without \c DeclNameFlag::AllowCompoundNames, parse an
18301836
/// unqualified-decl-base-name.
18311837
///

lib/Parse/ParseDecl.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10108,8 +10108,9 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1010810108
// designated protocol. These both look like identifiers, so we
1010910109
// parse them both as identifiers here and sort it out in type
1011010110
// checking.
10111-
SourceLoc colonLoc, groupLoc;
10112-
Identifier groupName;
10111+
SourceLoc colonLoc;
10112+
DeclNameLoc groupLoc;
10113+
DeclNameRef groupName;
1011310114
if (Tok.is(tok::colon)) {
1011410115
colonLoc = consumeToken();
1011510116
if (Tok.is(tok::code_complete)) {
@@ -10122,9 +10123,9 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1012210123
return makeParserCodeCompletionResult<OperatorDecl>();
1012310124
}
1012410125

10125-
(void)parseIdentifier(groupName, groupLoc,
10126-
diag::operator_decl_expected_precedencegroup,
10127-
/*diagnoseDollarPrefix=*/false);
10126+
groupName = parseDeclNameRef(groupLoc,
10127+
diag::operator_decl_expected_precedencegroup,
10128+
{});
1012810129

1012910130
if (Context.TypeCheckerOpts.EnableOperatorDesignatedTypes) {
1013010131
// Designated types have been removed; consume the list (mainly for source
@@ -10139,7 +10140,7 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1013910140
// These have no precedence group, so we already parsed the first entry
1014010141
// in the designated types list. Retroactively include it in the range.
1014110142
typesStartLoc = colonLoc;
10142-
typesEndLoc = groupLoc;
10143+
typesEndLoc = groupLoc.getEndLoc();
1014310144
}
1014410145

1014510146
while (Tok.isNot(tok::eof)) {
@@ -10158,7 +10159,7 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1015810159
} else {
1015910160
if (isPrefix || isPostfix) {
1016010161
// If we have nothing after the colon, then just remove the colon.
10161-
auto endLoc = groupLoc.isValid() ? groupLoc : colonLoc;
10162+
auto endLoc = groupLoc.isValid() ? groupLoc.getEndLoc() : colonLoc;
1016210163
diagnose(colonLoc, diag::precedencegroup_not_infix)
1016310164
.fixItRemove({colonLoc, endLoc});
1016410165
}
@@ -10169,14 +10170,14 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1016910170
}
1017010171

1017110172
// Diagnose deprecated operator body syntax `operator + { ... }`.
10173+
SourceLoc lastGoodLoc = PreviousLoc;
1017210174
SourceLoc lBraceLoc;
1017310175
if (consumeIf(tok::l_brace, lBraceLoc)) {
1017410176
if (isInfix && !Tok.is(tok::r_brace)) {
1017510177
diagnose(lBraceLoc, diag::deprecated_operator_body_use_group);
1017610178
} else {
1017710179
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
1017810180
if (Tok.is(tok::r_brace)) {
10179-
SourceLoc lastGoodLoc = groupLoc.isValid() ? groupLoc : NameLoc;
1018010181
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
1018110182
lastGoodLoc);
1018210183
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
@@ -10198,7 +10199,8 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
1019810199
else
1019910200
res = new (Context)
1020010201
InfixOperatorDecl(CurDeclContext, OperatorLoc, Name, NameLoc, colonLoc,
10201-
groupName, groupLoc);
10202+
groupName.getBaseIdentifier(),
10203+
groupLoc.getBaseNameLoc());
1020210204

1020310205
diagnoseOperatorFixityAttributes(*this, Attributes, res);
1020410206

@@ -10417,14 +10419,16 @@ Parser::parseDeclPrecedenceGroup(ParseDeclOptions flags,
1041710419
return abortBody(/*hasCodeCompletion*/true);
1041810420
}
1041910421

10420-
if (Tok.isNot(tok::identifier)) {
10421-
diagnose(Tok, diag::expected_precedencegroup_relation, attrName);
10422+
DeclNameLoc nameLoc;
10423+
auto name = parseDeclNameRef(nameLoc,
10424+
{ diag::expected_precedencegroup_relation,
10425+
attrName },
10426+
{});
10427+
if (!name) {
1042210428
return abortBody();
1042310429
}
10424-
Identifier name;
10425-
SourceLoc nameLoc = consumeIdentifier(name,
10426-
/*diagnoseDollarPrefix=*/false);
10427-
relations.push_back({nameLoc, name, nullptr});
10430+
relations.push_back({nameLoc.getBaseNameLoc(), name.getBaseIdentifier(),
10431+
nullptr});
1042810432

1042910433
if (skipUnspacedCodeCompleteToken())
1043010434
return abortBody(/*hasCodeCompletion*/true);

lib/Parse/ParseExpr.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,10 @@ DeclNameRef Parser::parseDeclNameRef(DeclNameLoc &loc,
22562256
Identifier baseNameId;
22572257
baseNameLoc = consumeIdentifier(baseNameId, /*diagnoseDollarPrefix=*/false);
22582258
baseName = baseNameId;
2259+
} else if (flags.contains(DeclNameFlag::AllowAnonymousParamNames)
2260+
&& Tok.is(tok::dollarident)) {
2261+
baseName = Context.getIdentifier(Tok.getText());
2262+
baseNameLoc = consumeToken(tok::dollarident);
22592263
} else if (flags.contains(DeclNameFlag::AllowOperators) &&
22602264
Tok.isAnyOperator()) {
22612265
baseName = Context.getIdentifier(Tok.getText());
@@ -3040,8 +3044,13 @@ ParserResult<Expr> Parser::parseExprClosure() {
30403044
/// expr-anon-closure-argument:
30413045
/// dollarident
30423046
Expr *Parser::parseExprAnonClosureArg() {
3043-
StringRef Name = Tok.getText();
3044-
SourceLoc Loc = consumeToken(tok::dollarident);
3047+
DeclNameLoc nameLoc;
3048+
DeclNameRef nameRef =
3049+
parseDeclNameRef(nameLoc, diag::impossible_parse,
3050+
DeclNameFlag::AllowAnonymousParamNames);
3051+
3052+
StringRef Name = nameRef.getBaseIdentifier().str();
3053+
SourceLoc Loc = nameLoc.getBaseNameLoc();
30453054
assert(Name[0] == '$' && "Not a dollarident");
30463055

30473056
// We know from the lexer that this is all-numeric.

test/Parse/operator_decl.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ precedencegroup D {
8383
precedencegroup E {
8484
higherThan:
8585
} // expected-error {{expected name of related precedence group after 'higherThan'}}
86+
precedencegroup EE {
87+
higherThan: E,
88+
} // expected-error {{expected name of related precedence group after 'higherThan'}}
8689

8790
precedencegroup F {
8891
higherThan: A, B, C

0 commit comments

Comments
 (0)