Skip to content

More user-friendly pretty printing of types #2835

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 6 commits into from
Jul 6, 2017
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class ScalaSettings extends Settings.SettingGroup {
val YnoDeepSubtypes = BooleanSetting("-Yno-deep-subtypes", "throw an exception on deep subtyping call stacks.")
val YplainPrinter = BooleanSetting("-Yplain-printer", "Pretty-print using a plain printer.")
val YprintSyms = BooleanSetting("-Yprint-syms", "when printing trees print info in symbols instead of corresponding info in trees.")
val YprintDebug = BooleanSetting("-Yprint-debug", "when printing trees, print some extra information useful for debugging.")
val YtestPickler = BooleanSetting("-Ytest-pickler", "self-test for pickling functionality; should be used with -Ystop-after:pickler")
val YcheckReentrant = BooleanSetting("-Ycheck-reentrant", "check that compiled program does not contain vars that can be accessed from a global root.")
val YkeepComments = BooleanSetting("-Ykeep-comments", "Keep comments when scanning source files.")
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ object NameOps {
name.rewrite { case ExpandedName(_, unexp) => unexp }
}

/** Remove the variance from the name. */
def invariantName: N = likeSpaced {
name.rewrite { case VariantName(invariant, _) => invariant }
}

def implClassName: N = likeSpaced(name ++ tpnme.IMPL_CLASS_SUFFIX)

def errorName: N = likeSpaced(name ++ nme.ERROR)
Expand Down
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")"
case tp: TypeRef =>
toTextPrefix(tp.prefix) ~ selectionString(tp)
case AppliedType(tycon, args) =>
(toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
case tp: RefinedType =>
val parent :: (refined: List[RefinedType @unchecked]) =
refinementChain(tp).reverse
Expand Down Expand Up @@ -169,12 +171,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
changePrec(GlobalPrec) {
(if (tp.isImplicit) "(implicit " else "(") ~
Text((tp.paramNames, tp.paramInfos).zipped map paramText, ", ") ~
")" ~ toText(tp.resultType)
(if (tp.resultType.isInstanceOf[MethodType]) ")" else "): ") ~
toText(tp.resultType)
}
case tp: ExprType =>
changePrec(GlobalPrec) { "=> " ~ toText(tp.resultType) }
case tp: TypeLambda =>
def paramText(name: Name, bounds: TypeBounds): Text = name.toString ~ toText(bounds)
def paramText(name: Name, bounds: TypeBounds): Text = name.unexpandedName.toString ~ toText(bounds)
changePrec(GlobalPrec) {
"[" ~ Text((tp.paramNames, tp.paramInfos).zipped.map(paramText), ", ") ~
"]" ~ lambdaHash(tp) ~ (" => " provided !tp.resultType.isInstanceOf[MethodType]) ~
Expand All @@ -190,13 +193,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
toTextLocal(tycon) ~ "[" ~ Text(args.map(argText), ", ") ~ "]"
case tp: TypeVar =>
if (tp.isInstantiated)
toTextLocal(tp.instanceOpt) ~ "^" // debug for now, so that we can see where the TypeVars are.
toTextLocal(tp.instanceOpt) ~ ("^" provided ctx.settings.YprintDebug.value)
else {
val constr = ctx.typerState.constraint
val bounds =
if (constr.contains(tp)) constr.fullBounds(tp.origin)(ctx.addMode(Mode.Printing))
else TypeBounds.empty
if (bounds.isAlias) toText(bounds.lo) ~ "^"
if (bounds.isAlias) toText(bounds.lo) ~ ("^" provided ctx.settings.YprintDebug.value)
else if (ctx.settings.YshowVarBounds.value) "(" ~ toText(tp.origin) ~ "?" ~ toText(bounds) ~ ")"
else toText(tp.origin)
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
if (defn.isFunctionClass(cls)) return toTextFunction(args, cls.name.isImplicitFunction)
if (defn.isTupleClass(cls)) return toTextTuple(args)
if (isInfixType(tp)) return toTextInfixType(tycon, args)
return (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
case EtaExpansion(tycon) =>
return toText(tycon)
case tp: TypeRef =>
val hideType = !ctx.settings.debugAlias.value && (tp.symbol.isAliasPreferred)
if (hideType && !ctx.phase.erasedTypes && !tp.symbol.isCompleting) {
Expand Down Expand Up @@ -635,7 +636,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
if (tree.exists(!_.isEmpty)) encl(blockText(tree)) else ""

override protected def ParamRefNameString(name: Name): String =
name.unexpandedName.toString
name.unexpandedName.invariantName.toString

override protected def treatAsTypeParam(sym: Symbol): Boolean = sym is TypeParam

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages

assertEquals("Scope.foo(1)", tree.show)
assertEquals("((a: Int)Unit)(Scope.foo)", methodPart.show)
assertEquals("((a: Int): Unit)(Scope.foo)", methodPart.show)
}

@Test def ambiugousOverloadWithWildcard =
Expand All @@ -408,7 +408,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertMessageCount(1, messages)
val AmbiguousOverload(tree, List(alt1, alt2), pt: WildcardType) :: Nil = messages
assertEquals("method foo", alt1.show)
assertEquals("(s: String)String", alt1.info.show)
assertEquals("(s: String): String", alt1.info.show)
assertEquals("method foo", alt2.show)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/repl/i2492.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ scala> val s: Map {type Map$K =String;type Map$V = Int} = null
-- [E055] Syntax Error: <console>:5:7 ------------------------------------------
5 |val s: Map {type Map$K =String;type Map$V = Int} = null
| ^^^
|missing type parameter for [line1$object$$iw$$iw$Map$$K, line1$object$$iw$$iw$Map$$V] => Map[K, V]
| missing type parameter for Map
scala> :quit
5 changes: 5 additions & 0 deletions tests/repl/names.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scala> case class Foo[M[_]](x: M[Int])
defined class Foo
scala> Foo(Option(1))
val res0: Foo[Option] = Foo(Some(1))
scala> :quit