Skip to content

Fix #5551: Handle repl line modules in splicer #5688

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
Jan 12, 2019
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
23 changes: 18 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameKinds.FlatName
import dotty.tools.dotc.core.Names.{Name, TermName}
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.StdNames.str.MODULE_INSTANCE_FIELD
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.Symbols._
Expand All @@ -22,6 +21,7 @@ import dotty.tools.dotc.tastyreflect.ReflectionImpl

import scala.util.control.NonFatal
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.repl.AbstractFileClassLoader

import scala.reflect.ClassTag

Expand Down Expand Up @@ -113,14 +113,22 @@ object Splicer {
}

protected def interpretStaticMethodCall(moduleClass: Symbol, fn: Symbol, args: => List[Object])(implicit env: Env): Object = {
val instance = loadModule(moduleClass)
val (instance, clazz) =
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE)) {
(null, loadReplLineClass(moduleClass))
} else {
val instance = loadModule(moduleClass)
(instance, instance.getClass)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need instance in the return? Seems it is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

def getDirectName(tp: Type, name: TermName): TermName = tp.widenDealias match {
case tp: AppliedType if defn.isImplicitFunctionType(tp) =>
getDirectName(tp.args.last, NameKinds.DirectMethodName(name))
case _ => name
}

val name = getDirectName(fn.info.finalResultType, fn.name.asTermName)
val method = getMethod(instance.getClass, name, paramsSig(fn))
val method = getMethod(clazz, name, paramsSig(fn))
stopIfRuntimeException(method.invoke(instance, args: _*))
}

Expand All @@ -140,14 +148,19 @@ object Splicer {
if (sym.owner.is(Package)) {
// is top level object
val moduleClass = loadClass(sym.fullName)
moduleClass.getField(MODULE_INSTANCE_FIELD).get(null)
moduleClass.getField(str.MODULE_INSTANCE_FIELD).get(null)
} else {
// nested object in an object
val clazz = loadClass(sym.fullNameSeparated(FlatName))
clazz.getConstructor().newInstance().asInstanceOf[Object]
}
}

private def loadReplLineClass(moduleClass: Symbol)(implicit env: Env): Class[_] = {
val lineClassloader = new AbstractFileClassLoader(ctx.settings.outputDir.value, classLoader)
lineClassloader.loadClass(moduleClass.name.firstPart.toString)
}

private def loadClass(name: Name): Class[_] = {
try classLoader.loadClass(name.toString)
catch {
Expand Down
12 changes: 12 additions & 0 deletions compiler/test-resources/repl/i5551
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
scala> import scala.quoted._

scala> def assertImpl(expr: Expr[Boolean]) = '{ if !(~expr) then throw new AssertionError("failed assertion")}
def assertImpl(expr: quoted.Expr[Boolean]): quoted.Expr[Unit]

scala> inline def assert(expr: => Boolean): Unit = ~ assertImpl('(expr))
def assert(expr: => Boolean): Unit

scala> assert(0 == 0)

scala> try assert(0 == 1) catch { case _: AssertionError => println("ok") }
ok