Skip to content

Commit 62a50da

Browse files
committed
Use name from tree when it is a NameTree for the ReassignmentToVal error message
1 parent 8fe16fe commit 62a50da

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
13891389
def lhs1 = adapt(lhsCore, LhsProto, locked)
13901390

13911391
def reassignmentToVal =
1392-
report.error(ReassignmentToVal(lhsCore.symbol.name), tree.srcPos)
1392+
val lhsName = lhsCore match {
1393+
case nameTree: NameTree => nameTree.name
1394+
case _ => lhsCore.symbol.name
1395+
}
1396+
report.error(ReassignmentToVal(lhsName), tree.srcPos)
13931397
cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType)
13941398

13951399
def canAssign(sym: Symbol) =

tests/neg/i22671.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- [E052] Type Error: tests/neg/i22671.scala:6:6 -----------------------------------------------------------------------
2+
6 | X.x = 27 // error
3+
| ^^^^^^^^
4+
| Reassignment to val x
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E052] Type Error: tests/neg/i22671.scala:10:4 ----------------------------------------------------------------------
8+
10 | x = 27 // error
9+
| ^^^^^^
10+
| Reassignment to val x
11+
|
12+
| longer explanation available when compiling with `-explain`
13+
-- [E052] Type Error: tests/neg/i22671.scala:14:4 ----------------------------------------------------------------------
14+
14 | x = 27 // error
15+
| ^^^^^^
16+
| Reassignment to val x
17+
|
18+
| longer explanation available when compiling with `-explain`

tests/neg/i22671.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object X:
2+
def x: Int = 42
3+
def x(y: Int): Int = x + y
4+
5+
def test1 =
6+
X.x = 27 // error
7+
8+
def test2 =
9+
import X.x
10+
x = 27 // error
11+
12+
def test3 =
13+
val x = 42
14+
x = 27 // error

0 commit comments

Comments
 (0)