Skip to content

Commit 4c99fde

Browse files
Always load REPL classes in macros including the output directory (#16866)
Fixes #15104 Also re-enable `compiler/test-resources/repl-macros` tests.
2 parents 60f2b96 + 94a6c67 commit 4c99fde

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ import dotty.tools.dotc.reporting.Message
3232
import dotty.tools.repl.AbstractFileClassLoader
3333

3434
/** Tree interpreter for metaprogramming constructs */
35-
class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
35+
class Interpreter(pos: SrcPos, classLoader0: ClassLoader)(using Context):
3636
import Interpreter._
3737
import tpd._
3838

39+
val classLoader =
40+
if ctx.owner.topLevelClass.name.startsWith(str.REPL_SESSION_LINE) then
41+
new AbstractFileClassLoader(ctx.settings.outputDir.value, classLoader0)
42+
else classLoader0
43+
3944
/** Local variable environment */
4045
type Env = Map[Symbol, Object]
4146
def emptyEnv: Env = Map.empty
@@ -157,18 +162,12 @@ class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
157162
args.toSeq
158163

159164
private def interpretedStaticMethodCall(moduleClass: Symbol, fn: Symbol, args: List[Object]): Object = {
160-
val (inst, clazz) =
161-
try
162-
if (moduleClass.name.startsWith(str.REPL_SESSION_LINE))
163-
(null, loadReplLineClass(moduleClass))
164-
else {
165-
val inst = loadModule(moduleClass)
166-
(inst, inst.getClass)
167-
}
165+
val inst =
166+
try loadModule(moduleClass)
168167
catch
169168
case MissingClassDefinedInCurrentRun(sym) =>
170169
suspendOnMissing(sym, pos)
171-
170+
val clazz = inst.getClass
172171
val name = fn.name.asTermName
173172
val method = getMethod(clazz, name, paramsSig(fn))
174173
stopIfRuntimeException(method.invoke(inst, args: _*), method)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
scala> import scala.quoted._
2+
scala> object Foo { def macroImpl(using Quotes) = Expr(1) }
3+
// defined object Foo
4+
scala> inline def foo = ${ Foo.macroImpl }
5+
def foo: Int
6+
scala> foo
7+
val res0: Int = 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
scala> import scala.quoted._
2+
scala> object Foo { def macroImpl(using Quotes) = Expr(1); inline def foo = ${ Foo.macroImpl } }
3+
// defined object Foo
4+
scala> Foo.foo
5+
val res0: Int = 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
scala> import scala.quoted._
2+
scala> def macroImpl(using Quotes) = Expr(1)
3+
def macroImpl(using x$1: quoted.Quotes): quoted.Expr[Int]
4+
scala> inline def foo = ${ macroImpl }
5+
def foo: Int
6+
scala> foo
7+
val res0: Int = 1

compiler/test-resources/repl-macros/i5551

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
scala> import scala.quoted._
22
scala> def assertImpl(expr: Expr[Boolean])(using q: Quotes) = '{ if !($expr) then throw new AssertionError("failed assertion")}
33
def assertImpl
4-
(expr: quoted.Expr[Boolean])
5-
(using q: quoted.Quotes): quoted.Expr[Unit]
4+
(expr: quoted.Expr[Boolean])(using q: quoted.Quotes): quoted.Expr[Unit]
65
scala> inline def assert(expr: => Boolean): Unit = ${ assertImpl('{expr}) }
76
def assert(expr: => Boolean): Unit
87

compiler/test/dotty/tools/repl/ScriptedTests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package tools
33
package repl
44

55
import org.junit.Test
6+
import org.junit.experimental.categories.Category
67

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

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

13+
@Category(Array(classOf[BootstrappedOnlyTests]))
14+
@Test def replMacrosTests = scripts("/repl-macros").foreach(testFile)
15+
1216
@Test def typePrinterTests = scripts("/type-printer").foreach(testFile)
1317
}
1418

0 commit comments

Comments
 (0)