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 3 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
23 changes: 13 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,29 @@ 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).get
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this assume a getter? Do we generate a getter for case like this:

> private[this] val foo = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we're fine:

scala> private[this] val foo = 1
1 |private[this] val foo = 1
  |^^^^^^^
  |Illegal start of statement: no modifiers allowed here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess it doesn't hurt to avoid the .get...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if you could access the field instead

.invoke(null)
val string = value match {
case null => "null" // Calling .toString on null => NPE
case "" => "\"\"" // Sepcial cased for empty string, following scalac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: Special

case x => x.toString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another special case for Array?

}
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 if (string.startsWith(str.REPL_SESSION_LINE))
Some(string.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the code above is not new, but I'm wondering why this is needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I get it. It is to pretty print a class. However it quickly leaks:

scala> class Foo 
// defined class Foo
scala> new Foo 
val res0: Foo = Foo@7baf1f5a
scala> println(res0.toString) 
rs$line$1$Foo@7baf1f5a

It is the same in scalac, so I guess that's fine. I imagine we could generate a default toString for classes in the REPL, but it is maybe not worth the trouble

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it's fine to show the truth in this case, messing up with the object's toString might lead to more confusion...

else
Some(res)
Some(string)
}

/** 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/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