Skip to content

Commit ed81385

Browse files
authored
Fix order in which errors are reported for assignment to val (#16660)
Fixes #16655
2 parents a9be390 + d1ec6b1 commit ed81385

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
10001000
def lhs1 = adapt(lhsCore, AssignProto, locked)
10011001

10021002
def reassignmentToVal =
1003-
errorTree(cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)),
1004-
ReassignmentToVal(lhsCore.symbol.name))
1003+
report.error(ReassignmentToVal(lhsCore.symbol.name), tree.srcPos)
1004+
cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType)
10051005

10061006
def canAssign(sym: Symbol) =
10071007
sym.is(Mutable, butNot = Accessor) ||

tests/neg/i16655.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E052] Type Error: tests/neg/i16655.scala:3:4 -----------------------------------------------------------------------
2+
3 | x = 5 // error
3+
| ^^^^^
4+
| Reassignment to val x
5+
|
6+
| longer explanation available when compiling with `-explain`

tests/neg/i16655.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test:
2+
val x = "MyString"
3+
x = 5 // error

0 commit comments

Comments
 (0)