Skip to content

Commit

Permalink
Always load REPL classes in macros including the output directory (#1…
Browse files Browse the repository at this point in the history
…6866)

Fixes #15104

Also re-enable `compiler/test-resources/repl-macros` tests.
  • Loading branch information
nicolasstucki authored Feb 16, 2023
2 parents 60f2b96 + 94a6c67 commit 4c99fde
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
19 changes: 9 additions & 10 deletions compiler/src/dotty/tools/dotc/quoted/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ import dotty.tools.dotc.reporting.Message
import dotty.tools.repl.AbstractFileClassLoader

/** Tree interpreter for metaprogramming constructs */
class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
import Interpreter._
import tpd._

val classLoader =
if ctx.owner.topLevelClass.name.startsWith(str.REPL_SESSION_LINE) then
new AbstractFileClassLoader(ctx.settings.outputDir.value, classLoader0)
else classLoader0

/** Local variable environment */
type Env = Map[Symbol, Object]
def emptyEnv: Env = Map.empty
Expand Down Expand Up @@ -157,18 +162,12 @@ class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
args.toSeq

private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol, args: List[Object]): Object = {
val (inst, clazz) =
try
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE))
(null, loadReplLineClass(moduleClass))
else {
val inst = loadModule(moduleClass)
(inst, inst.getClass)
}
val inst =
try loadModule(moduleClass)
catch
case MissingClassDefinedInCurrentRun(sym) =>
suspendOnMissing(sym, pos)

val clazz = inst.getClass
val name = fn.name.asTermName
val method = getMethod(clazz, name, paramsSig(fn))
stopIfRuntimeException(method.invoke(inst, args: _*), method)
Expand Down
7 changes: 7 additions & 0 deletions compiler/test-resources/repl-macros/i15104a
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scala> import scala.quoted._
scala> object Foo { def macroImpl(using Quotes) = Expr(1) }
// defined object Foo
scala> inline def foo = ${ Foo.macroImpl }
def foo: Int
scala> foo
val res0: Int = 1
5 changes: 5 additions & 0 deletions compiler/test-resources/repl-macros/i15104b
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scala> import scala.quoted._
scala> object Foo { def macroImpl(using Quotes) = Expr(1); inline def foo = ${ Foo.macroImpl } }
// defined object Foo
scala> Foo.foo
val res0: Int = 1
7 changes: 7 additions & 0 deletions compiler/test-resources/repl-macros/i15104c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scala> import scala.quoted._
scala> def macroImpl(using Quotes) = Expr(1)
def macroImpl(using x$1: quoted.Quotes): quoted.Expr[Int]
scala> inline def foo = ${ macroImpl }
def foo: Int
scala> foo
val res0: Int = 1
3 changes: 1 addition & 2 deletions compiler/test-resources/repl-macros/i5551
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
scala> import scala.quoted._
scala> def assertImpl(expr: Expr[Boolean])(using q: Quotes) = '{ if !($expr) then throw new AssertionError("failed assertion")}
def assertImpl
(expr: quoted.Expr[Boolean])
(using q: quoted.Quotes): quoted.Expr[Unit]
(expr: quoted.Expr[Boolean])(using q: quoted.Quotes): quoted.Expr[Unit]
scala> inline def assert(expr: => Boolean): Unit = ${ assertImpl('{expr}) }
def assert(expr: => Boolean): Unit

Expand Down
4 changes: 4 additions & 0 deletions compiler/test/dotty/tools/repl/ScriptedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package tools
package repl

import org.junit.Test
import org.junit.experimental.categories.Category

/** Runs all tests contained in `compiler/test-resources/repl/` */
class ScriptedTests extends ReplTest {

@Test def replTests = scripts("/repl").foreach(testFile)

@Category(Array(classOf[BootstrappedOnlyTests]))
@Test def replMacrosTests = scripts("/repl-macros").foreach(testFile)

@Test def typePrinterTests = scripts("/type-printer").foreach(testFile)
}

Expand Down

0 comments on commit 4c99fde

Please sign in to comment.