Skip to content

Commit

Permalink
Merge pull request #14724 from dotty-staging/fix-14722
Browse files Browse the repository at this point in the history
Extend "cannot override mutable variable" restriction to deferred var…
  • Loading branch information
smarter authored Mar 21, 2022
2 parents 58b59a5 + 1a5b858 commit 84fc139
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ object RefChecks {
overrideError("needs `override` modifier")
else if (other.is(AbsOverride) && other.isIncompleteIn(clazz) && !member.is(AbsOverride))
overrideError("needs `abstract override` modifiers")
else if member.is(Override) && other.is(Accessor, butNot = Deferred) && other.accessedFieldOrGetter.is(Mutable, butNot = Lazy) then
else if member.is(Override) && other.is(Mutable) then
overrideError("cannot override a mutable variable")
else if (member.isAnyOverride &&
!(member.owner.thisType.baseClasses exists (_ isSubClass other.owner)) &&
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i13019.scala → tests/neg/i13019.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class Ok2C extends Ok2 { override var i: Int = 1 }

// was: variable i of type Int cannot override a mutable variable
trait NotOk {var i: Int}
class NotOkC extends NotOk { override var i: Int = 1 }
class NotOkC extends NotOk { override var i: Int = 1 } // error
20 changes: 20 additions & 0 deletions tests/neg/i14722.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
abstract class HasId(var id: String)

case class Entity(override val id: String) extends HasId(id) // error

object Test extends App {
val entity = Entity("0001")
entity.id = "0002"
println(entity.id)
}

trait HasId2:
var id: String = ""

case class Entity2(override val id: String) extends HasId2 // error

trait HasId3:
def id: String
def id_=(x: String): Unit

case class Entity3(override var id: String) extends HasId3 // ok

0 comments on commit 84fc139

Please sign in to comment.