Skip to content

Fix cyclic reference errors in collection strawman #2914

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 4 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Standard-Section: "ASTs" TopLevelStat*
BIND Length boundName_NameRef bounds_Type
// for type-variables defined in a type pattern
BYNAMEtype underlying_Type
LAZYref underlying_Type
POLYtype Length result_Type NamesTypes
METHODtype Length result_Type NamesTypes // needed for refinements
TYPELAMBDAtype Length result_Type NamesTypes // variance encoded in front of name: +/-/(nothing)
Expand Down Expand Up @@ -256,7 +257,7 @@ object TastyFormat {
final val OBJECTCLASS = 40

final val SIGNED = 63

final val firstInternalTag = 64
final val IMPLMETH = 64

Expand Down Expand Up @@ -322,6 +323,7 @@ object TastyFormat {
final val PROTECTEDqualified = 105
final val RECtype = 106
final val SINGLETONtpt = 107
final val LAZYref = 108

final val IDENT = 112
final val IDENTtpt = 113
Expand Down Expand Up @@ -512,6 +514,7 @@ object TastyFormat {
case DOUBLEconst => "DOUBLEconst"
case STRINGconst => "STRINGconst"
case RECtype => "RECtype"
case LAZYref => "LAZYref"

case IDENT => "IDENT"
case IDENTtpt => "IDENTtpt"
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class TreePickler(pickler: TastyPickler) {
case tpe: ParamRef =>
assert(pickleParamRef(tpe), s"orphan parameter reference: $tpe")
case tpe: LazyRef =>
writeByte(LAZYref)
pickleType(tpe.ref)
}

Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
RecType(rt => registeringType(rt, readType()))
case RECthis =>
RecThis(readTypeRef().asInstanceOf[RecType])
case LAZYref =>
val rdr = fork
def readUnderlying() = rdr.readType()
skipTree()
LazyRef(readUnderlying)
Copy link
Member

Choose a reason for hiding this comment

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

This means LazyRefs will keep the context from which they were unpickled around, if these LazyRefs are subsequently stored in some cache, that could lead to space leaks, no?

Copy link
Contributor Author

@odersky odersky Jul 25, 2017

Choose a reason for hiding this comment

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

Yes, correct. Maybe the best option is to allow a context to be passed into a LazyRef completer. In the case of unpickling, this is fine; we don't need to hang on to more than the reader (I think, at least that's what we do for the other completers).

case SHARED =>
val ref = readAddr()
typeAtAddr.getOrElseUpdate(ref, forkAt(ref).readType())
Expand Down Expand Up @@ -991,7 +996,12 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
val refinements = readStats(refineCls, end)(localContext(refineCls))
RefinedTypeTree(parent, refinements, refineCls)
case APPLIEDtpt =>
AppliedTypeTree(readTpt(), until(end)(readTpt()))
// If we do directly a tpd.AppliedType tree we might get a
// wrong number of arguments in some scenarios reading F-bounded
// types. This came up in #137 of collection strawman.
val tycon = readTpt()
val args = until(end)(readTpt())
untpd.AppliedTypeTree(tycon, args).withType(tycon.tpe.safeAppliedTo(args.tpes))
case ANDtpt =>
val tpt1 = readTpt()
val tpt2 = readTpt()
Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.ast.untpd.Modifiers
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import scala.util.control.NonFatal

object messages {

Expand Down Expand Up @@ -708,10 +709,15 @@ object messages {

private val actualArgString = actual.map(_.show).mkString("[", ", ", "]")

private val prettyName = fntpe.termSymbol match {
case NoSymbol => fntpe.show
case symbol => symbol.showFullName
}
private val prettyName =
try
fntpe.termSymbol match {
case NoSymbol => fntpe.show
case symbol => symbol.showFullName
}
catch {
case NonFatal(ex) => fntpe.show
Copy link
Member

Choose a reason for hiding this comment

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

If this is only for cyclic references, could we do case ex: CyclicReference instead?

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 think we by default we should not throw any exception from printing. It detracts from the underlying cause.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but we shouldn't be throwing any kind of exceptions, and silently catching them all might hide issues.

}

val msg =
hl"""|${NoColor(msgPrefix)} type arguments for $prettyName$expectedArgString
Expand Down