Skip to content

Commit 5015425

Browse files
authored
Merge pull request #15198 from adampauls/unary_type_param
Parse unary operators as regular identifiers when backquoted
2 parents 0260d75 + cfd2760 commit 5015425

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
@@ -2293,10 +2293,10 @@ object Parsers {
22932293
isOperator = !(location.inArgs && followingIsVararg()))
22942294

22952295
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
2296-
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
2296+
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
22972297
*/
22982298
val prefixExpr: Location => Tree = location =>
2299-
if isIdent && nme.raw.isUnary(in.name)
2299+
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
23002300
&& in.canStartExprTokens.contains(in.lookahead.token)
23012301
then
23022302
val start = in.offset

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ InfixExpr ::= PrefixExpr
254254
| InfixExpr MatchClause
255255
MatchClause ::= ‘match’ <<< CaseClauses >>> Match(expr, cases)
256256
PrefixExpr ::= [PrefixOperator] SimpleExpr PrefixOp(expr, op)
257-
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
257+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted
258258
SimpleExpr ::= SimpleRef
259259
| Literal
260260
| ‘_’

docs/_docs/reference/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ InfixExpr ::= PrefixExpr
252252
| InfixExpr MatchClause
253253
MatchClause ::= ‘match’ <<< CaseClauses >>>
254254
PrefixExpr ::= [PrefixOperator] SimpleExpr
255-
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
255+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted
256256
SimpleExpr ::= SimpleRef
257257
| Literal
258258
| ‘_’
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 parenthesized argument
5+
+(6): Int // Parser prioritizes + as unary when possible
6+
}

0 commit comments

Comments
 (0)