Skip to content

Fix #8931: Refinement of markFree in lambda lift #8953

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
May 12, 2020
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
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,21 @@ object LambdaLift {
if (!enclosure.exists) throw new NoPath
if (enclosure == sym.enclosure) NoSymbol
else {
def nestedInConstructor(sym: Symbol): Boolean =
sym.isConstructor ||
sym.isTerm && nestedInConstructor(sym.enclosure)
ctx.debuglog(i"mark free: ${sym.showLocated} with owner ${sym.maybeOwner} marked free in $enclosure")
val intermediate =
if (enclosure.is(PackageClass)) enclosure
else if (enclosure.isConstructor) markFree(sym, enclosure.owner.enclosure)
else markFree(sym, enclosure.enclosure)
if (intermediate.exists) narrowLiftedOwner(enclosure, intermediate)
if (!intermediate.isRealClass || enclosure.isConstructor)
if !intermediate.isRealClass || nestedInConstructor(enclosure) then
// Constructors and methods nested inside traits get the free variables
// of the enclosing trait or class.
// of the enclosing trait or class.
// Conversely, local traits do not get free variables.
// Methods inside constructors also don't have intermediates,
// need to get all their free variables passed directly.
if (!enclosure.is(Trait))
if (symSet(free, enclosure).add(sym)) {
changedFreeVars = true
Expand Down Expand Up @@ -301,7 +306,7 @@ object LambdaLift {
private def generateProxies()(implicit ctx: Context): Unit =
for ((owner, freeValues) <- free.iterator) {
val newFlags = Synthetic | (if (owner.isClass) ParamAccessor | Private else Param)
ctx.debuglog(i"free var proxy: ${owner.showLocated}, ${freeValues.toList}%, %")
ctx.debuglog(i"free var proxy of ${owner.showLocated}: ${freeValues.toList}%, %")
proxyMap(owner) = {
for (fv <- freeValues.toList) yield {
val proxyName = newName(fv)
Expand Down
61 changes: 61 additions & 0 deletions tests/run/i8931.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
object test1:

trait Trait

trait Managed[T](x: T) {

def flatMap(f: T => Managed[T]): Managed[T] = new Managed[T](x) {

def make() = new Trait {
val t: T = x
val u =
try {
f(t)
} catch {
case e: Exception => ()
}
}
}
}

object test2:

trait Trait

trait Managed[T](x: T) {
def xx = x

def flatMap(f: T => Managed[T]): Managed[T] = new Managed[T](x) {
def make() = new Trait {
val t: T = x
val u = {
def foo = f(t)
assert(foo.xx == 22)
foo
}
}
make()
}
}

object test3:

trait Trait

trait Managed[T]:

def flatMap[U](f: T => Managed[U]) =
class C:
def make() =
class D:
def bar(): T = ???
val t: T = ???
val u =
def foo = (f(t), f(bar()))
foo
new D().u
()

@main def Test() =
val m = new test2.Managed[Int](22) {}
m.flatMap(x => new test2.Managed(x) {})