Skip to content

Commit edd689e

Browse files
committed
Only parse prefix operators if next token can start expression
Fixes #12396
1 parent 69c800b commit edd689e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,14 +2184,15 @@ object Parsers {
21842184
/** PrefixExpr ::= [`-' | `+' | `~' | `!'] SimpleExpr
21852185
*/
21862186
val prefixExpr: Location => Tree = location =>
2187-
if (isIdent && nme.raw.isUnary(in.name)) {
2187+
if isIdent && nme.raw.isUnary(in.name)
2188+
&& in.canStartExprTokens.contains(in.lookahead.token)
2189+
then
21882190
val start = in.offset
21892191
val op = termIdent()
21902192
if (op.name == nme.raw.MINUS && isNumericLit)
21912193
simpleExprRest(literal(start), location, canApply = true)
21922194
else
21932195
atSpan(start) { PrefixOp(op, simpleExpr(location)) }
2194-
}
21952196
else simpleExpr(location)
21962197

21972198
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]

tests/pos/i12396.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object + {
2+
val x = 1
3+
}
4+
5+
object Check {
6+
val y = +.x
7+
}

0 commit comments

Comments
 (0)