Skip to content

Fix #1990: Handle inlining where this proxies change types #1996

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 5 commits into from
Feb 21, 2017
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
private def followOuterLinks(t: Tree)(implicit ctx: Context) = t match {
case t: This if ctx.erasedTypes && !(t.symbol == ctx.owner.enclosingClass || t.symbol.isStaticOwner) =>
// after erasure outer paths should be respected
new ExplicitOuter.OuterOps(ctx).path(t.tpe.widen.classSymbol)
new ExplicitOuter.OuterOps(ctx).path(toCls = t.tpe.widen.classSymbol)
case t =>
t
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ object NameOps {
else -1
}


/** The number of hops specified in an outer-select name */
def outerSelectHops: Int = {
require(isOuterSelect)
name.dropRight(nme.OUTER_SELECT.length).toString.toInt
}

/** The name of the generic runtime operation corresponding to an array operation */
def genericArrayOp: TermName = name match {
case nme.apply => nme.array_apply
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ object Erasure extends TypeTestsCasts{
if (tree.symbol == ctx.owner.lexicallyEnclosingClass || tree.symbol.isStaticOwner) promote(tree)
else {
ctx.log(i"computing outer path from ${ctx.owner.ownersIterator.toList}%, % to ${tree.symbol}, encl class = ${ctx.owner.enclosingClass}")
outer.path(tree.symbol)
outer.path(toCls = tree.symbol)
}

private def runtimeCallWithProtoArgs(name: Name, pt: Type, args: Tree*)(implicit ctx: Context): Tree = {
Expand Down
25 changes: 17 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class ExplicitOuter extends MiniPhaseTransform with InfoTransformer { thisTransf
/** Convert a selection of the form `qual.C_<OUTER>` to an outer path from `qual` to `C` */
override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo) =
if (tree.name.isOuterSelect)
outer.path(tree.tpe.widen.classSymbol, tree.qualifier).ensureConforms(tree.tpe)
outer.path(start = tree.qualifier, count = tree.name.outerSelectHops)
.ensureConforms(tree.tpe)
else tree

/** First, add outer accessors if a class does not have them yet and it references an outer this.
Expand Down Expand Up @@ -354,24 +355,32 @@ object ExplicitOuter {
} else Nil
}

/** The path of outer accessors that references `toCls.this` starting from
* the context owner's this node.
/** A path of outer accessors starting from node `start`. `start` defaults to the
* context owner's this node. There are two alternative conditions that determine
* where the path ends:
*
* - if the initial `count` parameter is non-negative: where the number of
* outer accessors reaches count.
* - if the initial `count` parameter is negative: where the class symbol of
* the type of the reached tree matches `toCls`.
*/
def path(toCls: Symbol, start: Tree = This(ctx.owner.lexicallyEnclosingClass.asClass)): Tree = try {
def loop(tree: Tree): Tree = {
def path(start: Tree = This(ctx.owner.lexicallyEnclosingClass.asClass),
toCls: Symbol = NoSymbol,
count: Int = -1): Tree = try {
def loop(tree: Tree, count: Int): Tree = {
val treeCls = tree.tpe.widen.classSymbol
val outerAccessorCtx = ctx.withPhaseNoLater(ctx.lambdaLiftPhase) // lambdalift mangles local class names, which means we cannot reliably find outer acessors anymore
ctx.log(i"outer to $toCls of $tree: ${tree.tpe}, looking for ${outerAccName(treeCls.asClass)(outerAccessorCtx)} in $treeCls")
if (treeCls == toCls) tree
if (count == 0 || count < 0 && treeCls == toCls) tree
else {
val acc = outerAccessor(treeCls.asClass)(outerAccessorCtx)
assert(acc.exists,
i"failure to construct path from ${ctx.owner.ownersIterator.toList}%/% to `this` of ${toCls.showLocated};\n${treeCls.showLocated} does not have an outer accessor")
loop(tree.select(acc).ensureApplied)
loop(tree.select(acc).ensureApplied, count - 1)
}
}
ctx.log(i"computing outerpath to $toCls from ${ctx.outersIterator.map(_.owner).toList}")
loop(start)
loop(start, count)
} catch {
case ex: ClassCastException =>
throw new ClassCastException(i"no path exists from ${ctx.owner.enclosingClass} to $toCls")
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
singleton(clazz.thisType)
else if (ctx.owner.isConstructor)
outerParam.get(ctx.owner) match {
case Some(param) => outer.path(clazz, Ident(param.termRef))
case _ => outer.path(clazz)
case Some(param) => outer.path(start = Ident(param.termRef), toCls = clazz)
case _ => outer.path(toCls = clazz)
}
else outer.path(clazz)
else outer.path(toCls = clazz)
transformFollowingDeep(qual.select(sym))
}

Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import core.Types._
import core.Flags._
import core.Constants._
import core.StdNames._
import core.NameOps._
import core.Decorators._
import core.TypeErasure.isErasedType
import core.Phases.Phase
Expand Down Expand Up @@ -336,7 +337,10 @@ class TreeChecker extends Phase with SymTransformer {
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
val tpe = tree.typeOpt
val sym = tree.symbol
if (!tpe.isInstanceOf[WithFixedSym] && sym.exists && !sym.is(Private)) {
if (!tpe.isInstanceOf[WithFixedSym] &&
sym.exists && !sym.is(Private) &&
!tree.name.isOuterSelect // outer selects have effectively fixed symbols
) {
val qualTpe = tree.qualifier.typeOpt
val member =
if (sym.is(Private)) qualTpe.member(tree.name)
Expand Down
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -396,24 +396,29 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
def classOf(selfSym: Symbol) = selfSym.info.widen.classSymbol

// The name of the outer selector that computes the rhs of `selfSym`
def outerSelector(selfSym: Symbol): TermName = classOf(selfSym).name.toTermName ++ nme.OUTER_SELECT
def outerSelector(n: Int): TermName = n.toString.toTermName ++ nme.OUTER_SELECT

// The total nesting depth of the class represented by `selfSym`.
def outerLevel(selfSym: Symbol): Int = classOf(selfSym).ownersIterator.length

// All needed this-proxies, sorted by nesting depth of the classes they represent (innermost first)
val accessedSelfSyms = thisProxy.values.toList.map(_.symbol).sortBy(-outerLevel(_))
// All needed this-proxies, paired-with and sorted-by nesting depth of
// the classes they represent (innermost first)
val sortedProxies = thisProxy.toList.map {
case (cls, proxy) => (outerLevel(cls), proxy.symbol)
} sortBy (-_._1)

// Compute val-definitions for all this-proxies and append them to `bindingsBuf`
var lastSelf: Symbol = NoSymbol
for (selfSym <- accessedSelfSyms) {
var lastLevel: Int = 0
for ((level, selfSym) <- sortedProxies) {
val rhs =
if (!lastSelf.exists)
prefix
else
untpd.Select(ref(lastSelf), outerSelector(selfSym)).withType(selfSym.info)
untpd.Select(ref(lastSelf), outerSelector(lastLevel - level)).withType(selfSym.info)
bindingsBuf += ValDef(selfSym.asTerm, rhs)
lastSelf = selfSym
lastLevel = level
}

// The type map to apply to the inlined tree. This maps references to this-types
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i1990.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class A {
class Foo {
inline def inlineMeth: Unit = {
new Bar
}
}
class Bar
}

class B extends A {
(new Foo).inlineMeth
}
20 changes: 20 additions & 0 deletions tests/pos/i1990a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class A { self =>
class Foo {
inline def inlineMeth: Unit = {
println(self)
}
}
}

class C extends A {
class B extends A
}

object Test {
def main(args: Array[String]): Unit = {
val c = new C
val b = new c.B

(new b.Foo).inlineMeth
}
}
1 change: 1 addition & 0 deletions tests/run/builder.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Table(Row(Cell(A1), Cell(B1)), Row(Cell(A2), Cell(B2)))
51 changes: 51 additions & 0 deletions tests/run/builder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import collection.mutable.ArrayBuffer

class Table {
val rows = new ArrayBuffer[Row]
def add(r: Row): Unit = rows += r
override def toString = rows.mkString("Table(", ", ", ")")
}

class Row {
val cells = new ArrayBuffer[Cell]
def add(c: Cell): Unit = cells += c
override def toString = cells.mkString("Row(", ", ", ")")
}

class Cell(elem: String) {
override def toString = s"Cell($elem)"
}

object Test {

def table(init: implicit Table => Unit) = {
implicit val t = new Table
init
t
}

def row(init: implicit Row => Unit)(implicit t: Table) = {
implicit val r = new Row
init
t.add(r)
}

def cell(str: String)(implicit r: Row) =
r.add(new Cell(str))

val data =
table {
row {
cell("A1")
cell("B1")
}
row {
cell("A2")
cell("B2")
}
}

def main(args: Array[String]) = {
println(data)
}
}
1 change: 1 addition & 0 deletions tests/run/i1990b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C()
20 changes: 20 additions & 0 deletions tests/run/i1990b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trait A { self =>
class Foo {
inline def inlineMeth: Unit = {
println(self)
}
}
}

case class A2() extends A {
case class C() extends Foo with A

val c = new C
(new c.Foo).inlineMeth
}

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