From d1ec6b1ffe0ac99ee38ac8998b3c28c57340e6d6 Mon Sep 17 00:00:00 2001 From: odersky Date: Wed, 11 Jan 2023 11:44:58 +0100 Subject: [PATCH] Fix order in which errors are reported for assignment to val Fixes #16655 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 4 ++-- tests/neg/i16655.check | 6 ++++++ tests/neg/i16655.scala | 3 +++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i16655.check create mode 100644 tests/neg/i16655.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9e76d57c763c..646295451c95 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1000,8 +1000,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer def lhs1 = adapt(lhsCore, AssignProto, locked) def reassignmentToVal = - errorTree(cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)), - ReassignmentToVal(lhsCore.symbol.name)) + report.error(ReassignmentToVal(lhsCore.symbol.name), tree.srcPos) + cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType) def canAssign(sym: Symbol) = sym.is(Mutable, butNot = Accessor) || diff --git a/tests/neg/i16655.check b/tests/neg/i16655.check new file mode 100644 index 000000000000..e1335b624244 --- /dev/null +++ b/tests/neg/i16655.check @@ -0,0 +1,6 @@ +-- [E052] Type Error: tests/neg/i16655.scala:3:4 ----------------------------------------------------------------------- +3 | x = 5 // error + | ^^^^^ + | Reassignment to val x + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i16655.scala b/tests/neg/i16655.scala new file mode 100644 index 000000000000..c758678d9896 --- /dev/null +++ b/tests/neg/i16655.scala @@ -0,0 +1,3 @@ +object Test: + val x = "MyString" + x = 5 // error