Skip to content

Fix GADT-related memory leak #3233

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 5 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix GADT-related memory leak
There was a confusion which led to the wrong gadt map being used
for pattern-bound variables if there was no other GADT variable
in the enclosing method.

This led to the outermost gadt map in the initial context being
populated with type bounds which never went away.
  • Loading branch information
odersky committed Oct 4, 2017
commit eb21d24043cc285238b7bb41518c777dc16af28c
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ object Contexts {
def setRunInfo(runInfo: RunInfo): this.type = { this.runInfo = runInfo; this }
def setDiagnostics(diagnostics: Option[StringBuilder]): this.type = { this.diagnostics = diagnostics; this }
def setGadt(gadt: GADTMap): this.type = { this.gadt = gadt; this }
def setFreshGADTBounds: this.type = setGadt(new GADTMap(gadt.bounds))
def setTypeComparerFn(tcfn: Context => TypeComparer): this.type = { this.typeComparer = tcfn(this); this }
def setSearchHistory(searchHistory: SearchHistory): this.type = { this.searchHistory = searchHistory; this }
def setFreshNames(freshNames: FreshNameCreator): this.type = { this.freshNames = freshNames; this }
Expand All @@ -493,7 +494,6 @@ object Contexts {
def setSetting[T](setting: Setting[T], value: T): this.type =
setSettings(setting.updateIn(sstate, value))

def setFreshGADTBounds: this.type = { this.gadt = new GADTMap(gadt.bounds); this }

def setDebug = setSetting(base.settings.debug, true)
}
Expand Down Expand Up @@ -532,7 +532,7 @@ object Contexts {
moreProperties = Map.empty
typeComparer = new TypeComparer(this)
searchHistory = new SearchHistory(0, Map())
gadt = new GADTMap(SimpleMap.Empty)
gadt = new GADTMap(SimpleMap.Empty) // EmptyGADTMap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this supposed to be = EmptyGADTMap ?

}

@sharable object NoContext extends Context {
Expand Down Expand Up @@ -694,10 +694,13 @@ object Contexts {
implicit val ctx: Context = initctx
}

class GADTMap(initBounds: SimpleMap[Symbol, TypeBounds]) {
class GADTMap(initBounds: SimpleMap[Symbol, TypeBounds]) extends util.DotClass {
private var myBounds = initBounds
def setBounds(sym: Symbol, b: TypeBounds): Unit =
myBounds = myBounds.updated(sym, b)
def bounds = myBounds
}
object EmptyGADTMap extends GADTMap(SimpleMap.Empty) {
override def setBounds(sym: Symbol, b: TypeBounds) = unsupported("EmptyGADTMap.setBound")
}
}
12 changes: 3 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,9 @@ import reporting.diagnostic.messages.SuperCallsNotAllowedInline
* mini-phase or subfunction of a macro phase equally well. But taken by themselves
* they do not warrant their own group of miniphases before pickling.
*/
class PostTyper extends MacroTransform with SymTransformer { thisTransformer =>


class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTransformer =>
import tpd._

def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation = {
if (ref.is(BindDefinedType) && ctx.gadt.bounds.contains(ref.symbol)) {
ref.copySymDenotation(info = ctx.gadt.bounds.apply(ref.symbol) & ref.info)
} else ref
}

/** the following two members override abstract members in Transform */
override def phaseName: String = "posttyper"

Expand Down Expand Up @@ -289,6 +281,8 @@ class PostTyper extends MacroTransform with SymTransformer { thisTransformer =>
case _ =>
}
super.transform(tree)
case Typed(Ident(nme.WILDCARD), _) =>
tree // skip checking pattern type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because type bounds in a pattern need not conform to selector bounds. I.e. you have

type Tree[T >: Null <: Type]

You are still allowed to write

case x: Tree[_]

(which translates to)

case x: (_: Tree[_])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to refine & explain the logic better

case tree =>
super.transform(tree)
}
Expand Down
22 changes: 9 additions & 13 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -921,16 +921,21 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedCase(tree: untpd.CaseDef, pt: Type, selType: Type, gadtSyms: Set[Symbol])(implicit ctx: Context): CaseDef = track("typedCase") {
val originalCtx = ctx

val gadtCtx = ctx.fresh.setFreshGADTBounds
for (sym <- gadtSyms)
if (!gadtCtx.gadt.bounds.contains(sym))
gadtCtx.gadt.setBounds(sym, TypeBounds.empty)

/** - replace all references to symbols associated with wildcards by their GADT bounds
* - enter all symbols introduced by a Bind in current scope
*/
val indexPattern = new TreeMap {
val elimWildcardSym = new TypeMap {
def apply(t: Type) = t match {
case ref: TypeRef if ref.name == tpnme.WILDCARD && ctx.gadt.bounds.contains(ref.symbol) =>
ctx.gadt.bounds(ref.symbol)
case TypeAlias(ref: TypeRef) if ref.name == tpnme.WILDCARD && ctx.gadt.bounds.contains(ref.symbol) =>
ctx.gadt.bounds(ref.symbol)
case ref: TypeRef if ref.name == tpnme.WILDCARD && gadtCtx.gadt.bounds.contains(ref.symbol) =>
gadtCtx.gadt.bounds(ref.symbol)
case TypeAlias(ref: TypeRef) if ref.name == tpnme.WILDCARD && gadtCtx.gadt.bounds.contains(ref.symbol) =>
gadtCtx.gadt.bounds(ref.symbol)
case _ =>
mapOver(t)
}
Expand All @@ -955,15 +960,6 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
assignType(cpy.CaseDef(tree)(pat1, guard1, body1), body1)
}

val gadtCtx =
if (gadtSyms.isEmpty) ctx
else {
val c = ctx.fresh.setFreshGADTBounds
for (sym <- gadtSyms)
if (!c.gadt.bounds.contains(sym))
c.gadt.setBounds(sym, TypeBounds.empty)
c
}
val pat1 = typedPattern(tree.pat, selType)(gadtCtx)
caseRest(pat1)(gadtCtx.fresh.setNewScope)
}
Expand Down