Skip to content

Commit 2712b40

Browse files
Omit more prefixes in non-package module printing (#17758)
When we pretty-print a type in a module class, we end up printing the module class's symbol. For instance in a reduction failure of Tuple.Union, we end up printing Tuple.Fold, with Tuple being an internal (ThisType) reference to the Tuple module class. I tweaked the logic in fullNameString so that we omit more prefixes, if the module is non-package (and make it consistent across -Ytest-pickler). The package part is important, because we want to continue to get "import scala.concurrent.duration..." instead of "import concurrent.duration..".
2 parents 348729e + c04bb4f commit 2712b40

31 files changed

+96
-66
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
9292
nameString(if (ctx.property(XprintMode).isEmpty) sym.initial.name else sym.name)
9393

9494
override def fullNameString(sym: Symbol): String =
95-
if !sym.exists || isEmptyPrefix(sym.effectiveOwner) then nameString(sym)
95+
if !sym.exists
96+
|| isEmptyPrefix(sym.effectiveOwner)
97+
|| !homogenizedView && !sym.is(Package) && isOmittablePrefix(sym.effectiveOwner)
98+
then nameString(sym)
9699
else super.fullNameString(sym)
97100

98101
override protected def fullNameOwner(sym: Symbol): Symbol = {
@@ -106,6 +109,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
106109
if (tp.cls.isAnonymousClass) keywordStr("this")
107110
if (tp.cls.is(ModuleClass)) fullNameString(tp.cls.sourceModule)
108111
else super.toTextRef(tp)
112+
case tp: TermRef if !printDebug =>
113+
if tp.symbol.is(Package) then fullNameString(tp.symbol)
114+
else super.toTextRef(tp)
109115
case _ =>
110116
super.toTextRef(tp)
111117
}

compiler/test-resources/repl-macros/i15104c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
scala> import scala.quoted._
22
scala> def macroImpl(using Quotes) = Expr(1)
3-
def macroImpl(using x$1: quoted.Quotes): quoted.Expr[Int]
3+
def macroImpl(using x$1: scala.quoted.Quotes): scala.quoted.Expr[Int]
44
scala> inline def foo = ${ macroImpl }
55
def foo: Int
66
scala> foo

compiler/test-resources/repl-macros/i5551

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
scala> import scala.quoted._
22
scala> def assertImpl(expr: Expr[Boolean])(using q: Quotes) = '{ if !($expr) then throw new AssertionError("failed assertion")}
33
def assertImpl
4-
(expr: quoted.Expr[Boolean])(using q: quoted.Quotes): scala.quoted.Expr[Unit]
4+
(expr: scala.quoted.Expr[Boolean])
5+
(using q: scala.quoted.Quotes): scala.quoted.Expr[Unit]
56
scala> inline def assert(expr: => Boolean): Unit = ${ assertImpl('{expr}) }
67
def assert(expr: => Boolean): Unit
78

compiler/test-resources/repl/i10355

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
scala> import scala.quoted._
22
scala> def foo(expr: Expr[Any])(using Quotes) = expr match { case '{ $x: t } => '{ $x: Any } }
33
def foo
4-
(expr: quoted.Expr[Any])(using x$2: quoted.Quotes): scala.quoted.Expr[Any]
4+
(expr: scala.quoted.Expr[Any])
5+
(using x$2: scala.quoted.Quotes): scala.quoted.Expr[Any]
56
scala> def bar(expr: Expr[Any])(using Quotes) = expr match { case '{ $x: t } => '{ val a: t = ??? ; ???} }
67
def bar
7-
(expr: quoted.Expr[Any])(using x$2: quoted.Quotes): scala.quoted.Expr[Nothing]
8+
(expr: scala.quoted.Expr[Any])
9+
(using x$2: scala.quoted.Quotes): scala.quoted.Expr[Nothing]

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@ class CompletionTest {
3232

3333
@Test def completionFromScalaPackage: Unit = {
3434
code"class Foo { val foo: Conv${m1} }"
35-
.completion(("Conversion", Class, "scala.Conversion"))
35+
.completion(("Conversion", Class, "Conversion"))
3636
}
3737

3838
@Test def completionFromScalaPackageObject: Unit = {
3939
code"class Foo { val foo: BigD${m1} }"
4040
.completion(
41-
("BigDecimal", Field, "scala.BigDecimal"),
42-
("BigDecimal", Method, "=> math.BigDecimal.type"),
41+
("BigDecimal", Field, "BigDecimal"),
42+
("BigDecimal", Method, "=> scala.math.BigDecimal.type"),
4343
)
4444
}
4545

4646
@Test def completionFromSyntheticPackageObject: Unit = {
4747
code"class Foo { val foo: IArr${m1} }"
4848
.completion(
4949
("IArray", Module, "IArray"),
50-
("IArray", Field, "scala.IArray"),
50+
("IArray", Field, "IArray"),
5151
)
5252
}
5353

5454
@Test def completionFromJavaDefaults: Unit = {
5555
code"class Foo { val foo: Runn${m1} }"
5656
.completion(
57-
("Runnable", Class, "java.lang.Runnable"),
57+
("Runnable", Class, "Runnable"),
5858
("Runnable", Module, "Runnable"),
5959
)
6060
}
@@ -971,7 +971,7 @@ class CompletionTest {
971971
("implicitNotFound", Module, "scala.annotation.implicitNotFound"),
972972
)
973973
.completion(m2,
974-
("main", Class, "scala.main"),
974+
("main", Class, "main"),
975975
("main", Module, "main"),
976976
)
977977

staging/test-resources/repl-staging/i6007

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
scala> import scala.quoted._
22
scala> import quoted.staging.{Compiler => StagingCompiler, _}
33
scala> implicit def compiler: StagingCompiler = StagingCompiler.make(getClass.getClassLoader)
4-
def compiler: quoted.staging.Compiler
4+
def compiler: scala.quoted.staging.Compiler
55
scala> def v(using Quotes) = '{ (if true then Some(1) else None).map(v => v+1) }
6-
def v(using x$1: quoted.Quotes): scala.quoted.Expr[Option[Int]]
6+
def v(using x$1: scala.quoted.Quotes): scala.quoted.Expr[Option[Int]]
77
scala> scala.quoted.staging.withQuotes(v.show)
88
val res0: String = (if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))
99
scala> scala.quoted.staging.run(v)

staging/test-resources/repl-staging/i6263

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
scala> import quoted._
22
scala> import quoted.staging.{Compiler => StagingCompiler, _}
33
scala> implicit def compiler: StagingCompiler = StagingCompiler.make(getClass.getClassLoader)
4-
def compiler: quoted.staging.Compiler
4+
def compiler: scala.quoted.staging.Compiler
55
scala> def fn[T : Type](v : T) = println("ok")
6-
def fn[T](v: T)(implicit evidence$1: quoted.Type[T]): Unit
6+
def fn[T](v: T)(implicit evidence$1: scala.quoted.Type[T]): Unit
77
scala> withQuotes { fn("foo") }
88
ok
99
scala> withQuotes { fn((1,2)) }

tests/neg-custom-args/captures/cc-this2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
-- Error: tests/neg-custom-args/captures/cc-this2/D_2.scala:2:6 --------------------------------------------------------
33
2 |class D extends C: // error
44
|^
5-
|reference (scala.caps.cap : Any) is not included in allowed capture set {} of pure base class class C
5+
|reference (caps.cap : Any) is not included in allowed capture set {} of pure base class class C
66
3 | this: D^ =>

tests/neg-custom-args/captures/exception-definitions.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg-custom-args/captures/exception-definitions.scala:2:6 -----------------------------------------------
22
2 |class Err extends Exception: // error
33
|^
4-
|reference (scala.caps.cap : Any) is not included in allowed capture set {} of pure base class class Throwable
4+
|reference (caps.cap : Any) is not included in allowed capture set {} of pure base class class Throwable
55
3 | self: Err^ =>
66
-- Error: tests/neg-custom-args/captures/exception-definitions.scala:10:6 ----------------------------------------------
77
10 |class Err4(c: Any^) extends AnyVal // error

tests/neg-custom-args/explain/i16888.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E172] Type Error: tests/neg-custom-args/explain/i16888.scala:1:38 --------------------------------------------------
22
1 |def test = summon[scala.quoted.Quotes] // error
33
| ^
4-
| No given instance of type quoted.Quotes was found for parameter x of method summon in object Predef
4+
| No given instance of type scala.quoted.Quotes was found for parameter x of method summon in object Predef
55
|---------------------------------------------------------------------------------------------------------------------
66
| Explanation (enabled by `-explain`)
77
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

0 commit comments

Comments
 (0)