diff options
author | Martin Odersky <odersky@gmail.com> | 2015-06-19 16:15:04 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2015-06-19 16:15:04 +0200 |
commit | 25304a028e6d1426b14ead4a469c362b8536d893 (patch) | |
tree | 061bd796b2d65ad7b55df32ba34f57268e6c6b47 | |
parent | bbc584f0ac3586ce4e4a92e58f74ad69a15f0ed5 (diff) | |
download | dotty-25304a028e6d1426b14ead4a469c362b8536d893.tar.gz dotty-25304a028e6d1426b14ead4a469c362b8536d893.tar.bz2 dotty-25304a028e6d1426b14ead4a469c362b8536d893.zip |
Add initializers for lambdalift proxy fields
If an inner class has proxy fields, we missed so far the assignment sfrom
the proxy parameters in the primary constructor of the class to the proxy
fields.
Test case tries several variations of this.
-rw-r--r-- | src/dotty/tools/dotc/ast/tpd.scala | 9 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/LambdaLift.scala | 15 | ||||
-rw-r--r-- | tests/run/i659.scala | 24 |
3 files changed, 38 insertions, 10 deletions
diff --git a/src/dotty/tools/dotc/ast/tpd.scala b/src/dotty/tools/dotc/ast/tpd.scala index 51011f90b..a35e0e523 100644 --- a/src/dotty/tools/dotc/ast/tpd.scala +++ b/src/dotty/tools/dotc/ast/tpd.scala @@ -76,8 +76,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { def Block(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = ta.assignType(untpd.Block(stats, expr), stats, expr) - def maybeBlock(stats: List[Tree], expr: Tree)(implicit ctx: Context): Tree = - if (stats.isEmpty) expr else Block(stats, expr) + /** Join `stats` in front of `expr` creating a new block if necessary */ + def seq(stats: List[Tree], expr: Tree)(implicit ctx: Context): Tree = + if (stats.isEmpty) expr + else expr match { + case Block(estats, eexpr) => cpy.Block(expr)(stats ::: estats, eexpr) + case _ => Block(stats, expr) + } def If(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = ta.assignType(untpd.If(cond, thenp, elsep), thenp, elsep) diff --git a/src/dotty/tools/dotc/transform/LambdaLift.scala b/src/dotty/tools/dotc/transform/LambdaLift.scala index bffc7458e..41d6f3c43 100644 --- a/src/dotty/tools/dotc/transform/LambdaLift.scala +++ b/src/dotty/tools/dotc/transform/LambdaLift.scala @@ -384,14 +384,23 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform private def addFreeParams(tree: Tree, proxies: List[Symbol])(implicit ctx: Context, info: TransformerInfo): Tree = proxies match { case Nil => tree case proxies => + val sym = tree.symbol val ownProxies = - if (!tree.symbol.isConstructor) proxies - else proxies.map(_.copy(owner = tree.symbol, flags = Synthetic | Param)) + if (!sym.isConstructor) proxies + else proxies.map(_.copy(owner = sym, flags = Synthetic | Param)) 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))) + def copyParams(rhs: Tree) = { + ctx.log(i"copy params ${proxies.map(_.showLocated)}%, %, own = ${ownProxies.map(_.showLocated)}%, %") + seq((proxies, ownProxies).zipped.map(proxyInit), rhs) + } tree match { case tree: DefDef => - cpy.DefDef(tree)(vparamss = tree.vparamss.map(freeParamDefs ++ _)) + cpy.DefDef(tree)( + vparamss = tree.vparamss.map(freeParamDefs ++ _), + rhs = if (sym.isPrimaryConstructor) copyParams(tree.rhs) else tree.rhs) case tree: Template => cpy.Template(tree)(body = freeParamDefs ++ tree.body) } diff --git a/tests/run/i659.scala b/tests/run/i659.scala index 5f10f15e8..d1229515d 100644 --- a/tests/run/i659.scala +++ b/tests/run/i659.scala @@ -1,12 +1,26 @@ class Foo(val a: Int) { - //def foo = { {case x => x + a}: PartialFunction[Int, Int]} + def foo = { {case x => x + a}: PartialFunction[Int, Int]} class Bar { def result(x: Int) = x + a } def bar = new Bar } +class VFoo(val a: Int) extends AnyVal { + def foo = { {case x => x + a}: PartialFunction[Int, Int]} +} + object Test extends dotty.runtime.LegacyApp { - //val x = new Foo(1).foo.apply(2) - val y = new Foo(1).bar.result(2) - assert(y == 3, y) - //assert(x == 3, x) + + def Foo(a: Int) = { + class Bar { def result(x: Int) = x + a } + new Bar().result(2) + } + + val x1 = new Foo(1).bar.result(2) + assert(x1 == 3, s"x1 = $x1") + val x2 = Foo(1) + assert(x2 == 3, s"x2 = $x2") + val x3 = new Foo(1).foo.apply(2) + assert(x3 == 3, s"x3 = $x3") + val x4 = new VFoo(1).foo.apply(2) + assert(x4 == 3, s"x4 = $x4") } |