@@ -78,6 +78,10 @@ extension Parser {
7878 ///
7979 /// We don't allow allow newlines here.
8080 case poundIfDirective
81+
82+ /// Parsing an attribute's arguments, which can contain declaration
83+ /// references like `subscript` or `deinit`.
84+ case attributeArguments
8185 }
8286
8387 enum PatternContext {
@@ -725,7 +729,7 @@ extension Parser {
725729
726730 // If there is an expr-call-suffix, parse it and form a call.
727731 if let lparen = self . consume ( if: TokenSpec ( . leftParen, allowAtStartOfLine: false ) ) {
728- let args = self . parseArgumentListElements ( pattern: pattern)
732+ let args = self . parseArgumentListElements ( pattern: pattern, flavor : flavor . callArgumentFlavor )
729733 let ( unexpectedBeforeRParen, rparen) = self . expect ( . rightParen)
730734
731735 // If we can parse trailing closures, do so.
@@ -1135,9 +1139,9 @@ extension Parser {
11351139 return RawExprSyntax ( RawPatternExprSyntax ( pattern: pattern, arena: self . arena) )
11361140 }
11371141
1138- return RawExprSyntax ( self . parseIdentifierExpression ( ) )
1142+ return RawExprSyntax ( self . parseIdentifierExpression ( flavor : flavor ) )
11391143 case ( . Self, _) ? : // Self
1140- return RawExprSyntax ( self . parseIdentifierExpression ( ) )
1144+ return RawExprSyntax ( self . parseIdentifierExpression ( flavor : flavor ) )
11411145 case ( . Any, _) ? : // Any
11421146 let anyType = RawTypeSyntax ( self . parseAnyType ( ) )
11431147 return RawExprSyntax ( RawTypeExprSyntax ( type: anyType, arena: self . arena) )
@@ -1237,8 +1241,14 @@ extension Parser {
12371241
12381242extension Parser {
12391243 /// Parse an identifier as an expression.
1240- mutating func parseIdentifierExpression( ) -> RawExprSyntax {
1241- let declName = self . parseDeclReferenceExpr ( . compoundNames)
1244+ mutating func parseIdentifierExpression( flavor: ExprFlavor ) -> RawExprSyntax {
1245+ var options : DeclNameOptions = . compoundNames
1246+ switch flavor {
1247+ case . basic, . poundIfDirective, . stmtCondition: break
1248+ case . attributeArguments: options. insert ( . keywordsUsingSpecialNames)
1249+ }
1250+
1251+ let declName = self . parseDeclReferenceExpr ( options)
12421252 guard self . withLookahead ( { $0. canParseAsGenericArgumentList ( ) } ) else {
12431253 return RawExprSyntax ( declName)
12441254 }
@@ -1689,7 +1699,7 @@ extension Parser {
16891699 name = nil
16901700 unexpectedBeforeEqual = nil
16911701 equal = nil
1692- expression = RawExprSyntax ( self . parseIdentifierExpression ( ) )
1702+ expression = RawExprSyntax ( self . parseIdentifierExpression ( flavor : . basic ) )
16931703 }
16941704
16951705 keepGoing = self . consume ( if: . comma)
@@ -1833,7 +1843,7 @@ extension Parser {
18331843 ///
18341844 /// This is currently the same as parsing a tuple expression. In the future,
18351845 /// this will be a dedicated argument list type.
1836- mutating func parseArgumentListElements( pattern: PatternContext ) -> [ RawLabeledExprSyntax ] {
1846+ mutating func parseArgumentListElements( pattern: PatternContext , flavor : ExprFlavor = . basic ) -> [ RawLabeledExprSyntax ] {
18371847 if let remainingTokens = remainingTokensIfMaximumNestingLevelReached ( ) {
18381848 return [
18391849 RawLabeledExprSyntax (
@@ -1878,7 +1888,7 @@ extension Parser {
18781888 if self . at ( . binaryOperator) && self . peek ( isAt: . comma, . rightParen, . rightSquare) {
18791889 expr = RawExprSyntax ( self . parseDeclReferenceExpr ( . operators) )
18801890 } else {
1881- expr = self . parseExpression ( flavor: . basic , pattern: pattern)
1891+ expr = self . parseExpression ( flavor: flavor , pattern: pattern)
18821892 }
18831893 keepGoing = self . consume ( if: . comma)
18841894 result. append (
@@ -2548,3 +2558,14 @@ extension SyntaxKind {
25482558 }
25492559 }
25502560}
2561+
2562+ private extension Parser . ExprFlavor {
2563+ /// The expression flavor used for the argument of a call that occurs
2564+ /// within a particularly-flavored expression.
2565+ var callArgumentFlavor : Parser . ExprFlavor {
2566+ switch self {
2567+ case . basic, . poundIfDirective, . stmtCondition: return . basic
2568+ case . attributeArguments: return . attributeArguments
2569+ }
2570+ }
2571+ }
0 commit comments