Skip to content

Commit 26a8679

Browse files
committed
currentRun.compiles now correctly works in toolboxes
Another random bug uncovered and extinguished when hacking macro annots.
1 parent ed5c1ab commit 26a8679

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/compiler/scala/tools/reflect/ToolBoxFactory.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.lang.{Class => jClass}
1515
import scala.compat.Platform.EOL
1616
import scala.reflect.NameTransformer
1717
import scala.reflect.api.JavaUniverse
18+
import scala.reflect.io.NoAbstractFile
1819

1920
abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
2021

@@ -138,7 +139,9 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
138139
val wrapper2 = if (!withMacrosDisabled) (currentTyper.context.withMacrosEnabled[Tree] _) else (currentTyper.context.withMacrosDisabled[Tree] _)
139140
def wrapper (tree: => Tree) = wrapper1(wrapper2(tree))
140141

141-
phase = (new Run).typerPhase // need to set a phase to something <= typerPhase, otherwise implicits in typedSelect will be disabled
142+
val run = new Run
143+
run.symSource(ownerClass) = NoAbstractFile // need to set file to something different from null, so that currentRun.defines works
144+
phase = run.typerPhase // need to set a phase to something <= typerPhase, otherwise implicits in typedSelect will be disabled
142145
currentTyper.context.setReportErrors() // need to manually set context mode, otherwise typer.silent will throw exceptions
143146
reporter.reset()
144147

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
true
2+
false
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pkg {
2+
import scala.reflect.macros.Context
3+
import scala.language.experimental.macros
4+
5+
object Macros {
6+
def impl[T: c.WeakTypeTag](c: Context) = {
7+
import c.universe._
8+
val sym = c.weakTypeOf[T].typeSymbol
9+
val g = c.universe.asInstanceOf[scala.tools.nsc.Global]
10+
c.Expr[Boolean](Literal(Constant(g.currentRun.compiles(sym.asInstanceOf[g.Symbol]))))
11+
}
12+
def compiles[T] = macro impl[T]
13+
}
14+
}
15+
16+
import scala.reflect.runtime.universe._
17+
import scala.reflect.runtime.{universe => ru}
18+
import scala.tools.reflect.ToolBox
19+
20+
object Test extends App {
21+
val cm = ru.runtimeMirror(getClass.getClassLoader)
22+
val toolbox = cm.mkToolBox()
23+
toolbox.eval(toolbox.parse("""{
24+
class C
25+
println(pkg.Macros.compiles[C])
26+
println(pkg.Macros.compiles[Object])
27+
}"""))
28+
}

0 commit comments

Comments
 (0)