Skip to content

Commit 44de114

Browse files
committed
Support param accessors that override concrete trait vals.
1 parent 1128a98 commit 44de114

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/src/dotty/tools/dotc/transform/Memoize.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ class Memoize extends MiniPhase with IdentityDenotTransformer { thisPhase =>
150150
// When transforming the getter, we determined that no field was needed.
151151
// In that case we can keep the setter as is, with a () rhs.
152152
tree
153+
else if field.getter.is(ParamAccessor, butNot = Mutable) then
154+
// This is a trait setter (because not Mutable) for a param accessor.
155+
// We must keep the () rhs of the trait setter, otherwise the value
156+
// inherited from the trait will overwrite the value of the parameter.
157+
// See tests/run/traitValOverriddenByParamAccessor.scala
158+
tree
153159
else
154160
field.setFlag(Mutable) // Necessary for vals mixed in from traits
155161
val initializer =
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Foo {
2+
val someField: Int = 5
3+
}
4+
5+
class SimpleFoo extends Foo
6+
7+
class Bar(override val someField: Int) extends Foo
8+
9+
object Test {
10+
def main(args: Array[String]): Unit = {
11+
val simpleFoo = new SimpleFoo
12+
assert(simpleFoo.someField == 5)
13+
14+
val bar = new Bar(44)
15+
assert(bar.someField == 44)
16+
}
17+
}

0 commit comments

Comments
 (0)