Skip to content

Commit bfb6d7d

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

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,7 @@ object Parsers {
22232223
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
22242224
*/
22252225
val prefixExpr: Location => Tree = location =>
2226-
if isIdent && nme.raw.isUnary(in.name)
2226+
if in.isNonBackquotedIdent && nme.raw.isUnary(in.name)
22272227
&& in.canStartExprTokens.contains(in.lookahead.token)
22282228
then
22292229
val start = in.offset

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ object Scanners {
7070
def isNewLine = token == NEWLINE || token == NEWLINES
7171
def isStatSep = isNewLine || token == SEMI
7272
def isIdent = token == IDENTIFIER || token == BACKQUOTED_IDENT
73+
def isNonBackquotedIdent = token == IDENTIFIER
7374
def isIdent(name: Name) = token == IDENTIFIER && this.name == name
7475

7576
def isNestedStart = token == LBRACE || token == INDENT
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)