Skip to content

Commit 53546ae

Browse files
committed
REPL: do not dealias before printing types
Also simplify the mechanism used to avoid printing prefixes. This is now handled in the PlainPrinter and limited to List, Map, Set, Seq.
1 parent 4fae88a commit 53546ae

File tree

7 files changed

+34
-35
lines changed

7 files changed

+34
-35
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,16 @@ class Definitions {
357357
def newGenericArrayMethod(implicit ctx: Context) = DottyArraysModule.requiredMethod("newGenericArray")
358358
def newArrayMethod(implicit ctx: Context) = DottyArraysModule.requiredMethod("newArray")
359359

360+
lazy val ListType = ctx.requiredClassRef("scala.collection.immutable.List")
361+
def ListClass(implicit ctx: Context) = ListType.symbol.asClass
360362
lazy val NilModuleRef = ctx.requiredModuleRef("scala.collection.immutable.Nil")
361363
def NilModule(implicit ctx: Context) = NilModuleRef.symbol
362364

365+
lazy val immutableMapType = ctx.requiredClassRef("scala.collection.immutable.Map")
366+
def immutableMapClass(implicit ctx: Context) = immutableMapType.symbol.asClass
367+
lazy val immutableSetType = ctx.requiredClassRef("scala.collection.immutable.Set")
368+
def immutableSetClass(implicit ctx: Context) = immutableSetType.symbol.asClass
369+
363370
lazy val SingletonClass: ClassSymbol =
364371
// needed as a synthetic class because Scala 2.x refers to it in classfiles
365372
// but does not define it as an explicit class.

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class PlainPrinter(_ctx: Context) extends Printer {
136136
case _ => Nil
137137
})
138138

139+
// Direct references to these symbols are printed without their prefix for convenience,
140+
// they are either aliased in scala.Predef or in the scala package object.
141+
private[this] lazy val printWithoutPrefix: Set[Symbol] =
142+
Set(defn.ListClass, defn.immutableMapClass, defn.immutableSetClass, defn.SeqClass)
143+
139144
def toText(tp: Type): Text = controlled {
140145
homogenize(tp) match {
141146
case tp: TypeType =>
@@ -147,7 +152,12 @@ class PlainPrinter(_ctx: Context) extends Printer {
147152
case tp: TermRef if tp.denot.isOverloaded =>
148153
"<overloaded " ~ toTextRef(tp) ~ ">"
149154
case tp: TypeRef =>
150-
toTextPrefix(tp.prefix) ~ selectionString(tp)
155+
printWithoutPrefix.find(sym => tp.isDirectRef(sym)) match {
156+
case Some(sym) =>
157+
toText(sym.name)
158+
case _ =>
159+
toTextPrefix(tp.prefix) ~ selectionString(tp)
160+
}
151161
case tp: TermParamRef =>
152162
ParamRefNameString(tp) ~ ".type"
153163
case tp: TypeParamRef =>

compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1111
private[this] def getPkgCls(path: String) =
1212
_ctx.requiredPackage(path).moduleClass.asClass
1313

14-
private lazy val collectionPkg = getPkgCls("scala.collection")
15-
private lazy val immutablePkg = getPkgCls("scala.collection.immutable")
16-
private lazy val scalaPkg = defn.ScalaPackageClass
17-
private lazy val javaLangPkg = defn.JavaLangPackageVal.moduleClass.asClass
18-
19-
def standardPkg(pkgSym: Symbol) = pkgSym match {
20-
case `scalaPkg` | `collectionPkg` | `immutablePkg` | `javaLangPkg` => true
21-
case _ => false
22-
}
23-
24-
def wrappedName(pkgSym: Symbol) =
25-
pkgSym.name.toTermName == nme.EMPTY_PACKAGE ||
26-
pkgSym.name.isReplWrapperName
27-
28-
def wellKnownPkg(pkgSym: Symbol) = standardPkg(pkgSym) || wrappedName(pkgSym)
29-
3014
override protected def keyString(sym: Symbol): String =
3115
if (sym.flagsUNSAFE is Package) "" else super.keyString(sym)
3216

@@ -55,22 +39,6 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
5539
override def toText(tp: Type): Text = tp match {
5640
case ExprType(result) => ":" ~~ toText(result)
5741
case tp: ConstantType => toText(tp.value)
58-
case tp: TypeRef => tp.info match {
59-
case TypeAlias(alias) => toText(alias)
60-
case _ => toText(tp.info)
61-
}
62-
case tp: ClassInfo => {
63-
if (wellKnownPkg(tp.cls.owner)) nameString(tp.cls.name)
64-
else {
65-
def printPkg(sym: ClassSymbol): Text =
66-
if (sym.owner == defn.RootClass || wrappedName(sym.owner))
67-
nameString(sym.name.stripModuleClassSuffix)
68-
else
69-
printPkg(sym.owner.asClass) ~ "." ~ toText(sym)
70-
71-
printPkg(tp.cls.owner.asClass) ~ "." ~ nameString(tp.cls.name)
72-
}
73-
}
7442
case tp => super.toText(tp)
7543
}
7644
}

compiler/test-resources/repl/importFromObj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ scala> import o._
77
scala> buf += xs
88
1 | buf += xs
99
| ^^
10-
| found: scala.collection.immutable.List[Int](o.xs)
10+
| found: List[Int](o.xs)
1111
| required: Int
1212
|
1313
scala> buf ++= xs
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
scala> type Identity[T] = T
2+
// defined alias type Identity = [T] => T
3+
scala> def foo[T](x: T): Identity[T] = x
4+
def foo[T](x: T): Identity[T]
5+
scala> foo(1)
6+
val res0: Identity[Int] = 1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
scala> List(1,2,3)
2+
val res0: List[Int] = List(1, 2, 3)
3+
scala> Map("foo" -> 1)
4+
val res1: Map[String, Int] = Map("foo" -> 1)
5+
scala> Seq('a','b')
6+
val res2: Seq[Char] = List(a, b)
7+
scala> Set(4, 5)
8+
val res3: Set[Int] = Set(4, 5)

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
493493
val DoesNotConformToBound(tpe, which, bound) :: Nil = messages
494494
assertEquals("Int", tpe.show)
495495
assertEquals("upper", which)
496-
assertEquals("scala.collection.immutable.List[Int]", bound.show)
496+
assertEquals("List[Int]", bound.show)
497497
}
498498

499499
@Test def doesNotConformToSelfType =

0 commit comments

Comments
 (0)