Skip to content

Fix #4415: remove show from repl #4437

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
May 7, 2018
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
27 changes: 17 additions & 10 deletions compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,33 @@ private[repl] class Rendering(compiler: ReplCompiler,
myClassLoader
}

/** Load the value of the symbol using reflection
/** Load the value of the symbol using reflection.
*
* Calling this method evaluates the expression using reflection
*/
private[this] def valueOf(sym: Symbol)(implicit ctx: Context): Option[String] = {
val defn = ctx.definitions
val objectName = sym.owner.fullName.encode.toString.dropRight(1) // gotta drop the '$'
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
val resObj: Class[_] = Class.forName(objectName, true, classLoader())

val res =
val value =
resObj
.getDeclaredMethods.find(_.getName == sym.name.toString + "Show").get
.invoke(null).toString

.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
.map(_.invoke(null))
val string = value.map {
case null => "null" // Calling .toString on null => NPE
case "" => "\"\"" // Special cased for empty string, following scalac
case a: Array[_] => a.mkString("Array(", ", ", ")")
case x => x.toString
}
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
None
else if (res.startsWith(str.REPL_SESSION_LINE))
Some(res.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$'))
else
Some(res)
string.map { s =>
if (s.startsWith(str.REPL_SESSION_LINE))
s.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
else
s
}
}

/** Render method definition result */
Expand Down
54 changes: 4 additions & 50 deletions compiler/src/dotty/tools/repl/ReplCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
}

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

implicit val ctx: Context = state.run.runContext

def createShow(name: TermName, pos: Position) = {
val showName = name ++ "Show"
val select = Select(Ident(name), "show".toTermName)
val valAsAnyRef = TypeApply(Select(Ident(name), nme.asInstanceOf_),
List(Ident(tpnme.AnyRef)))
val cond = InfixOp(valAsAnyRef,
Ident(nme.EQ),
Literal(Constant(null)))
val showWithNullCheck = If(cond, Literal(Constant("null")), select)
DefDef(showName, Nil, Nil, TypeTree(), showWithNullCheck).withFlags(Synthetic).withPos(pos)
}

def createPatDefShows(patDef: PatDef) = {
def createDeepShows(tree: untpd.Tree) = {
class PatFolder extends UntypedDeepFolder[List[DefDef]] (
(acc, tree) => tree match {
case Ident(name) if name.isVariableName && name != nme.WILDCARD =>
createShow(name.toTermName, tree.pos) :: acc
case Bind(name, _) if name.isVariableName && name != nme.WILDCARD =>
createShow(name.toTermName, tree.pos) :: acc
case _ =>
acc
}
)
(new PatFolder).apply(Nil, tree).reverse
}

// cannot fold over the whole tree because we need to generate show methods
// for top level identifier starting with an uppercase (e.g. val X, Y = 2)
patDef.pats.flatMap {
case id @ Ident(name) if name != nme.WILDCARD =>
List(createShow(name.toTermName, id.pos))
case bd @ Bind(name, body) if name != nme.WILDCARD =>
createShow(name.toTermName, bd.pos) :: createDeepShows(body)
case other =>
createDeepShows(other)
}
}

var valIdx = state.valIndex

val defs = trees.flatMap {
case vd: ValDef =>
List(vd, createShow(vd.name, vd.pos))
case pd: PatDef =>
pd :: createPatDefShows(pd)
case expr @ Assign(id: Ident, rhs) =>
// special case simple reassignment (e.g. x = 3)
// in order to print the new value in the REPL
val assignName = (id.name ++ str.REPL_ASSIGN_SUFFIX).toTermName
val assign = ValDef(assignName, TypeTree(), id).withPos(expr.pos)
val show = createShow(assignName, expr.pos)
List(expr, assign, show)
List(expr, assign)
case expr if expr.isTerm =>
val resName = (str.REPL_RES_PREFIX + valIdx).toTermName
valIdx += 1
val show = createShow(resName, expr.pos)
val vd = ValDef(resName, TypeTree(), expr).withPos(expr.pos)
List(vd, show)
vd :: Nil
case other =>
List(other)
other :: Nil
}

Definitions(
Expand All @@ -154,7 +109,6 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
* package <none> {
* object rs$line$nextId {
* import rs$line${i <- 0 until nextId}._
* import dotty.Show._
*
* <trees>
* }
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i3388
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
scala> val foo = "1"; foo.toInt
val foo: String = "1"
val foo: String = 1
val res0: Int = 1
2 changes: 2 additions & 0 deletions compiler/test-resources/repl/renderArray
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scala> val a = Array(1, 2, 3)
val a: Array[Int] = Array(1, 2, 3)
2 changes: 2 additions & 0 deletions compiler/test-resources/repl/renderEmptyString
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scala> val s = ""
val s: String = ""
2 changes: 1 addition & 1 deletion compiler/test-resources/type-printer/prefixless
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scala> List(1,2,3)
val res0: List[Int] = List(1, 2, 3)
scala> Map("foo" -> 1)
val res1: Map[String, Int] = Map("foo" -> 1)
val res1: Map[String, Int] = Map(foo -> 1)
scala> Seq('a','b')
val res2: Seq[Char] = List(a, b)
scala> Set(4, 5)
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/type-printer/vals
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ val xs: List[Int] = List(1)
scala> scala.util.Try(1)
val res0: scala.util.Try[Int] = Success(1)
scala> Map(1 -> "one")
val res1: Map[Int, String] = Map(1 -> "one")
val res1: Map[Int, String] = Map(1 -> one)
81 changes: 0 additions & 81 deletions library/src/dotty/Show.scala

This file was deleted.

94 changes: 0 additions & 94 deletions library/test/dotty/ShowTests.scala

This file was deleted.

2 changes: 0 additions & 2 deletions tests/neg/i3537.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import dotty.Show._

class Foo(x: Int)

object Test {
Expand Down