Skip to content

REPL: do not dealias before printing types #3566

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 2 commits into from
Nov 30, 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/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class Definitions {
else NoSymbol)
cls
}
lazy val ScalaPackageObjectRef = ctx.requiredModuleRef("scala.package")
lazy val JavaPackageVal = ctx.requiredPackage("java")
lazy val JavaLangPackageVal = ctx.requiredPackage("java.lang")
// fundamental modules
Expand Down
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@ object Types {
(name, buf) => buf += member(name).asSingleDenotation)
}

/** The set of type alias members of this type */
final def typeAliasMembers(implicit ctx: Context): Seq[SingleDenotation] = track("typeAlias") {
memberDenots(typeAliasNameFilter,
(name, buf) => buf += member(name).asSingleDenotation)
}

/** The set of type members of this type */
final def typeMembers(implicit ctx: Context): Seq[SingleDenotation] = track("typeMembers") {
memberDenots(typeNameFilter,
Expand Down Expand Up @@ -4411,6 +4417,15 @@ object Types {
name.isTermName && pre.nonPrivateMember(name).hasAltWith(_.symbol is Deferred)
}

/** A filter for names of type aliases of a given type */
object typeAliasNameFilter extends NameFilter {
def apply(pre: Type, name: Name)(implicit ctx: Context): Boolean =
name.isTypeName && {
val mbr = pre.nonPrivateMember(name)
mbr.symbol.isAliasType
}
}

object typeNameFilter extends NameFilter {
def apply(pre: Type, name: Name)(implicit ctx: Context): Boolean = name.isTypeName
}
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
case _ => Nil
})

/** Direct references to these symbols are printed without their prefix for convenience.
* They are either aliased in scala.Predef or in the scala package object.
*/
private[this] lazy val printWithoutPrefix: Set[Symbol] =
(defn.ScalaPredefModuleRef.typeAliasMembers
++ defn.ScalaPackageObjectRef.typeAliasMembers).map(_.info.classSymbol).toSet

def toText(tp: Type): Text = controlled {
homogenize(tp) match {
case tp: TypeType =>
Expand All @@ -147,7 +154,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
case tp: TermRef if tp.denot.isOverloaded =>
"<overloaded " ~ toTextRef(tp) ~ ">"
case tp: TypeRef =>
toTextPrefix(tp.prefix) ~ selectionString(tp)
if (printWithoutPrefix.contains(tp.symbol))
toText(tp.name)
else
toTextPrefix(tp.prefix) ~ selectionString(tp)
case tp: TermParamRef =>
ParamRefNameString(tp) ~ ".type"
case tp: TypeParamRef =>
Expand Down
32 changes: 0 additions & 32 deletions compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
private[this] def getPkgCls(path: String) =
_ctx.requiredPackage(path).moduleClass.asClass

private lazy val collectionPkg = getPkgCls("scala.collection")
private lazy val immutablePkg = getPkgCls("scala.collection.immutable")
private lazy val scalaPkg = defn.ScalaPackageClass
private lazy val javaLangPkg = defn.JavaLangPackageVal.moduleClass.asClass

def standardPkg(pkgSym: Symbol) = pkgSym match {
case `scalaPkg` | `collectionPkg` | `immutablePkg` | `javaLangPkg` => true
case _ => false
}

def wrappedName(pkgSym: Symbol) =
pkgSym.name.toTermName == nme.EMPTY_PACKAGE ||
pkgSym.name.isReplWrapperName

def wellKnownPkg(pkgSym: Symbol) = standardPkg(pkgSym) || wrappedName(pkgSym)

override protected def keyString(sym: Symbol): String =
if (sym.flagsUNSAFE is Package) "" else super.keyString(sym)

Expand Down Expand Up @@ -55,22 +39,6 @@ class UserFacingPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
override def toText(tp: Type): Text = tp match {
case ExprType(result) => ":" ~~ toText(result)
case tp: ConstantType => toText(tp.value)
case tp: TypeRef => tp.info match {
case TypeAlias(alias) => toText(alias)
case _ => toText(tp.info)
}
case tp: ClassInfo => {
if (wellKnownPkg(tp.cls.owner)) nameString(tp.cls.name)
else {
def printPkg(sym: ClassSymbol): Text =
if (sym.owner == defn.RootClass || wrappedName(sym.owner))
nameString(sym.name.stripModuleClassSuffix)
else
printPkg(sym.owner.asClass) ~ "." ~ toText(sym)

printPkg(tp.cls.owner.asClass) ~ "." ~ nameString(tp.cls.name)
}
}
case tp => super.toText(tp)
}
}
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/importFromObj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ scala> import o._
scala> buf += xs
1 | buf += xs
| ^^
| found: scala.collection.immutable.List[Int](o.xs)
| found: List[Int](o.xs)
| required: Int
|
scala> buf ++= xs
Expand Down
6 changes: 6 additions & 0 deletions compiler/test-resources/type-printer/hkAlias
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scala> type Identity[T] = T
// defined alias type Identity = [T] => T
scala> def foo[T](x: T): Identity[T] = x
def foo[T](x: T): Identity[T]
scala> foo(1)
val res0: Identity[Int] = 1
10 changes: 10 additions & 0 deletions compiler/test-resources/type-printer/prefixless
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
scala> List(1,2,3)
val res0: List[Int] = List(1, 2, 3)
scala> Map("foo" -> 1)
val res1: Map[String, Int] = Map("foo" -> 1)
scala> Seq('a','b')
val res2: Seq[Char] = List(a, b)
scala> Set(4, 5)
val res3: Set[Int] = Set(4, 5)
scala> Iterator(1)
val res4: Iterator[Int] = non-empty iterator
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val DoesNotConformToBound(tpe, which, bound) :: Nil = messages
assertEquals("Int", tpe.show)
assertEquals("upper", which)
assertEquals("scala.collection.immutable.List[Int]", bound.show)
assertEquals("List[Int]", bound.show)
}

@Test def doesNotConformToSelfType =
Expand Down