Skip to content

Commit cca5f8f

Browse files
authored
Merge pull request #11918 from dotty-staging/fix-#11914
Don't generate paths with outer accessors to stable references
2 parents 75a4589 + 4d1288e commit cca5f8f

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -518,26 +518,25 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
518518
private def computeThisBindings() = {
519519
// All needed this-proxies, paired-with and sorted-by nesting depth of
520520
// the classes they represent (innermost first)
521-
val sortedProxies = thisProxy.toList.map {
522-
case (cls, proxy) =>
523-
// The class that the this-proxy `selfSym` represents
524-
def classOf(selfSym: Symbol) = selfSym.info.classSymbol
525-
// The total nesting depth of the class represented by `selfSym`.
526-
def outerLevel(selfSym: Symbol): Int = classOf(selfSym).ownersIterator.length
527-
(outerLevel(cls), proxy.symbol)
528-
}.sortBy(-_._1)
521+
val sortedProxies = thisProxy.toList
522+
.map((cls, proxy) => (cls.ownersIterator.length, proxy.symbol))
523+
.sortBy(-_._1)
529524

530525
var lastSelf: Symbol = NoSymbol
531526
var lastLevel: Int = 0
532527
for ((level, selfSym) <- sortedProxies) {
533528
lazy val rhsClsSym = selfSym.info.widenDealias.classSymbol
534-
val rhs =
535-
if (lastSelf.exists)
536-
ref(lastSelf).outerSelect(lastLevel - level, selfSym.info)
537-
else if (rhsClsSym.is(Module) && rhsClsSym.isStatic)
538-
ref(rhsClsSym.sourceModule)
539-
else
540-
inlineCallPrefix
529+
val rhs = selfSym.info.dealias match
530+
case info: TermRef if info.isStable =>
531+
ref(info)
532+
case info =>
533+
val rhsClsSym = info.widenDealias.classSymbol
534+
if rhsClsSym.is(Module) && rhsClsSym.isStatic then
535+
ref(rhsClsSym.sourceModule)
536+
else if lastSelf.exists then
537+
ref(lastSelf).outerSelect(lastLevel - level, selfSym.info)
538+
else
539+
inlineCallPrefix
541540
val binding = ValDef(selfSym.asTerm, QuoteUtils.changeOwnerOfTree(rhs, selfSym)).withSpan(selfSym.span)
542541
bindingsBuf += binding
543542
inlining.println(i"proxy at $level: $selfSym = ${bindingsBuf.last}")

tests/run/i11914.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Wrapper[E <: Throwable]:
2+
object syntax:
3+
extension [A <: Matchable](a: E | A)
4+
transparent inline def foreach(f: A => Any): Unit =
5+
a match
6+
case e: E => ()
7+
case a: A => f(a)
8+
9+
def _catch[A](a: => A): E | A =
10+
try a
11+
catch case e: E => e
12+
13+
object throwables extends Wrapper[Throwable]
14+
import throwables.syntax.*
15+
16+
@main def Test(): Unit =
17+
for x <- throwables._catch(1/1) do println(x)

tests/run/i11914a.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
object Wrapper:
2+
type E <: Throwable
3+
class C:
4+
object syntax:
5+
extension [A <: Matchable](a: E | A)
6+
transparent inline def foreach(f: A => Any): Unit =
7+
a match
8+
case e: E =>
9+
Wrapper.this.n + m
10+
case a: A => f(a)
11+
val m: Int = 4
12+
13+
def _catch[A](a: => A): E | A =
14+
try a
15+
catch case e: E => e
16+
17+
val n: Int = 3
18+
19+
object throwables extends Wrapper.C
20+
import throwables.syntax.*
21+
22+
@main def Test(): Unit =
23+
for x <- Wrapper._catch(1/1) do println(x)

0 commit comments

Comments
 (0)