Skip to content

Commit d8d9f61

Browse files
committed
Fix #3683: print x.type for singleton types
1 parent 3361d44 commit d8d9f61

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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

@@ -38,6 +38,11 @@ class ReplPrinter(_ctx: Context) extends DecompilerPrinter(_ctx) {
3838
}
3939
}
4040

41+
override def toTextSingleton(tp: SingletonType): Text = tp match {
42+
case ConstantType(const) => toText(const)
43+
case _ => toTextRef(tp) ~ ".type"
44+
}
45+
4146
// We don't want the colors coming from RefinedPrinter as the REPL uses its
4247
// own syntax coloring mechanism.
4348
override def coloredStr(text: String, color: String): String = text

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ 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+
}
168173
}

0 commit comments

Comments
 (0)