From f7910005038c188e573e8d1a42ff3e31c69c90c1 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 7 May 2014 18:26:37 +0200 Subject: Better printing of anonymous classes. Used to be just $anon, now is `Ps { ... }`, where `Ps` are the instantiated parents of the anonymous class. --- src/dotty/tools/dotc/core/Types.scala | 11 +++++++++++ src/dotty/tools/dotc/printing/PlainPrinter.scala | 7 +++---- src/dotty/tools/dotc/printing/RefinedPrinter.scala | 11 ++++------- 3 files changed, 18 insertions(+), 11 deletions(-) (limited to 'src/dotty') diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 36b546230..89facfee5 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -2002,12 +2002,23 @@ object Types { // cached because baseType needs parents private var parentsCache: List[TypeRef] = null + /** The parent type refs as seen from the given prefix */ override def parents(implicit ctx: Context): List[TypeRef] = { if (parentsCache == null) parentsCache = cls.classParents.mapConserve(rebase(_).asInstanceOf[TypeRef]) parentsCache } + /** The parent types with all type arguments */ + def instantiatedParents(implicit ctx: Context): List[Type] = + parents mapConserve { pref => + ((pref: Type) /: pref.classSymbol.typeParams) { (parent, tparam) => + val targSym = decls.lookup(tparam.name) + if (targSym.exists) RefinedType(parent, targSym.name, targSym.info) + else parent + } + } + def derivedClassInfo(prefix: Type)(implicit ctx: Context) = if (prefix eq this.prefix) this else ClassInfo(prefix, cls, classParents, decls, selfInfo) diff --git a/src/dotty/tools/dotc/printing/PlainPrinter.scala b/src/dotty/tools/dotc/printing/PlainPrinter.scala index 308470885..3a322648a 100644 --- a/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -251,22 +251,21 @@ class PlainPrinter(_ctx: Context) extends Printer { case sym: Symbol if !sym.isCompleted => "this: ? =>" case _ => "this: " ~ atPrec(InfixPrec) { toText(tp.selfType) } ~ " =>" } - val parentsText = Text(cparents.map(p => - toTextLocal(reconstituteParent(cls, p))), " with ") val trueDecls = otherDecls.filterNot(treatAsTypeArg) val declsText = if (trueDecls.isEmpty || !ctx.settings.debug.value) Text() else dclsText(trueDecls) - tparamsText ~ " extends " ~ parentsText ~ "{" ~ selfText ~ declsText ~ + tparamsText ~ " extends " ~ toTextParents(tp.parents) ~ "{" ~ selfText ~ declsText ~ "} at " ~ preText case _ => ": " ~ toTextGlobal(tp) } } + protected def toTextParents(parents: List[Type]): Text = Text(parents.map(toTextLocal), " with ") + protected def treatAsTypeParam(sym: Symbol): Boolean = false protected def treatAsTypeArg(sym: Symbol): Boolean = false - protected def reconstituteParent(cls: ClassSymbol, parent: Type): Type = parent /** String representation of symbol's kind. */ def kindString(sym: Symbol): String = { diff --git a/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/src/dotty/tools/dotc/printing/RefinedPrinter.scala index c20598bb3..d0a681f90 100644 --- a/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -115,8 +115,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { case _ => nameString(tp.symbol) } } + else if (tp.symbol.isAnonymousClass) + return toText(tp.info) case ExprType(result) => return "=> " ~ toText(result) + case tp: ClassInfo => + return toTextParents(tp.instantiatedParents) ~ "{...}" case tp: SelectionProto => return toText(RefinedType(WildcardType, tp.name, tp.memberProto)) case tp: ViewProto => @@ -437,13 +441,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { sym.isType && (sym is ProtectedLocal) && (sym.allOverriddenSymbols exists (_ is TypeParam)) - override protected def reconstituteParent(cls: ClassSymbol, parent: Type): Type = - (parent /: parent.classSymbol.typeParams) { (parent, tparam) => - val targSym = cls.decls.lookup(tparam.name) - if (targSym.exists) RefinedType(parent, targSym.name, targSym.info) - else parent - } - override def toText(sym: Symbol): Text = { if (sym.name == nme.IMPORT) { def importString(tree: untpd.Tree) = s"import ${tree.show}" -- cgit v1.2.3