Skip to content

Commit 831dec3

Browse files
authored
Merge pull request #6590 from dotty-staging/fix-3683
Fix #3683: print x.type for singleton types
2 parents d9c04cc + 69be52f commit 831dec3

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
147147
case tp: TypeParamRef =>
148148
ParamRefNameString(tp) ~ lambdaHash(tp.binder)
149149
case tp: SingletonType =>
150-
toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")"
150+
toTextSingleton(tp)
151151
case AppliedType(tycon, args) =>
152152
(toTextLocal(tycon) ~ "[" ~ argsText(args) ~ "]").close
153153
case tp: RefinedType =>
@@ -226,6 +226,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
226226
}
227227
}.close
228228

229+
def toTextSingleton(tp: SingletonType): Text =
230+
toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")"
231+
229232
protected def paramsText(tp: LambdaType): Text = {
230233
def paramText(name: Name, tp: Type) = toText(name) ~ toTextRHS(tp)
231234
Text((tp.paramNames, tp.paramInfos).zipped.map(paramText), ", ")

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,33 @@ import dotty.tools.dotc.core.Flags._
77
import dotty.tools.dotc.core.NameOps._
88
import dotty.tools.dotc.core.Names.Name
99
import dotty.tools.dotc.core.Symbols._
10-
import dotty.tools.dotc.core.Types.ExprType
10+
import dotty.tools.dotc.core.Types._
1111
import dotty.tools.dotc.printing.Texts._
1212

1313

1414
class ReplPrinter(_ctx: Context) extends DecompilerPrinter(_ctx) {
1515

16+
val debugPrint = _ctx.settings.YprintDebug.value
17+
1618
override def nameString(name: Name): String =
1719
if (name.isReplAssignName) name.decode.toString.takeWhile(_ != '$')
1820
else super.nameString(name)
1921

2022
override protected def exprToText(tp: ExprType): Text =
21-
": " ~ toText(tp.resType)
23+
if (debugPrint) super.exprToText(tp)
24+
else ": " ~ toText(tp.resType)
2225

2326
override def toText(sym: Symbol): Text =
2427
if (sym.name.isReplAssignName) nameString(sym.name)
28+
else if (debugPrint) super.toText(sym)
2529
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)
2630

2731
override def toText(const: Constant): Text =
28-
if (const.tag == Constants.StringTag) Str('"' + const.value.toString + '"')
32+
if (debugPrint) super.toText(const)
33+
else if (const.tag == Constants.StringTag) Str('"' + const.value.toString + '"')
2934
else Str(const.value.toString)
3035

31-
override def dclText(sym: Symbol): Text = {
36+
override def dclText(sym: Symbol): Text = if (debugPrint) super.dclText(sym) else {
3237
("lazy": Text).provided(sym.is(Lazy)) ~~
3338
toText(sym) ~ {
3439
if (sym.is(Method)) toText(sym.info)
@@ -38,6 +43,15 @@ class ReplPrinter(_ctx: Context) extends DecompilerPrinter(_ctx) {
3843
}
3944
}
4045

46+
override def toTextSingleton(tp: SingletonType): Text =
47+
if (debugPrint)
48+
super.toTextSingleton(tp)
49+
else
50+
tp match {
51+
case ConstantType(const) => toText(const)
52+
case _ => toTextRef(tp) ~ ".type"
53+
}
54+
4155
// We don't want the colors coming from RefinedPrinter as the REPL uses its
4256
// own syntax coloring mechanism.
4357
override def coloredStr(text: String, color: String): String = text

compiler/test-resources/repl/defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def baz(): Int
77
scala> def qux(): Int = 2
88
def qux(): Int
99
scala> def id(x: 4): 4 = x
10-
def id(x: Int(4)): Int(4)
10+
def id(x: 4): 4
1111
scala> id(4)
1212
val res0: Int = 4
1313
scala> def f given Int = 1

compiler/test-resources/repl/i5218

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ val tuple: (Int, String, Long) = (1,2,3)
33
scala> 0.0 *: tuple
44
val res0: (Double, Int, String, Long) = (0.0,1,2,3)
55
scala> tuple ++ tuple
6-
val res1: Int *: String *: Long *:
7-
scala.Tuple.Concat[Unit, (Int, String, Long)(tuple)] = (1,2,3,1,2,3)
6+
val res1: Int *: String *: Long *: scala.Tuple.Concat[Unit, tuple.type] = (1,2,3,1,2,3)

compiler/test-resources/type-printer/defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ def baz[A](a: A): A
77
scala> def qux[A](a: A): a.type = a
88
def qux[A](a: A): a.type
99
scala> def singleton: 1 = 1
10-
def singleton: Int(1)
10+
def singleton: 1

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ReplCompilerTests extends ReplTest {
1010

1111
@Test def compileSingle = fromInitialState { implicit state =>
1212
run("def foo: 1 = 1")
13-
assertEquals("def foo: Int(1)", storedOutput().trim)
13+
assertEquals("def foo: 1", storedOutput().trim)
1414
}
1515

1616
@Test def compileTwo =
@@ -165,4 +165,14 @@ class ReplCompilerTests extends ReplTest {
165165
run("IntOrd")
166166
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
167167
}
168+
169+
@Test def testSingletonPrint = fromInitialState { implicit state =>
170+
run("""val a = "hello"; val x: a.type = a""")
171+
assertEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
172+
}
173+
174+
@Test def i6574 = fromInitialState { implicit state =>
175+
run("val a: 1 | 0 = 1")
176+
assertEquals("val a: 1 | 0 = 1", storedOutput().trim)
177+
}
168178
}

0 commit comments

Comments
 (0)