File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed
src/compiler/scala/tools/reflect Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import java.lang.{Class => jClass}
1515import scala .compat .Platform .EOL
1616import scala .reflect .NameTransformer
1717import scala .reflect .api .JavaUniverse
18+ import scala .reflect .io .NoAbstractFile
1819
1920abstract 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
Original file line number Diff line number Diff line change 1+ true
2+ false
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments