Skip to content

Fix #3683: print x.type for singleton types #6590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
case tp: TypeParamRef =>
ParamRefNameString(tp) ~ lambdaHash(tp.binder)
case tp: SingletonType =>
toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")"
toTextSingleton(tp)
case AppliedType(tycon, args) =>
(toTextLocal(tycon) ~ "[" ~ argsText(args) ~ "]").close
case tp: RefinedType =>
Expand Down Expand Up @@ -226,6 +226,9 @@ class PlainPrinter(_ctx: Context) extends Printer {
}
}.close

def toTextSingleton(tp: SingletonType): Text =
toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")"

protected def paramsText(tp: LambdaType): Text = {
def paramText(name: Name, tp: Type) = toText(name) ~ toTextRHS(tp)
Text((tp.paramNames, tp.paramInfos).zipped.map(paramText), ", ")
Expand Down
22 changes: 18 additions & 4 deletions compiler/src/dotty/tools/dotc/printing/ReplPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,33 @@ import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameOps._
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types.ExprType
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.printing.Texts._


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

val debugPrint = _ctx.settings.YprintDebug.value

override def nameString(name: Name): String =
if (name.isReplAssignName) name.decode.toString.takeWhile(_ != '$')
else super.nameString(name)

override protected def exprToText(tp: ExprType): Text =
": " ~ toText(tp.resType)
if (debugPrint) super.exprToText(tp)
else ": " ~ toText(tp.resType)

override def toText(sym: Symbol): Text =
if (sym.name.isReplAssignName) nameString(sym.name)
else if (debugPrint) super.toText(sym)
else keyString(sym) ~~ nameString(sym.name.stripModuleClassSuffix)

override def toText(const: Constant): Text =
if (const.tag == Constants.StringTag) Str('"' + const.value.toString + '"')
if (debugPrint) super.toText(const)
else if (const.tag == Constants.StringTag) Str('"' + const.value.toString + '"')
else Str(const.value.toString)

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

override def toTextSingleton(tp: SingletonType): Text =
if (debugPrint)
super.toTextSingleton(tp)
else
tp match {
case ConstantType(const) => toText(const)
case _ => toTextRef(tp) ~ ".type"
}

// We don't want the colors coming from RefinedPrinter as the REPL uses its
// own syntax coloring mechanism.
override def coloredStr(text: String, color: String): String = text
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/defs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def baz(): Int
scala> def qux(): Int = 2
def qux(): Int
scala> def id(x: 4): 4 = x
def id(x: Int(4)): Int(4)
def id(x: 4): 4
scala> id(4)
val res0: Int = 4
scala> def f given Int = 1
Expand Down
3 changes: 1 addition & 2 deletions compiler/test-resources/repl/i5218
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ val tuple: (Int, String, Long) = (1,2,3)
scala> 0.0 *: tuple
val res0: (Double, Int, String, Long) = (0.0,1,2,3)
scala> tuple ++ tuple
val res1: Int *: String *: Long *:
scala.Tuple.Concat[Unit, (Int, String, Long)(tuple)] = (1,2,3,1,2,3)
val res1: Int *: String *: Long *: scala.Tuple.Concat[Unit, tuple.type] = (1,2,3,1,2,3)
2 changes: 1 addition & 1 deletion compiler/test-resources/type-printer/defs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def baz[A](a: A): A
scala> def qux[A](a: A): a.type = a
def qux[A](a: A): a.type
scala> def singleton: 1 = 1
def singleton: Int(1)
def singleton: 1
12 changes: 11 additions & 1 deletion compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReplCompilerTests extends ReplTest {

@Test def compileSingle = fromInitialState { implicit state =>
run("def foo: 1 = 1")
assertEquals("def foo: Int(1)", storedOutput().trim)
assertEquals("def foo: 1", storedOutput().trim)
}

@Test def compileTwo =
Expand Down Expand Up @@ -165,4 +165,14 @@ class ReplCompilerTests extends ReplTest {
run("IntOrd")
assertTrue(storedOutput().startsWith("val res0: IntOrd.type ="))
}

@Test def testSingletonPrint = fromInitialState { implicit state =>
run("""val a = "hello"; val x: a.type = a""")
assertEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
}

@Test def i6574 = fromInitialState { implicit state =>
run("val a: 1 | 0 = 1")
assertEquals("val a: 1 | 0 = 1", storedOutput().trim)
}
}