Skip to content

Always load REPL classes in macros including the output directory #16866

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
Feb 16, 2023
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
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)
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the REPL take care of passing the correct classloader to the Interpreter?

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 should, but I have not found a way to make it work.

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