Skip to content

Fix/#652 erase constructors #675

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 10 commits into from
Jun 22, 2015
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
24 changes: 23 additions & 1 deletion src/dotty/tools/dotc/transform/Constructors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor

val constrStats, clsStats = new mutable.ListBuffer[Tree]

/** Map outer getters $outer and outer accessors $A$B$$$outer to the given outer parameter. */
def mapOuter(outerParam: Symbol) = new TreeMap {
override def transform(tree: Tree)(implicit ctx: Context) = tree match {
case Apply(fn, Nil)
if (fn.symbol.is(OuterAccessor)
|| fn.symbol.isGetter && fn.symbol.name == nme.OUTER
) &&
fn.symbol.info.resultType.classSymbol == outerParam.info.classSymbol =>
ref(outerParam)
case _ =>
super.transform(tree)
}
}

// Split class body into statements that go into constructor and
// definitions that are kept as members of the class.
def splitStats(stats: List[Tree]): Unit = stats match {
Expand All @@ -174,6 +188,8 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
owner = constr.symbol).installAfter(thisTransform)
constrStats += intoConstr(stat, sym)
}
case DefDef(nme.CONSTRUCTOR, _, ((outerParam @ ValDef(nme.OUTER, _, _)) :: _) :: Nil, _, _) =>
clsStats += mapOuter(outerParam.symbol).transform(stat)
case _: DefTree =>
clsStats += stat
case _ =>
Expand Down Expand Up @@ -221,9 +237,15 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
case stats => (Nil, stats)
}

val mappedSuperCalls = vparams match {
case (outerParam @ ValDef(nme.OUTER, _, _)) :: _ =>
superCalls.map(mapOuter(outerParam.symbol).transform)
case _ => superCalls
}

cpy.Template(tree)(
constr = cpy.DefDef(constr)(
rhs = Block(superCalls ::: copyParams ::: followConstrStats, unitLiteral)),
rhs = Block(mappedSuperCalls ::: copyParams ::: followConstrStats, unitLiteral)),
body = clsStats.toList)
}
}
4 changes: 3 additions & 1 deletion src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ object Erasure extends TypeTestsCasts{
val MethodType(pnames, ptypes) = sym.info.resultType
effectiveSym = sym.copy(info = MethodType(pnames, ptypes, defn.ObjectType))
}
val restpe = effectiveSym.info.resultType
val restpe =
if (effectiveSym.isConstructor) defn.UnitType
else effectiveSym.info.resultType
val ddef1 = untpd.cpy.DefDef(ddef)(
tparams = Nil,
vparamss = (outer.paramDefs(effectiveSym) ::: ddef.vparamss.flatten) :: Nil,
Expand Down
17 changes: 15 additions & 2 deletions src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,24 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
val freeParamDefs = ownProxies.map(proxy =>
transformFollowingDeep(ValDef(proxy.asTerm).withPos(tree.pos)).asInstanceOf[ValDef])
def proxyInit(field: Symbol, param: Symbol) =
transformFollowingDeep(ref(field).becomes(ref(param)))
transformFollowingDeep(memberRef(field).becomes(ref(param)))

/** Map references to proxy fields `this.proxy` to proxy parameters */
def mapProxies = new TreeMap {
override def transform(tree: Tree)(implicit ctx: Context) = tree match {
case Select(This(_), _) if proxies contains tree.symbol =>
ref(tree.symbol.subst(proxies, ownProxies))
case _ =>
super.transform(tree)
}
}

/** Initialize proxy fields from proxy parameters and map `rhs` from fields to parameters */
def copyParams(rhs: Tree) = {
ctx.log(i"copy params ${proxies.map(_.showLocated)}%, %, own = ${ownProxies.map(_.showLocated)}%, %")
seq((proxies, ownProxies).zipped.map(proxyInit), rhs)
seq((proxies, ownProxies).zipped.map(proxyInit), mapProxies.transform(rhs))
}

tree match {
case tree: DefDef =>
cpy.DefDef(tree)(
Expand Down
7 changes: 6 additions & 1 deletion src/dotty/tools/dotc/transform/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {
appendOffsetDefs.get(cls) match {
case None => template
case Some(data) =>
cpy.Template(template)(body = data.defs ::: template.body)
cpy.Template(template)(body = addInFront(data.defs, template.body))
}

}

private def addInFront(prefix: List[Tree], stats: List[Tree]) = stats match {
case first :: rest if isSuperConstrCall(first) => first :: prefix ::: rest
case _ => prefix ::: stats
}

/** Replace a local lazy val inside a method,
* with a LazyHolder from
* dotty.runtime(eg dotty.runtime.LazyInt)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class A(x: Int, y: Int) {
def this(x: Int) = this(x, x);
def this() = this(1);
override def toString() = "x=" + x + " y=" + y;
class B(a: Int, b: Int, c: String) {
class B(val a: Int, b: Int, c: String) {
def this(str: String) = this(x, y, str);
val xx = a
override def toString() =
"x=" + x + " y=" + y + " a=" + a + " b=" + b + " c=" + c;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
bippity.bop.Foo
bippity.bop.Foo$Bar
bippity.bop.Foo$Bar$
Test$$anon$1
Test$$anon$2
Test$$anon$
Test$$anon$
Foo
Bar
Bar$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ object Test {
import bippity._
import bop._

def printSanitized(x: String) = println(x.filterNot(_.isDigit))

def main(args: Array[String]): Unit = {
val f = new Foo
val instances = List(f, new f.Bar, f.Bar, new Foo with DingDongBippy, new f.Bar with DingDongBippy)
instances map (_.getClass.getName) foreach println
instances map shortClassOfInstance foreach println
instances map (_.getClass.getName) foreach printSanitized
instances map shortClassOfInstance foreach printSanitized
}
}
File renamed without changes.
File renamed without changes.