Skip to content

Commit

Permalink
[nomerge] Restore findMacroClassloader into Analyzer for the 2.12.x s…
Browse files Browse the repository at this point in the history
…eries
  • Loading branch information
retronym committed Feb 28, 2019
1 parent 141a72f commit 7707a76
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/plugins/Plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ trait Plugins { global: Global =>
*
* Mirrors with runtime definitions (e.g. Repl) need to adjust this method.
*/
protected[scala] def findMacroClassLoader(): ClassLoader = {
protected def findMacroClassLoader(): ClassLoader = {
val classpath: Seq[URL] = if (settings.YmacroClasspath.isSetByUser) {
for {
file <- scala.tools.nsc.util.ClassPath.expandPath(settings.YmacroClasspath.value, true)
Expand Down
37 changes: 37 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ trait Macros extends MacroRuntimes with Traces with Helpers {

def globalSettings = global.settings

/** Obtains a `ClassLoader` instance used for macro expansion.
*
* By default a new `ScalaClassLoader` is created using the classpath
* from global and the classloader of self as parent.
*
* Mirrors with runtime definitions (e.g. Repl) need to adjust this method.
*/
protected def findMacroClassLoader(): ClassLoader = {
import java.net.URL
import scala.tools.nsc.io.AbstractFile

val classpath: Seq[URL] = if (settings.YmacroClasspath.isSetByUser) {
for {
file <- scala.tools.nsc.util.ClassPath.expandPath(settings.YmacroClasspath.value, true)
af <- Option(AbstractFile getDirectory file)
} yield af.file.toURI.toURL
} else global.classPath.asURLs
def newLoader: () => ScalaClassLoader.URLClassLoader = () => {
analyzer.macroLogVerbose("macro classloader: initializing from -cp: %s".format(classpath))
ScalaClassLoader.fromURLs(classpath, getClass.getClassLoader)
}

val policy = settings.YcacheMacroClassLoader.value
val cache = Macros.macroClassLoadersCache
val disableCache = policy == settings.CachePolicy.None.name
val checkStamps = policy == settings.CachePolicy.LastModified.name
cache.checkCacheability(classpath, checkStamps, disableCache) match {
case Left(msg) =>
analyzer.macroLogVerbose(s"macro classloader: $msg.")
val loader = newLoader()
closeableRegistry.registerClosable(loader)
loader
case Right(paths) =>
cache.getOrCreate(paths, newLoader, closeableRegistry, checkStamps)
}
}

/** `MacroImplBinding` and its companion module are responsible for
* serialization/deserialization of macro def -> impl bindings.
*
Expand Down
20 changes: 12 additions & 8 deletions src/compiler/scala/tools/reflect/ReflectGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ import scala.tools.nsc.typechecker.Analyzer
class ReflectGlobal(currentSettings: Settings, reporter: Reporter, override val rootClassLoader: ClassLoader)
extends Global(currentSettings, reporter) with scala.tools.reflect.ReflectSetup with scala.reflect.runtime.SymbolTable {

/** Obtains the classLoader used for runtime macro expansion.
*
* Macro expansion can use everything available in `global.classPath` or `rootClassLoader`.
* The `rootClassLoader` is used to obtain runtime defined macros.
*/
override protected[scala] def findMacroClassLoader(): ClassLoader = {
val classpath = classPath.asURLs
perRunCaches.recordClassloader(ScalaClassLoader.fromURLs(classpath, rootClassLoader))
override lazy val analyzer = new {
val global: ReflectGlobal.this.type = ReflectGlobal.this
} with Analyzer {
/** Obtains the classLoader used for runtime macro expansion.
*
* Macro expansion can use everything available in [[global.classPath]] or [[rootClassLoader]].
* The [[rootClassLoader]] is used to obtain runtime defined macros.
*/
override protected def findMacroClassLoader(): ClassLoader = {
val classpath = global.classPath.asURLs
ScalaClassLoader.fromURLs(classpath, rootClassLoader)
}
}

override def transformedType(sym: Symbol) =
Expand Down
14 changes: 9 additions & 5 deletions src/repl/scala/tools/nsc/interpreter/ReplGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ trait ReplGlobal extends Global {
super.abort(msg)
}

override protected[scala] def findMacroClassLoader(): ClassLoader = {
val loader = super.findMacroClassLoader
analyzer.macroLogVerbose("macro classloader: initializing from a REPL classloader: %s".format(classPath.asURLs))
val virtualDirectory = analyzer.globalSettings.outputDirs.getSingleOutput.get
new util.AbstractFileClassLoader(virtualDirectory, loader) {}
override lazy val analyzer = new {
val global: ReplGlobal.this.type = ReplGlobal.this
} with Analyzer {
override protected def findMacroClassLoader(): ClassLoader = {
val loader = super.findMacroClassLoader
macroLogVerbose("macro classloader: initializing from a REPL classloader: %s".format(global.classPath.asURLs))
val virtualDirectory = globalSettings.outputDirs.getSingleOutput.get
new util.AbstractFileClassLoader(virtualDirectory, loader) {}
}
}

override def optimizerClassPath(base: ClassPath): ClassPath = {
Expand Down

0 comments on commit 7707a76

Please sign in to comment.