Skip to content

Commit 290f4c9

Browse files
committed
Parse unary operators as regular identifiers when backquoted and followed by square bracket.
1 parent bb29e20 commit 290f4c9

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,10 +2220,10 @@ object Parsers {
22202220
isOperator = !(location.inArgs && followingIsVararg()))
22212221

22222222
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
2223-
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
2223+
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
22242224
*/
22252225
val prefixExpr: Location => Tree = location =>
2226-
if isIdent && nme.raw.isUnary(in.name)
2226+
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
22272227
&& in.canStartExprTokens.contains(in.lookahead.token)
22282228
then
22292229
val start = in.offset

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ InfixExpr ::= PrefixExpr
238238
| InfixExpr MatchClause
239239
MatchClause ::= ‘match’ <<< CaseClauses >>> Match(expr, cases)
240240
PrefixExpr ::= [PrefixOperator] SimpleExpr PrefixOp(expr, op)
241-
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
241+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted
242242
SimpleExpr ::= SimpleRef
243243
| Literal
244244
| ‘_’

docs/_docs/reference/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ InfixExpr ::= PrefixExpr
236236
| InfixExpr MatchClause
237237
MatchClause ::= ‘match’ <<< CaseClauses >>>
238238
PrefixExpr ::= [PrefixOperator] SimpleExpr
239-
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
239+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted
240240
SimpleExpr ::= SimpleRef
241241
| Literal
242242
| ‘_’
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
def +[T](x: T): String = "x"
3+
+[Int](6): String // error: expression expected but '[' found
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def +[T](x: T): String = "x"
3+
`+`[Int](6): String // Parser can treat + as identifier when backquoted and followed by a type argument
4+
`+`(6): String // Parser can treat + as identifier when backquoted and followed by a type argument
5+
+(6): Int // Parser prioritizes + as unary when possible
6+
}

0 commit comments

Comments
 (0)