diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index bdf19ac7d013..35f4f5c0a138 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2293,10 +2293,10 @@ object Parsers { isOperator = !(location.inArgs && followingIsVararg())) /** PrefixExpr ::= [PrefixOperator'] SimpleExpr - * PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ + * PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted) */ val prefixExpr: Location => Tree = location => - if isIdent && nme.raw.isUnary(in.name) + if in.token == IDENTIFIER && nme.raw.isUnary(in.name) && in.canStartExprTokens.contains(in.lookahead.token) then val start = in.offset diff --git a/docs/_docs/internals/syntax.md b/docs/_docs/internals/syntax.md index 35ada397a0d3..729d29ffec4a 100644 --- a/docs/_docs/internals/syntax.md +++ b/docs/_docs/internals/syntax.md @@ -254,7 +254,7 @@ InfixExpr ::= PrefixExpr | InfixExpr MatchClause MatchClause ::= ‘match’ <<< CaseClauses >>> Match(expr, cases) PrefixExpr ::= [PrefixOperator] SimpleExpr PrefixOp(expr, op) -PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ +PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted SimpleExpr ::= SimpleRef | Literal | ‘_’ diff --git a/docs/_docs/reference/syntax.md b/docs/_docs/reference/syntax.md index c26354bd6a25..166cb04c2c86 100644 --- a/docs/_docs/reference/syntax.md +++ b/docs/_docs/reference/syntax.md @@ -252,7 +252,7 @@ InfixExpr ::= PrefixExpr | InfixExpr MatchClause MatchClause ::= ‘match’ <<< CaseClauses >>> PrefixExpr ::= [PrefixOperator] SimpleExpr -PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ +PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ -- unless backquoted SimpleExpr ::= SimpleRef | Literal | ‘_’ diff --git a/tests/neg/unary-with-type-params.scala b/tests/neg/unary-with-type-params.scala new file mode 100644 index 000000000000..64465459e893 --- /dev/null +++ b/tests/neg/unary-with-type-params.scala @@ -0,0 +1,4 @@ +object Test { + def +[T](x: T): String = "x" + +[Int](6): String // error: expression expected but '[' found +} diff --git a/tests/pos/unary-with-type-params.scala b/tests/pos/unary-with-type-params.scala new file mode 100644 index 000000000000..4168852e7c4b --- /dev/null +++ b/tests/pos/unary-with-type-params.scala @@ -0,0 +1,6 @@ +object Test { + def +[T](x: T): String = "x" + `+`[Int](6): String // Parser can treat + as identifier when backquoted and followed by a type argument + `+`(6): String // Parser can treat + as identifier when backquoted and followed by a parenthesized argument + +(6): Int // Parser prioritizes + as unary when possible +}