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

Extend "cannot override mutable variable" restriction to deferred var… #14724

Merged
merged 2 commits into from
Mar 21, 2022
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this breaks it means we're not really aligned with Scala 2 and we need to re-open #13019 /cc @som-snytt . The Scala 2 logic here is scala/scala@9b59f5f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tweak I didn't try trait params. I'll take another look at a fix. I haven't taken the trait params coursera yet.

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