Skip to content

Don't generate paths with outer accessors to stable references #11918

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 3 commits into from
Mar 28, 2021
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
29 changes: 14 additions & 15 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
17 changes: 17 additions & 0 deletions tests/run/i11914.scala
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions tests/run/i11914a.scala
Original file line number Diff line number Diff line change
@@ -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)