Skip to content

Use ScalaRunTime.replStringOf to render values in the REPL #5226

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 1 commit into from
Oct 10, 2018
Merged
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
10 changes: 4 additions & 6 deletions compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.{ StringWriter, PrintWriter }
import java.lang.{ ClassLoader, ExceptionInInitializerError }
import java.lang.reflect.InvocationTargetException

import scala.runtime.ScalaRunTime

import dotc.core.Contexts.Context
import dotc.core.Denotations.Denotation
Expand Down Expand Up @@ -40,6 +41,8 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
myClassLoader
}

private[this] def MaxStringElements = 1000 // no need to mkString billions of elements
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know what's used in scalac?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

:shipit:


/** Load the value of the symbol using reflection.
*
* Calling this method evaluates the expression using reflection
Expand All @@ -52,12 +55,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
resObj
.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
}
val string = value.map(ScalaRunTime.replStringOf(_, MaxStringElements).trim)
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
None
else
Expand Down