Skip to content

Commit 5939849

Browse files
committed
Allow prefix operators on the LHS of assignments
Fixes #13282
1 parent e4b421c commit 5939849

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ object Parsers {
20452045
def expr1Rest(t: Tree, location: Location): Tree = in.token match
20462046
case EQUALS =>
20472047
t match
2048-
case Ident(_) | Select(_, _) | Apply(_, _) =>
2048+
case Ident(_) | Select(_, _) | Apply(_, _) | PrefixOp(_, _) =>
20492049
atSpan(startOffset(t), in.skipToken()) {
20502050
val loc = if location.inArgs then location else Location.ElseWhere
20512051
Assign(t, subPart(() => expr(loc)))

docs/docs/internals/syntax.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
223223
| ‘return’ [Expr] Return(expr?)
224224
| ForExpr
225225
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
226-
| SimpleExpr1 ArgumentExprs ‘=’ Expr Assign(expr, expr)
226+
| PrefixOperator SimpleExpr ‘=’ Expr Assign(expr, expr)
227+
| SimpleExpr ArgumentExprs ‘=’ Expr Assign(expr, expr)
227228
| PostfixExpr [Ascription]
228229
| ‘inline’ InfixExpr MatchClause
229230
Ascription ::= ‘:’ InfixType Typed(expr, tp)
@@ -235,7 +236,8 @@ InfixExpr ::= PrefixExpr
235236
| InfixExpr id ‘:’ IndentedExpr
236237
| InfixExpr MatchClause
237238
MatchClause ::= ‘match’ <<< CaseClauses >>> Match(expr, cases)
238-
PrefixExpr ::= [‘-’ | ‘+’ | ‘~’ | ‘!’] SimpleExpr PrefixOp(expr, op)
239+
PrefixExpr ::= [PrefixOperator] SimpleExpr PrefixOp(expr, op)
240+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
239241
SimpleExpr ::= SimpleRef
240242
| Literal
241243
| ‘_’
@@ -250,8 +252,8 @@ SimpleExpr ::= SimpleRef
250252
| SimpleExpr ‘.’ MatchClause
251253
| SimpleExpr TypeArgs TypeApply(expr, args)
252254
| SimpleExpr ArgumentExprs Apply(expr, args)
253-
| SimpleExpr1 ‘:’ IndentedExpr -- under language.experimental.fewerBraces
254-
| SimpleExpr1 FunParams (‘=>’ | ‘?=>’) IndentedExpr -- under language.experimental.fewerBraces
255+
| SimpleExpr ‘:’ IndentedExpr -- under language.experimental.fewerBraces
256+
| SimpleExpr FunParams (‘=>’ | ‘?=>’) IndentedExpr -- under language.experimental.fewerBraces
255257
| SimpleExpr ‘_’ PostfixOp(expr, _) (to be dropped)
256258
| XmlExpr -- to be dropped
257259
IndentedExpr ::= indent CaseClauses | Block outdent

docs/docs/reference/syntax.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
221221
| ‘return’ [Expr]
222222
| ForExpr
223223
| [SimpleExpr ‘.’] id ‘=’ Expr
224-
| SimpleExpr1 ArgumentExprs ‘=’ Expr
224+
| PrefixOperator SimpleExpr ‘=’ Expr
225+
| SimpleExpr ArgumentExprs ‘=’ Expr
225226
| PostfixExpr [Ascription]
226227
| ‘inline’ InfixExpr MatchClause
227228
Ascription ::= ‘:’ InfixType
@@ -232,7 +233,8 @@ InfixExpr ::= PrefixExpr
232233
| InfixExpr id [nl] InfixExpr
233234
| InfixExpr MatchClause
234235
MatchClause ::= ‘match’ <<< CaseClauses >>>
235-
PrefixExpr ::= [‘-’ | ‘+’ | ‘~’ | ‘!’] SimpleExpr
236+
PrefixExpr ::= [PrefixOperator] SimpleExpr
237+
PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’
236238
SimpleExpr ::= SimpleRef
237239
| Literal
238240
| ‘_’

tests/pos/i13282.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Ptr[T](var value: T):
2+
def `unary_!` : T = value
3+
def `unary_!_=`(value: T): Unit = this.value = value
4+
end Ptr
5+
6+
def test =
7+
val x = Ptr(9)
8+
!x = 10
9+
println(!x)

0 commit comments

Comments
 (0)