From 442915beab53971ba182177872276c1e68df3f70 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 18 Jul 2017 15:30:14 +0200 Subject: [PATCH 1/6] Make namedTrees surivive TypeErrors Same principle as for completions applies. --- .../dotty/tools/dotc/interactive/Interactive.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala index 1eed3533267f..215c50a242ce 100644 --- a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala +++ b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala @@ -62,6 +62,9 @@ object Interactive { sourceSymbol(sym.owner) else sym + private def safely[T](default: T)(op: => T) = + try op catch { case ex: TypeError => default } + /** Possible completions at position `pos` */ def completions(trees: List[SourceTree], pos: SourcePosition)(implicit ctx: Context): List[Symbol] = { val path = pathTo(trees, pos) @@ -85,16 +88,13 @@ object Interactive { } /** Possible completions of members of `prefix` which are accessible when called inside `boundary` */ - def completions(prefix: Type, boundary: Symbol)(implicit ctx: Context): List[Symbol] = { - val boundaryCtx = ctx.withOwner(boundary) - try + def completions(prefix: Type, boundary: Symbol)(implicit ctx: Context): List[Symbol] = + safely(Nil) { + val boundaryCtx = ctx.withOwner(boundary) prefix.memberDenots(completionsFilter, (name, buf) => buf ++= prefix.member(name).altsWith(d => !d.isAbsent && d.symbol.isAccessibleFrom(prefix)(boundaryCtx)) ).map(_.symbol).toList - catch { - case ex: TypeError => Nil } - } /** Filter for names that should appear when looking for completions. */ private[this] object completionsFilter extends NameFilter { @@ -131,7 +131,7 @@ object Interactive { * @param includeReferences If true, include references and not just definitions */ def namedTrees(trees: List[SourceTree], includeReferences: Boolean, treePredicate: NameTree => Boolean) - (implicit ctx: Context): List[SourceTree] = { + (implicit ctx: Context): List[SourceTree] = safely(Nil) { val buf = new mutable.ListBuffer[SourceTree] trees foreach { case SourceTree(topTree, source) => From 2cb53d9dd6e86f56a3dad6f18d4aef24d5ad602f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 18 Jul 2017 16:51:03 +0200 Subject: [PATCH 2/6] Fix build problem --- .../src/dotty/tools/dotc/interactive/Interactive.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala index 215c50a242ce..4284da2967a9 100644 --- a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala +++ b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala @@ -62,8 +62,8 @@ object Interactive { sourceSymbol(sym.owner) else sym - private def safely[T](default: T)(op: => T) = - try op catch { case ex: TypeError => default } + private def safely[T](op: => List[T]): List[T] = + try op catch { case ex: TypeError => Nil } /** Possible completions at position `pos` */ def completions(trees: List[SourceTree], pos: SourcePosition)(implicit ctx: Context): List[Symbol] = { @@ -89,7 +89,7 @@ object Interactive { /** Possible completions of members of `prefix` which are accessible when called inside `boundary` */ def completions(prefix: Type, boundary: Symbol)(implicit ctx: Context): List[Symbol] = - safely(Nil) { + safely { val boundaryCtx = ctx.withOwner(boundary) prefix.memberDenots(completionsFilter, (name, buf) => buf ++= prefix.member(name).altsWith(d => !d.isAbsent && d.symbol.isAccessibleFrom(prefix)(boundaryCtx)) @@ -131,7 +131,7 @@ object Interactive { * @param includeReferences If true, include references and not just definitions */ def namedTrees(trees: List[SourceTree], includeReferences: Boolean, treePredicate: NameTree => Boolean) - (implicit ctx: Context): List[SourceTree] = safely(Nil) { + (implicit ctx: Context): List[SourceTree] = safely { val buf = new mutable.ListBuffer[SourceTree] trees foreach { case SourceTree(topTree, source) => From 221320fa3de71e05cb59c97c5dff8c84cda07421 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 19 Jul 2017 13:07:02 +0200 Subject: [PATCH 3/6] Avoid unnecessary narrowLiftedOwner in markFree This prevented many local functions from being lifted out to static scope, and thereby caused memory leaks. The memory leak found in the IDE was caused an anonymous function in NamerContextOps (a base trait of Context) that was kept as a non-private private method of NamerContextOps, taking a `this` referring to a context. --- compiler/src/dotty/tools/dotc/transform/LambdaLift.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/LambdaLift.scala b/compiler/src/dotty/tools/dotc/transform/LambdaLift.scala index c5f5d879178c..4cedfe74efb2 100644 --- a/compiler/src/dotty/tools/dotc/transform/LambdaLift.scala +++ b/compiler/src/dotty/tools/dotc/transform/LambdaLift.scala @@ -204,7 +204,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform if (enclosure.is(PackageClass)) enclosure else if (enclosure.isConstructor) markFree(sym, enclosure.owner.enclosure) else markFree(sym, enclosure.enclosure) - narrowLiftedOwner(enclosure, intermediate orElse sym.enclosingClass) + if (intermediate.exists) narrowLiftedOwner(enclosure, intermediate) if (!intermediate.isRealClass || enclosure.isConstructor) { // Constructors and methods nested inside traits get the free variables // of the enclosing trait or class. @@ -384,7 +384,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform local.copySymDenotation( owner = newOwner, name = newName(local), - initFlags = local.flags &~ Module | Private | maybeStatic, + initFlags = local.flags &~ Module &~ Final | Private | maybeStatic, // drop Module because class is no longer a singleton in the lifted context. info = liftedInfo(local)).installAfter(thisTransform) } From 49fd9bd4859ced1b34ed658338b4860ed53e38fa Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 19 Jul 2017 13:09:48 +0200 Subject: [PATCH 4/6] Null out closure in LazyRef after use --- compiler/src/dotty/tools/dotc/core/Types.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 416daeabee84..415219c00a6a 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -2157,13 +2157,15 @@ object Types { } case class LazyRef(refFn: () => Type) extends UncachedProxyType with ValueType { + private var myRefFn: () => Type = refFn private var myRef: Type = null private var computed = false def ref = { if (computed) assert(myRef != null) else { computed = true - myRef = refFn() + myRef = myRefFn() + myRefFn = null // null out to avoid memory leak } myRef } From 2f6cb089d444f5894964244bebbf61bef808947c Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 19 Jul 2017 15:39:54 +0200 Subject: [PATCH 5/6] Fix LazyRef closure removal. The previous attempt did not actually remove the closure, since LazyRef is a case class. --- compiler/src/dotty/tools/dotc/core/Types.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 415219c00a6a..21f0b1708a21 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -2156,16 +2156,14 @@ object Types { } } - case class LazyRef(refFn: () => Type) extends UncachedProxyType with ValueType { - private var myRefFn: () => Type = refFn + case class LazyRef(private var refFn: () => Type) extends UncachedProxyType with ValueType { private var myRef: Type = null private var computed = false def ref = { if (computed) assert(myRef != null) else { computed = true - myRef = myRefFn() - myRefFn = null // null out to avoid memory leak + myRef = refFn() } myRef } From 0a8a2a3fd4e823b49e046257785e17e03ba1a74d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 19 Jul 2017 15:41:47 +0200 Subject: [PATCH 6/6] Null out closure in LazyBodyAnnotation --- compiler/src/dotty/tools/dotc/core/Annotations.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/Annotations.scala b/compiler/src/dotty/tools/dotc/core/Annotations.scala index 236079c9e1b9..17b1dba722ef 100644 --- a/compiler/src/dotty/tools/dotc/core/Annotations.scala +++ b/compiler/src/dotty/tools/dotc/core/Annotations.scala @@ -61,7 +61,7 @@ object Annotations { def tree(implicit ctx: Context) = body } - case class LazyBodyAnnotation(bodyExpr: Context => Tree) extends BodyAnnotation { + case class LazyBodyAnnotation(private var bodyExpr: Context => Tree) extends BodyAnnotation { private var evaluated = false private var myBody: Tree = _ def tree(implicit ctx: Context) = { @@ -69,6 +69,7 @@ object Annotations { else { evaluated = true myBody = bodyExpr(ctx) + bodyExpr = null } myBody }