Skip to content

New attempt to fix the leaking companion problem #424

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

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 29 additions & 1 deletion src/dotty/tools/dotc/transform/Flatten.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import core._
import DenotTransformers.SymTransformer
import Phases.Phase
import Contexts.Context
import Symbols._
import Names.Name
import NameOps._
import typer.Mode
import Flags._
import SymUtils._
import SymDenotations.SymDenotation
Expand All @@ -16,11 +20,35 @@ class Flatten extends MiniPhaseTransform with SymTransformer { thisTransform =>
import ast.tpd._
override def phaseName = "flatten"

/** Mark absent any companion classes or objects of the flattened version of `cls`
* in package `pkg` which are not yet completed. These are inner classes entered
* into the package scope by reading their class files. They should never be read
* because their information is superseded by the lifted class info.
*/
private def invalidateUndefinedCompanions(pkg: ClassSymbol, cls: ClassSymbol)(implicit ctx: Context): Unit = {
def invalidate(otherName: Name) = {
val other = pkg.info.decl(otherName).asSymDenotation
if (other.exists && !other.isCompleted) other.markAbsent
}
if (cls is Flags.Module) {
invalidate(cls.name.sourceModuleName)
invalidate(cls.name.stripModuleClassSuffix.toTypeName)
}
else {
invalidate(cls.name.toTermName)
invalidate(cls.name.moduleClassName)
}
}

def transformSym(ref: SymDenotation)(implicit ctx: Context) = {
if (ref.isClass && !ref.is(Package) && !ref.owner.is(Package)) {
val cls = ref.symbol.asClass
val pkg = ref.enclosingPackageClass.asClass
invalidateUndefinedCompanions(pkg, cls)(
ctx.withPhase(cls.initial.validFor.phaseId).addMode(Mode.FutureDefsOK))
ref.copySymDenotation(
name = ref.flatName(),
owner = ref.enclosingPackageClass)
owner = pkg)
}
else ref
}
Expand Down
17 changes: 0 additions & 17 deletions src/dotty/tools/dotc/transform/RestoreScopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import DenotTransformers.IdentityDenotTransformer
import Contexts.Context
import Symbols._
import Scopes._
import collection.mutable
import TreeTransforms.MiniPhaseTransform
import SymDenotations._
import ast.Trees._
import NameOps._
import typer.Mode
import TreeTransforms.TransformerInfo

/** The preceding lambda lift and flatten phases move symbols to different scopes
Expand All @@ -22,18 +19,6 @@ class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { t
import ast.tpd._
override def phaseName = "restoreScopes"

private def invalidateUndefinedCompanions(pkg: ClassSymbol, cls: ClassSymbol)(implicit ctx: Context): Unit = {
val otherNames =
if (cls is Flags.Module)
List(cls.name.sourceModuleName, cls.name.stripModuleClassSuffix.toTypeName)
else
List(cls.name.toTermName, cls.name.moduleClassName)
for (otherName <- otherNames) {
val other = pkg.info.decl(otherName).asSymDenotation
if (other.exists && !other.isCompleted) other.markAbsent
}
}

override def transformTypeDef(tree: TypeDef)(implicit ctx: Context, info: TransformerInfo) = {
val TypeDef(_, impl: Template) = tree
//
Expand All @@ -46,8 +31,6 @@ class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { t
val cls = tree.symbol.asClass
val pkg = cls.owner.asClass
pkg.enter(cls)
invalidateUndefinedCompanions(pkg, cls)(
ctx.withPhase(cls.initial.validFor.phaseId).addMode(Mode.FutureDefsOK))
val cinfo = cls.classInfo
tree.symbol.copySymDenotation(
info = cinfo.derivedClassInfo( // Dotty deviation: Cannot expand cinfo inline without a type error
Expand Down