Skip to content
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

Fix hoisting local symbols #14714

Merged
merged 1 commit into from
Mar 21, 2022
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
18 changes: 15 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
if enclClass.isContainedIn(thisClass) then thisClass
else enclClass) // unknown this reference, play it safe and assume the narrowest possible owner

def setLogicOwner(local: Symbol) =
val encClass = local.owner.enclosingClass
val preferEncClass =
encClass.isStatic
// non-static classes can capture owners, so should be avoided
&& (encClass.isProperlyContainedIn(local.topLevelClass)
// can be false for symbols which are defined in some weird combination of supercalls.
|| encClass.is(ModuleClass, butNot = Package)
// needed to not cause deadlocks in classloader. see t5375.scala
)
logicOwner(sym) = if preferEncClass then encClass else local.enclosingPackageClass

tree match
case tree: Ident =>
if isLocal(sym) then
Expand All @@ -194,7 +206,7 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
case tree: This =>
narrowTo(tree.symbol.asClass)
case tree: MemberDef if isExpr(sym) && sym.owner.isTerm =>
logicOwner(sym) = sym.enclosingPackageClass
setLogicOwner(sym)
// this will make methods in supercall constructors of top-level classes owned
// by the enclosing package, which means they will be static.
// On the other hand, all other methods will be indirectly owned by their
Expand All @@ -205,8 +217,8 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
// the class itself. This is done so that the constructor inherits
// the free variables of the class.
symSet(called, sym) += sym.owner
case tree: TypeDef =>
if sym.owner.isTerm then logicOwner(sym) = sym.topLevelClass.owner
case tree: TypeDef if sym.owner.isTerm =>
setLogicOwner(sym)
case _ =>
end process

Expand Down
15 changes: 1 addition & 14 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,7 @@ object LambdaLift:
private def liftLocals()(using Context): Unit = {
for ((local, lOwner) <- deps.logicalOwner) {
val (newOwner, maybeStatic) =
if (lOwner is Package) {
val encClass = local.enclosingClass
val topClass = local.topLevelClass
val preferEncClass =
encClass.isStatic &&
// non-static classes can capture owners, so should be avoided
(encClass.isProperlyContainedIn(topClass) ||
// can be false for symbols which are defined in some weird combination of supercalls.
encClass.is(ModuleClass, butNot = Package)
// needed to not cause deadlocks in classloader. see t5375.scala
)
if (preferEncClass) (encClass, EmptyFlags)
else (topClass, JavaStatic)
}
if lOwner is Package then (local.topLevelClass, JavaStatic)
else (lOwner, EmptyFlags)
// Drop Module because class is no longer a singleton in the lifted context.
var initFlags = local.flags &~ Module | Private | Lifted | maybeStatic
Expand Down
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/MegaPhase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,16 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
}
}

if (tree.source != ctx.source && tree.source.exists)
transformTree(tree, start)(using ctx.withSource(tree.source))
else if (tree.isInstanceOf[NameTree])
transformNamed(tree, start, ctx)
else
transformUnnamed(tree, start, ctx)
// try
if (tree.source != ctx.source && tree.source.exists)
transformTree(tree, start)(using ctx.withSource(tree.source))
else if (tree.isInstanceOf[NameTree])
transformNamed(tree, start, ctx)
else
transformUnnamed(tree, start, ctx)
// catch case ex: AssertionError =>
// println(i"error while transforming $tree")
// throw ex
}

def transformSpecificTree[T <: Tree](tree: T, start: Int)(using Context): T =
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i14707.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object M {
class A {{
def f(v: => Int): Int = 0
def g: Int = 0
new { f(g) }
}}
}

object M2 {
abstract class A {
def local = {
def f(v: () => Int): Int = 0
def g(): Int = 0
new AnyRef { def h = f(() => g()) }
}
}
}