Skip to content

Add cyclic reference detection to TASTY unpickling #3842

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 1 commit into from
Jan 15, 2018
Merged
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
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,16 @@ class TreeUnpickler(reader: TastyReader,
* or else read definition.
*/
def readIndexedDef()(implicit ctx: Context): Tree = treeAtAddr.remove(currentAddr) match {
case Some(tree) => skipTree(); tree
case none => readNewDef()
case Some(tree) =>
assert(tree != PoisonTree, s"Cyclic reference while unpickling definition at address ${currentAddr.index} in unit ${ctx.compilationUnit}")
skipTree()
tree
case none =>
val start = currentAddr
treeAtAddr(start) = PoisonTree
val tree = readNewDef()
treeAtAddr.remove(start)
tree
}

private def readNewDef()(implicit ctx: Context): Tree = {
Expand Down Expand Up @@ -1169,6 +1177,9 @@ class TreeUnpickler(reader: TastyReader,

object TreeUnpickler {

/** A marker value used to detect cyclic reference while unpickling definitions. */
@sharable val PoisonTree: tpd.Tree = Thicket(Nil)

/** An enumeration indicating which subtrees should be added to an OwnerTree. */
type MemberDefMode = Int
final val MemberDefsOnly = 0 // add only member defs; skip other statements
Expand Down