Skip to content

Commit f1f3558

Browse files
committed
Avoid printing prefixes for all types aliased in scala.package or scala.Predef
In the previous commit, this was limited to a hardcoded subset of these types.
1 parent 53546ae commit f1f3558

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class Definitions {
194194
else NoSymbol)
195195
cls
196196
}
197+
lazy val ScalaPackageObjectRef = ctx.requiredModuleRef("scala.package")
197198
lazy val JavaPackageVal = ctx.requiredPackage("java")
198199
lazy val JavaLangPackageVal = ctx.requiredPackage("java.lang")
199200
// fundamental modules
@@ -357,16 +358,9 @@ class Definitions {
357358
def newGenericArrayMethod(implicit ctx: Context) = DottyArraysModule.requiredMethod("newGenericArray")
358359
def newArrayMethod(implicit ctx: Context) = DottyArraysModule.requiredMethod("newArray")
359360

360-
lazy val ListType = ctx.requiredClassRef("scala.collection.immutable.List")
361-
def ListClass(implicit ctx: Context) = ListType.symbol.asClass
362361
lazy val NilModuleRef = ctx.requiredModuleRef("scala.collection.immutable.Nil")
363362
def NilModule(implicit ctx: Context) = NilModuleRef.symbol
364363

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-
370364
lazy val SingletonClass: ClassSymbol =
371365
// needed as a synthetic class because Scala 2.x refers to it in classfiles
372366
// but does not define it as an explicit class.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ object Types {
700700
(name, buf) => buf += member(name).asSingleDenotation)
701701
}
702702

703+
/** The set of type alias members of this type */
704+
final def typeAliasMembers(implicit ctx: Context): Seq[SingleDenotation] = track("typeAlias") {
705+
memberDenots(typeAliasNameFilter,
706+
(name, buf) => buf += member(name).asSingleDenotation)
707+
}
708+
703709
/** The set of type members of this type */
704710
final def typeMembers(implicit ctx: Context): Seq[SingleDenotation] = track("typeMembers") {
705711
memberDenots(typeNameFilter,
@@ -4411,6 +4417,15 @@ object Types {
44114417
name.isTermName && pre.nonPrivateMember(name).hasAltWith(_.symbol is Deferred)
44124418
}
44134419

4420+
/** A filter for names of type aliases of a given type */
4421+
object typeAliasNameFilter extends NameFilter {
4422+
def apply(pre: Type, name: Name)(implicit ctx: Context): Boolean =
4423+
name.isTypeName && {
4424+
val mbr = pre.nonPrivateMember(name)
4425+
mbr.symbol.isAliasType
4426+
}
4427+
}
4428+
44144429
object typeNameFilter extends NameFilter {
44154430
def apply(pre: Type, name: Name)(implicit ctx: Context): Boolean = name.isTypeName
44164431
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ 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.
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+
*/
141142
private[this] lazy val printWithoutPrefix: Set[Symbol] =
142-
Set(defn.ListClass, defn.immutableMapClass, defn.immutableSetClass, defn.SeqClass)
143+
(defn.ScalaPredefModuleRef.typeAliasMembers
144+
++ defn.ScalaPackageObjectRef.typeAliasMembers).map(_.info.classSymbol).toSet
143145

144146
def toText(tp: Type): Text = controlled {
145147
homogenize(tp) match {
@@ -152,12 +154,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
152154
case tp: TermRef if tp.denot.isOverloaded =>
153155
"<overloaded " ~ toTextRef(tp) ~ ">"
154156
case tp: TypeRef =>
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-
}
157+
if (printWithoutPrefix.contains(tp.symbol))
158+
toText(tp.name)
159+
else
160+
toTextPrefix(tp.prefix) ~ selectionString(tp)
161161
case tp: TermParamRef =>
162162
ParamRefNameString(tp) ~ ".type"
163163
case tp: TypeParamRef =>

compiler/test-resources/type-printer/prefixless

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ scala> Seq('a','b')
66
val res2: Seq[Char] = List(a, b)
77
scala> Set(4, 5)
88
val res3: Set[Int] = Set(4, 5)
9+
scala> Iterator(1)
10+
val res4: Iterator[Int] = non-empty iterator

0 commit comments

Comments
 (0)