From d45fbe898b249643563576ba8a436dd8ece29b98 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 13 Oct 2021 12:07:59 -0700 Subject: [PATCH] Can mark var impl as override --- compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 3 +-- tests/pos/i13019.scala | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i13019.scala diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 615e2915f6f8..4c20f5821ca4 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -466,8 +466,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) && - other.accessedFieldOrGetter.is(Mutable, butNot = Lazy)) + else if member.is(Override) && other.is(Accessor, butNot = Deferred) && other.accessedFieldOrGetter.is(Mutable, butNot = Lazy) then overrideError("cannot override a mutable variable") else if (member.isAnyOverride && !(member.owner.thisType.baseClasses exists (_ isSubClass other.owner)) && diff --git a/tests/pos/i13019.scala b/tests/pos/i13019.scala new file mode 100644 index 000000000000..491b6df03d0b --- /dev/null +++ b/tests/pos/i13019.scala @@ -0,0 +1,13 @@ + +trait Ok1 { var i: Int } +class Ok1C extends Ok1 { var i: Int = 1 } + +trait Ok2 { + def i: Int + def i_=(v: Int): Unit +} +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 }