Skip to content

Commit 5759e66

Browse files
committed
REPL: Fix Rendering's rewrapValueClass
1 parent 439a17d commit 5759e66

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import dotc.core.Contexts._
1010
import dotc.core.Denotations.Denotation
1111
import dotc.core.Flags
1212
import dotc.core.Flags._
13+
import dotc.core.NameOps.*
1314
import dotc.core.Symbols.{Symbol, defn}
1415
import dotc.core.StdNames.{nme, str}
1516
import dotc.printing.ReplPrinter
1617
import dotc.reporting.Diagnostic
1718
import dotc.transform.ValueClasses
1819

20+
import scala.util.control.NonFatal
21+
1922
/** This rendering object uses `ClassLoader`s to accomplish crossing the 4th
2023
* wall (i.e. fetching back values from the compiled class files put into a
2124
* specific class loader capable of loading from memory) and rendering them.
@@ -106,7 +109,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
106109
*
107110
* Calling this method evaluates the expression using reflection
108111
*/
109-
private def valueOf(sym: Symbol)(using Context): Option[String] =
112+
private def valueOf(sym: Symbol)(using Context): Option[String] = try
110113
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
111114
val resObj: Class[?] = Class.forName(objectName, true, classLoader())
112115
val symValue = resObj
@@ -123,6 +126,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
123126
else
124127
s
125128
}
129+
catch case e: ReflectiveOperationException => throw InvocationTargetException(e)
126130

127131
/** Rewrap value class to their Wrapper class
128132
*
@@ -131,7 +135,13 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
131135
*/
132136
private def rewrapValueClass(sym: Symbol, value: Object)(using Context): Option[Object] =
133137
if ValueClasses.isDerivedValueClass(sym) then
134-
val valueClassName = sym.flatName.encode.toString
138+
val pkg = sym.enclosingPackageClass
139+
val pkgName = if pkg.isEmptyPackage then "" else s"${pkg.fullName.mangledString}."
140+
val clsFlatName = if sym.isOneOf(JavaDefined | ConstructorProxy) then
141+
// See ExtractDependencies.recordDependency
142+
sym.flatName.stripModuleClassSuffix
143+
else sym.flatName
144+
val valueClassName = pkgName + clsFlatName.mangledString
135145
val valueClass = Class.forName(valueClassName, true, classLoader())
136146
valueClass.getConstructors.headOption.map(_.newInstance(value))
137147
else
@@ -161,7 +171,6 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
161171

162172
/** Force module initialization in the absence of members. */
163173
def forceModule(sym: Symbol)(using Context): Seq[Diagnostic] =
164-
import scala.util.control.NonFatal
165174
def load() =
166175
val objectName = sym.fullName.encode.toString
167176
Class.forName(objectName, true, classLoader())

compiler/test-resources/repl/i15493

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,8 @@ val res33: Outer.Foo = Outer$Foo@17
142142
scala> res33.toString
143143
val res34: String = Outer$Foo@17
144144

145+
scala> Vector.unapplySeq(Vector(2))
146+
val res35: scala.collection.SeqFactory.UnapplySeqWrapper[Int] = scala.collection.SeqFactory$UnapplySeqWrapper@df507bfd
147+
148+
scala> new scala.concurrent.duration.DurationInt(5)
149+
val res36: scala.concurrent.duration.package.DurationInt = scala.concurrent.duration.package$DurationInt@5

0 commit comments

Comments
 (0)