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

Fix order in which errors are reported for assignment to val #16660

Merged
merged 1 commit into from
Jan 11, 2023
Merged
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i16655.check
Original file line number Diff line number Diff line change
@@ -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`
3 changes: 3 additions & 0 deletions tests/neg/i16655.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test:
val x = "MyString"
x = 5 // error