Skip to content

Fix #2895: Two fixes for inlining #2912

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
Aug 2, 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
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/transform/Bridges.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Bridges(root: ClassSymbol)(implicit ctx: Context) {
* before erasure, if the following conditions are satisfied.
*
* - `member` and other have different signatures
* - `member` is not inline
* - there is not yet a bridge with the same name and signature in `root`
*
* The bridge has the erased info of `other` and forwards to `member`.
Expand All @@ -48,7 +47,7 @@ class Bridges(root: ClassSymbol)(implicit ctx: Context) {
def bridgeExists =
bridgesScope.lookupAll(member.name).exists(bridge =>
bridgeTarget(bridge) == member && bridge.signature == other.signature)
if (!(member.is(Inline) || member.signature == other.signature || bridgeExists))
if (!(member.signature == other.signature || bridgeExists))
addBridge(member, other)
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
assert(argss.isEmpty)
}

private def canElideThis(tpe: ThisType): Boolean =
prefix.tpe == tpe && ctx.owner.isContainedIn(tpe.cls) ||
tpe.cls.is(Package)

/** Populate `thisProxy` and `paramProxy` as follows:
*
* 1a. If given type refers to a static this, thisProxy binds it to corresponding global reference,
Expand All @@ -363,9 +367,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
* and MethodParams, not TypeRefs or TermRefs.
*/
private def registerType(tpe: Type): Unit = tpe match {
case tpe: ThisType
if !ctx.owner.isContainedIn(tpe.cls) && !tpe.cls.is(Package) &&
!thisProxy.contains(tpe.cls) =>
case tpe: ThisType if !canElideThis(tpe) && !thisProxy.contains(tpe.cls) =>
val proxyName = s"${tpe.cls.name}_this".toTermName
val proxyType = tpe.asSeenFrom(prefix.tpe, meth.owner)
thisProxy(tpe.cls) = newSym(proxyName, EmptyFlags, proxyType).termRef
Expand Down
17 changes: 17 additions & 0 deletions tests/run/i2895.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object Foo extends (Int => Int) {
inline def apply(x: Int): Int = impl(x)
def impl(x: Int): Int = x + 1
}

object Test {

def test(foo: Foo.type): Int = foo(41)

def test2(f: Int => Int): Int = f(41)

def main(args: Array[String]): Unit = {
assert(test(Foo) == 42)
assert(test2(Foo) == 42)
}

}
26 changes: 26 additions & 0 deletions tests/run/i2895a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

trait Foo[A <: Foo[A]] {

def next: A

inline final def once: A = next

def once1: A = once

def twice: A = once.once
}

trait Bar extends Foo[Bar]

case object Bar0 extends Bar { def next = Bar1 }
case object Bar1 extends Bar { def next = Bar2 }
case object Bar2 extends Bar { def next = this }

object Test {

def main(args: Array[String]): Unit = {
assert(Bar0.once.once.toString == "Bar2")
assert(Bar0.twice.toString == "Bar2")
}
}