Skip to content

Fix #3703: Various fixes to make printing more robust in face of errors #3705

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
Jan 27, 2018
Merged
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ object Trees {
*/
def withType(tpe: Type)(implicit ctx: Context): ThisTree[Type] = {
if (tpe.isInstanceOf[ErrorType])
assert(!Config.checkUnreportedErrors || ctx.reporter.errorsReported)
assert(!Config.checkUnreportedErrors ||
ctx.reporter.errorsReported ||
ctx.settings.YshowPrintErrors.value
// under -Yshow-print-errors, errors might arise during printing, but they do not count as reported
)
else if (Config.checkTreesConsistent)
checkChildrenTyped(productIterator)
withTypeUnchecked(tpe)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/printing/Formatting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Formatting {
case NonFatal(ex)
if !ctx.mode.is(Mode.PrintShowExceptions) &&
!ctx.settings.YshowPrintErrors.value =>
s"[cannot display due to $ex, raw string = $toString]"
s"[cannot display due to $ex, raw string = ${arg.toString}]"
}
case _ => arg.toString
}
Expand Down
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ class PlainPrinter(_ctx: Context) extends Printer {
else toText(tp.origin)
}
case tp: LazyRef =>
"LazyRef(" ~ toTextGlobal(tp.ref) ~ ")" // TODO: only print this during debug mode?
def refTxt =
try toTextGlobal(tp.ref)
catch {
case ex: Throwable => Str("...")
Copy link
Member

Choose a reason for hiding this comment

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

Is this just to catch infinite loops in LazyRefs? If so I'd just catch StackOverflowException, though I think it'd be cleaner to do loop detection in LazyRef#ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's to detect stack overflows. Evaluation of LazyRefs is not tail-recursive, so we will always get a stack overflow. Maybe we can do a better job in loop detection but I believe the two issues are separate.

}
"LazyRef(" ~ refTxt ~ ")"
case _ =>
tp.fallbackToText(this)
}
Expand All @@ -217,7 +222,10 @@ class PlainPrinter(_ctx: Context) extends Printer {

/** If -uniqid is set, the hashcode of the lambda type, after a # */
protected def lambdaHash(pt: LambdaType): Text =
if (ctx.settings.uniqid.value) "#" + pt.hashCode else ""
if (ctx.settings.uniqid.value)
try "#" + pt.hashCode
catch { case ex: NullPointerException => "" }
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't we do a null check first instead of catching NullPointerException ? This is fishy in general and is also not something that can be done on Scala.js by default (https://www.scala-js.org/doc/semantics.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, hashCode is computed, the null could be in a referenced type.

else ""

/** If -uniqid is set, the unique id of symbol, after a # */
protected def idString(sym: Symbol): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
typeDefText(tparamsTxt, toText(rhs))
case LambdaTypeTree(tparams, body) =>
recur(body, tparamsText(tparams))
case rhs: TypeTree if rhs.tpe.isInstanceOf[TypeBounds] =>
case rhs: TypeTree if rhs.typeOpt.isInstanceOf[TypeBounds] =>
typeDefText(tparamsTxt, toText(rhs))
case rhs =>
typeDefText(tparamsTxt, optText(rhs)(" = " ~ _))
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i3703.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bar {
trait M[F[_]]
class S[XS[_] <: M[XS], A](val x: XS[A])
object S {
def apply[X[_] <: M[X], A](x: X[A]): S[X, A] = S[X, A](x)
def unapply[X[_] <: M[X], A](p: S[X, A]): S[X, A] = S(p.x)
}
}