Skip to content

Commit 9a30e20

Browse files
authored
Merge pull request #4437 from dotty-staging/remove-show
Fix #4415: remove show from repl
2 parents ddf10a2 + ec958ac commit 9a30e20

File tree

10 files changed

+28
-240
lines changed

10 files changed

+28
-240
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

+17-10
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,33 @@ private[repl] class Rendering(compiler: ReplCompiler,
4343
myClassLoader
4444
}
4545

46-
/** Load the value of the symbol using reflection
46+
/** Load the value of the symbol using reflection.
4747
*
4848
* Calling this method evaluates the expression using reflection
4949
*/
5050
private[this] def valueOf(sym: Symbol)(implicit ctx: Context): Option[String] = {
5151
val defn = ctx.definitions
52-
val objectName = sym.owner.fullName.encode.toString.dropRight(1) // gotta drop the '$'
52+
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
5353
val resObj: Class[_] = Class.forName(objectName, true, classLoader())
54-
55-
val res =
54+
val value =
5655
resObj
57-
.getDeclaredMethods.find(_.getName == sym.name.toString + "Show").get
58-
.invoke(null).toString
59-
56+
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
57+
.map(_.invoke(null))
58+
val string = value.map {
59+
case null => "null" // Calling .toString on null => NPE
60+
case "" => "\"\"" // Special cased for empty string, following scalac
61+
case a: Array[_] => a.mkString("Array(", ", ", ")")
62+
case x => x.toString
63+
}
6064
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
6165
None
62-
else if (res.startsWith(str.REPL_SESSION_LINE))
63-
Some(res.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$'))
6466
else
65-
Some(res)
67+
string.map { s =>
68+
if (s.startsWith(str.REPL_SESSION_LINE))
69+
s.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
70+
else
71+
s
72+
}
6673
}
6774

6875
/** Render method definition result */

compiler/src/dotty/tools/repl/ReplCompiler.scala

+4-50
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
5353
}
5454

5555
(1 to objectIndex)
56-
.foldLeft(addImport("dotty.Show".toTermName)(initCtx)) { (ictx, i) =>
56+
.foldLeft(initCtx) { (ictx, i) =>
5757
addImport(nme.EMPTY_PACKAGE ++ "." ++ objectNames(i))(ictx)
5858
}
5959
}
@@ -74,67 +74,22 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
7474

7575
implicit val ctx: Context = state.run.runContext
7676

77-
def createShow(name: TermName, pos: Position) = {
78-
val showName = name ++ "Show"
79-
val select = Select(Ident(name), "show".toTermName)
80-
val valAsAnyRef = TypeApply(Select(Ident(name), nme.asInstanceOf_),
81-
List(Ident(tpnme.AnyRef)))
82-
val cond = InfixOp(valAsAnyRef,
83-
Ident(nme.EQ),
84-
Literal(Constant(null)))
85-
val showWithNullCheck = If(cond, Literal(Constant("null")), select)
86-
DefDef(showName, Nil, Nil, TypeTree(), showWithNullCheck).withFlags(Synthetic).withPos(pos)
87-
}
88-
89-
def createPatDefShows(patDef: PatDef) = {
90-
def createDeepShows(tree: untpd.Tree) = {
91-
class PatFolder extends UntypedDeepFolder[List[DefDef]] (
92-
(acc, tree) => tree match {
93-
case Ident(name) if name.isVariableName && name != nme.WILDCARD =>
94-
createShow(name.toTermName, tree.pos) :: acc
95-
case Bind(name, _) if name.isVariableName && name != nme.WILDCARD =>
96-
createShow(name.toTermName, tree.pos) :: acc
97-
case _ =>
98-
acc
99-
}
100-
)
101-
(new PatFolder).apply(Nil, tree).reverse
102-
}
103-
104-
// cannot fold over the whole tree because we need to generate show methods
105-
// for top level identifier starting with an uppercase (e.g. val X, Y = 2)
106-
patDef.pats.flatMap {
107-
case id @ Ident(name) if name != nme.WILDCARD =>
108-
List(createShow(name.toTermName, id.pos))
109-
case bd @ Bind(name, body) if name != nme.WILDCARD =>
110-
createShow(name.toTermName, bd.pos) :: createDeepShows(body)
111-
case other =>
112-
createDeepShows(other)
113-
}
114-
}
115-
11677
var valIdx = state.valIndex
11778

11879
val defs = trees.flatMap {
119-
case vd: ValDef =>
120-
List(vd, createShow(vd.name, vd.pos))
121-
case pd: PatDef =>
122-
pd :: createPatDefShows(pd)
12380
case expr @ Assign(id: Ident, rhs) =>
12481
// special case simple reassignment (e.g. x = 3)
12582
// in order to print the new value in the REPL
12683
val assignName = (id.name ++ str.REPL_ASSIGN_SUFFIX).toTermName
12784
val assign = ValDef(assignName, TypeTree(), id).withPos(expr.pos)
128-
val show = createShow(assignName, expr.pos)
129-
List(expr, assign, show)
85+
List(expr, assign)
13086
case expr if expr.isTerm =>
13187
val resName = (str.REPL_RES_PREFIX + valIdx).toTermName
13288
valIdx += 1
133-
val show = createShow(resName, expr.pos)
13489
val vd = ValDef(resName, TypeTree(), expr).withPos(expr.pos)
135-
List(vd, show)
90+
vd :: Nil
13691
case other =>
137-
List(other)
92+
other :: Nil
13893
}
13994

14095
Definitions(
@@ -154,7 +109,6 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
154109
* package <none> {
155110
* object rs$line$nextId {
156111
* import rs$line${i <- 0 until nextId}._
157-
* import dotty.Show._
158112
*
159113
* <trees>
160114
* }

compiler/test-resources/repl/i3388

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
scala> val foo = "1"; foo.toInt
2-
val foo: String = "1"
2+
val foo: String = 1
33
val res0: Int = 1
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scala> val a = Array(1, 2, 3)
2+
val a: Array[Int] = Array(1, 2, 3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scala> val s = ""
2+
val s: String = ""

compiler/test-resources/type-printer/prefixless

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
scala> List(1,2,3)
22
val res0: List[Int] = List(1, 2, 3)
33
scala> Map("foo" -> 1)
4-
val res1: Map[String, Int] = Map("foo" -> 1)
4+
val res1: Map[String, Int] = Map(foo -> 1)
55
scala> Seq('a','b')
66
val res2: Seq[Char] = List(a, b)
77
scala> Set(4, 5)

compiler/test-resources/type-printer/vals

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ val xs: List[Int] = List(1)
77
scala> scala.util.Try(1)
88
val res0: scala.util.Try[Int] = Success(1)
99
scala> Map(1 -> "one")
10-
val res1: Map[Int, String] = Map(1 -> "one")
10+
val res1: Map[Int, String] = Map(1 -> one)

library/src/dotty/Show.scala

-81
This file was deleted.

library/test/dotty/ShowTests.scala

-94
This file was deleted.

tests/neg/i3537.scala

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import dotty.Show._
2-
31
class Foo(x: Int)
42

53
object Test {

0 commit comments

Comments
 (0)