Skip to content

Fix #1263: Suppress super initializer call for val parameters of traits. #1410

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

Merged
merged 2 commits into from
Jul 28, 2016
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
14 changes: 7 additions & 7 deletions src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
}
}

def wasDeferred(sym: Symbol) =
ctx.atPhase(thisTransform) { implicit ctx => sym is Deferred }
def was(sym: Symbol, flags: FlagSet) =
ctx.atPhase(thisTransform) { implicit ctx => sym is flags }

def traitInits(mixin: ClassSymbol): List[Tree] = {
var argNum = 0
Expand All @@ -202,7 +202,7 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
EmptyTree
}

for (getter <- mixin.info.decls.filter(getr => getr.isGetter && !wasDeferred(getr)).toList) yield {
for (getter <- mixin.info.decls.toList if getter.isGetter && !was(getter, Deferred)) yield {
val isScala2x = mixin.is(Scala2x)
def default = Underscore(getter.info.resultType)
def initial = transformFollowing(superRef(initializer(getter)).appliedToNone)
Expand All @@ -220,23 +220,23 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>

if (isCurrent(getter) || getter.is(ExpandedName)) {
val rhs =
if (ctx.atPhase(thisTransform)(implicit ctx => getter.is(ParamAccessor))) nextArgument()
if (was(getter, ParamAccessor)) nextArgument()
else if (isScala2x)
if (getter.is(Lazy, butNot = Module)) lazyGetterCall
else if (getter.is(Module))
New(getter.info.resultType, List(This(cls)))
else Underscore(getter.info.resultType)
else transformFollowing(superRef(initializer(getter)).appliedToNone)
else initial
// transformFollowing call is needed to make memoize & lazy vals run
transformFollowing(DefDef(implementation(getter.asTerm), rhs))
}
else if (isScala2x) EmptyTree
else if (isScala2x || was(getter, ParamAccessor)) EmptyTree
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the only semantic change on this line. The rest are just refactorings.

else initial
}
}

def setters(mixin: ClassSymbol): List[Tree] =
for (setter <- mixin.info.decls.filter(setr => setr.isSetter && !wasDeferred(setr)).toList)
for (setter <- mixin.info.decls.filter(setr => setr.isSetter && !was(setr, Deferred)).toList)
yield transformFollowing(DefDef(implementation(setter.asTerm), unitLiteral.withPos(cls.pos)))

cpy.Template(impl)(
Expand Down
34 changes: 34 additions & 0 deletions tests/run/i1263.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
object Test {
trait Foo(val s: String)

val foo1 = new Foo("bar") {}
val foo2 = new Foo { override val s = "bar" }
def main(args: Array[String]): Unit = {
assert(foo1.s == "bar")
assert(foo2.s == "bar")
}
}
object Test1 {
trait Foo(private val s0: String) {
def s = s0
}

val foo1 = new Foo("bar") {}
def main(args: Array[String]): Unit = {
assert(foo1.s == "bar")
}
}
object Test2 {
trait Foo(protected val s: String)

val foo1 = new Foo("bar") {}
val foo2 = new Foo { override val s = "bar" }
}
object Test3 {
trait Foo(final val s: String)

val foo1 = new Foo("bar") {}
def main(args: Array[String]): Unit = {
assert(foo1.s == "bar")
}
}