Skip to content

Commit 81e2d38

Browse files
committed
Sharpen condition when to elide this during inlining
This fixes the second half of #2895.
1 parent 97b9c07 commit 81e2d38

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

+5-3
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
351351
assert(argss.isEmpty)
352352
}
353353

354+
private def canElideThis(tpe: ThisType): Boolean =
355+
prefix.tpe == tpe && ctx.owner.isContainedIn(tpe.cls) ||
356+
tpe.cls.is(Package)
357+
354358
/** Populate `thisProxy` and `paramProxy` as follows:
355359
*
356360
* 1a. If given type refers to a static this, thisProxy binds it to corresponding global reference,
@@ -363,9 +367,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
363367
* and MethodParams, not TypeRefs or TermRefs.
364368
*/
365369
private def registerType(tpe: Type): Unit = tpe match {
366-
case tpe: ThisType
367-
if !ctx.owner.isContainedIn(tpe.cls) && !tpe.cls.is(Package) &&
368-
!thisProxy.contains(tpe.cls) =>
370+
case tpe: ThisType if !canElideThis(tpe) && !thisProxy.contains(tpe.cls) =>
369371
val proxyName = s"${tpe.cls.name}_this".toTermName
370372
val proxyType = tpe.asSeenFrom(prefix.tpe, meth.owner)
371373
thisProxy(tpe.cls) = newSym(proxyName, EmptyFlags, proxyType).termRef

tests/run/i2895a.scala

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
trait Foo[A <: Foo[A]] {
3+
4+
def next: A
5+
6+
inline final def once: A = next
7+
8+
def once1: A = once
9+
10+
def twice: A = once.once
11+
}
12+
13+
trait Bar extends Foo[Bar]
14+
15+
case object Bar0 extends Bar { def next = Bar1 }
16+
case object Bar1 extends Bar { def next = Bar2 }
17+
case object Bar2 extends Bar { def next = this }
18+
19+
object Test {
20+
21+
def main(args: Array[String]): Unit = {
22+
assert(Bar0.once.once.toString == "Bar2")
23+
assert(Bar0.twice.toString == "Bar2")
24+
}
25+
}
26+

0 commit comments

Comments
 (0)