Skip to content

Provide a context ClassLoader to macros #9516

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
Aug 11, 2020
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
20 changes: 13 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ object Splicer {
val macroOwner = newSymbol(ctx.owner, nme.MACROkw, Macro | Synthetic, defn.AnyType, coord = tree.span)
try
inContext(ctx.withOwner(macroOwner)) {
val interpreter = new Interpreter(pos, classLoader)

// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val interpretedExpr = interpreter.interpret[scala.quoted.QuoteContext => scala.quoted.Expr[Any]](tree)
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContext())))

checkEscapedVariables(interpretedTree, macroOwner)
val oldContextClassLoader = Thread.currentThread().getContextClassLoader
Thread.currentThread().setContextClassLoader(classLoader)
try {
val interpreter = new Interpreter(pos, classLoader)

// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val interpretedExpr = interpreter.interpret[scala.quoted.QuoteContext => scala.quoted.Expr[Any]](tree)
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContext())))

checkEscapedVariables(interpretedTree, macroOwner)
} finally {
Thread.currentThread().setContextClassLoader(oldContextClassLoader)
}
}.changeOwner(macroOwner, ctx.owner)
catch {
case ex: CompilationUnit.SuspendException =>
Expand Down
3 changes: 3 additions & 0 deletions tests/pos-macros/macro-classloaders/Caller.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Caller {
Macro.f
}
15 changes: 15 additions & 0 deletions tests/pos-macros/macro-classloaders/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.net.URLClassLoader

import scala.quoted._

object Macro { self =>
inline def f: Any = ${ impl }

def impl(using QuoteContext): Expr[Any] = {
//println("======== "+self.getClass.getClassLoader.asInstanceOf[URLClassLoader].getURLs.mkString("; "))
//println(" ====== "+Thread.currentThread().getContextClassLoader.asInstanceOf[URLClassLoader].getURLs.mkString("; "))
assert(getClass.getClassLoader eq Thread.currentThread().getContextClassLoader,
"Macro ClassLoader should be available as context ClassLoader")
'{""}
}
}