diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 08ff58e918a2..e5eb8faa951d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -508,26 +508,25 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) { private def computeThisBindings() = { // 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) => - // The class that the this-proxy `selfSym` represents - def classOf(selfSym: Symbol) = selfSym.info.classSymbol - // The total nesting depth of the class represented by `selfSym`. - def outerLevel(selfSym: Symbol): Int = classOf(selfSym).ownersIterator.length - (outerLevel(cls), proxy.symbol) - }.sortBy(-_._1) + val sortedProxies = thisProxy.toList + .map((cls, proxy) => (cls.ownersIterator.length, proxy.symbol)) + .sortBy(-_._1) var lastSelf: Symbol = NoSymbol var lastLevel: Int = 0 for ((level, selfSym) <- sortedProxies) { lazy val rhsClsSym = selfSym.info.widenDealias.classSymbol - val rhs = - if (lastSelf.exists) - ref(lastSelf).outerSelect(lastLevel - level, selfSym.info) - else if (rhsClsSym.is(Module) && rhsClsSym.isStatic) - ref(rhsClsSym.sourceModule) - else - inlineCallPrefix + val rhs = selfSym.info.dealias match + case info: TermRef if info.isStable => + ref(info) + case info => + val rhsClsSym = info.widenDealias.classSymbol + if rhsClsSym.is(Module) && rhsClsSym.isStatic then + ref(rhsClsSym.sourceModule) + else if lastSelf.exists then + ref(lastSelf).outerSelect(lastLevel - level, selfSym.info) + else + inlineCallPrefix val binding = ValDef(selfSym.asTerm, QuoteUtils.changeOwnerOfTree(rhs, selfSym)).withSpan(selfSym.span) bindingsBuf += binding inlining.println(i"proxy at $level: $selfSym = ${bindingsBuf.last}") diff --git a/tests/run/i11914.scala b/tests/run/i11914.scala new file mode 100644 index 000000000000..f170fcc28f06 --- /dev/null +++ b/tests/run/i11914.scala @@ -0,0 +1,17 @@ +class Wrapper[E <: Throwable]: + object syntax: + extension [A <: Matchable](a: E | A) + transparent inline def foreach(f: A => Any): Unit = + a match + case e: E => () + case a: A => f(a) + + def _catch[A](a: => A): E | A = + try a + catch case e: E => e + +object throwables extends Wrapper[Throwable] +import throwables.syntax.* + +@main def Test(): Unit = + for x <- throwables._catch(1/1) do println(x) diff --git a/tests/run/i11914a.scala b/tests/run/i11914a.scala new file mode 100644 index 000000000000..5bece35ef326 --- /dev/null +++ b/tests/run/i11914a.scala @@ -0,0 +1,23 @@ +object Wrapper: + type E <: Throwable + class C: + object syntax: + extension [A <: Matchable](a: E | A) + transparent inline def foreach(f: A => Any): Unit = + a match + case e: E => + Wrapper.this.n + m + case a: A => f(a) + val m: Int = 4 + + def _catch[A](a: => A): E | A = + try a + catch case e: E => e + + val n: Int = 3 + +object throwables extends Wrapper.C +import throwables.syntax.* + +@main def Test(): Unit = + for x <- Wrapper._catch(1/1) do println(x) \ No newline at end of file