Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better selection of name for ReassignmentToVal error message #22823

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
def lhs1 = adapt(lhsCore, LhsProto, locked)

def reassignmentToVal =
report.error(ReassignmentToVal(lhsCore.symbol.name), tree.srcPos)
val lhsName = lhsCore match {
case nameTree: NameTree => nameTree.name
case _ => lhsCore.symbol.name
}
report.error(ReassignmentToVal(lhsName), tree.srcPos)
cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)).withType(defn.UnitType)

def canAssign(sym: Symbol) =
Expand Down
18 changes: 18 additions & 0 deletions tests/neg/i22671.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- [E052] Type Error: tests/neg/i22671.scala:6:6 -----------------------------------------------------------------------
6 | X.x = 27 // error
| ^^^^^^^^
| Reassignment to val x
|
| longer explanation available when compiling with `-explain`
-- [E052] Type Error: tests/neg/i22671.scala:10:4 ----------------------------------------------------------------------
10 | x = 27 // error
| ^^^^^^
| Reassignment to val x
|
| longer explanation available when compiling with `-explain`
-- [E052] Type Error: tests/neg/i22671.scala:14:4 ----------------------------------------------------------------------
14 | x = 27 // error
| ^^^^^^
| Reassignment to val x
|
| longer explanation available when compiling with `-explain`
14 changes: 14 additions & 0 deletions tests/neg/i22671.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object X:
def x: Int = 42
def x(y: Int): Int = x + y

def test1 =
X.x = 27 // error

def test2 =
import X.x
x = 27 // error

def test3 =
val x = 42
x = 27 // error
Loading