diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 318a4ccee25a..b54077fe9f7a 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 28ca14b7d501..f146a36fbfbb 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -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, @@ -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 } diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index 7b1c58667f8b..eeaf370e6517 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -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 => @@ -147,7 +154,10 @@ class PlainPrinter(_ctx: Context) extends Printer { case tp: TermRef if tp.denot.isOverloaded => "" 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 => diff --git a/compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala b/compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala index 0b0bbe13f47b..344d8ced20db 100644 --- a/compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/UserFacingPrinter.scala @@ -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) @@ -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) } } diff --git a/compiler/test-resources/repl/importFromObj b/compiler/test-resources/repl/importFromObj index 2bc88abd7693..f70b24d8d1c4 100644 --- a/compiler/test-resources/repl/importFromObj +++ b/compiler/test-resources/repl/importFromObj @@ -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 diff --git a/compiler/test-resources/type-printer/hkAlias b/compiler/test-resources/type-printer/hkAlias new file mode 100644 index 000000000000..14ee45b5a695 --- /dev/null +++ b/compiler/test-resources/type-printer/hkAlias @@ -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 diff --git a/compiler/test-resources/type-printer/prefixless b/compiler/test-resources/type-printer/prefixless new file mode 100644 index 000000000000..2101db7d3dff --- /dev/null +++ b/compiler/test-resources/type-printer/prefixless @@ -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 diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 61e114205027..e0611366e31e 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -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 =