Skip to content

Unintended mixin initializer for overriden lazy val #4559

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

Closed
gsps opened this issue May 22, 2018 · 0 comments
Closed

Unintended mixin initializer for overriden lazy val #4559

gsps opened this issue May 22, 2018 · 0 comments
Assignees

Comments

@gsps
Copy link
Contributor

gsps commented May 22, 2018

The Mixin phase currently emits initializer calls for lazy vals in mixed in traits, even if those fields were overridden:

trait A {
  lazy val x = { println("super[A].x()"); 123 }
}

class B extends A {
  override lazy val x = 456
}

object HelloWorld { def main(args: Array[String]): Unit = { new B() } }

results in output

super[A].x()

My ad-hoc fix is to check for override during the generation of those initializers, though I'm not sure that's the correct way to handle it:

diff --git a/compiler/src/dotty/tools/dotc/transform/Mixin.scala b/compiler/src/dotty/tools/dotc/transform/Mixin.scala
index 437a8a45af3..d10ccebb370 100644
--- a/compiler/src/dotty/tools/dotc/transform/Mixin.scala
+++ b/compiler/src/dotty/tools/dotc/transform/Mixin.scala
@@ -213,8 +213,10 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
               cls.pos)
           EmptyTree
       }
+      def needsInit(getter: Symbol) =
+        getter.isGetter && !was(getter, Deferred) && !cls.info.decl(getter.name).symbol.is(Override)
 
-      for (getter <- mixin.info.decls.toList if getter.isGetter && !was(getter, Deferred)) yield {
+      for (getter <- mixin.info.decls.toList if needsInit(getter)) yield {
         val isScala2x = mixin.is(Scala2x)
         def default = Underscore(getter.info.resultType)
         def initial = transformFollowing(superRef(initializer(getter)).appliedToNone)
@odersky odersky self-assigned this May 28, 2018
liufengyun added a commit that referenced this issue Jun 26, 2018
Fix #4559: Don't force initializers of lazy mixin fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants