Skip to content

Commit 14b1dd5

Browse files
committed
Extend "cannot override mutable variable" restriction to deferred variables.
Extend "cannot override mutable variable" restriction also to deferred variables. This aligns the behavior with Scala 2. Fixes #14722
1 parent 5587767 commit 14b1dd5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,11 @@ object RefChecks {
474474
overrideError("needs `override` modifier")
475475
else if (other.is(AbsOverride) && other.isIncompleteIn(clazz) && !member.is(AbsOverride))
476476
overrideError("needs `abstract override` modifiers")
477-
else if member.is(Override) && other.is(Accessor, butNot = Deferred) && other.accessedFieldOrGetter.is(Mutable, butNot = Lazy) then
477+
else if member.is(Override)
478+
&& (other.is(Mutable)
479+
|| other.is(Accessor, butNot = Deferred)
480+
&& other.accessedFieldOrGetter.is(Mutable, butNot = Lazy))
481+
then
478482
overrideError("cannot override a mutable variable")
479483
else if (member.isAnyOverride &&
480484
!(member.owner.thisType.baseClasses exists (_ isSubClass other.owner)) &&

tests/neg/i14722.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
abstract class HasId(var id: String)
2+
3+
case class Entity(override val id: String) extends HasId(id) // error
4+
5+
object Test extends App {
6+
val entity = Entity("0001")
7+
entity.id = "0002"
8+
println(entity.id)
9+
}

0 commit comments

Comments
 (0)