question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scala macros seem to peek at java internals when they shouldn't

See original GitHub issue

Compiler version

3.1.0

Minimized code

import jdk.incubator.foreign.CLinker
import scala.quoted.*

object compilerKiller {

   val instance = CLinker.getInstance

   inline def test = ${
      testImpl
   }

   def testImpl(using Quotes) =
      '{ println("test") }

}
@main def run = println(compilerKiller.test)

Output (click arrow to expand)

Exception in thread "MillServerActionRunner" java.lang.IllegalAccessError: class jdk.internal.foreign.CABI (in unnamed module @0x483e4dbb) cannot access class sun.security.action.GetPropertyAction (in module java.base) because module java.base does not export sun.security.action to unnamed module @0x483e4dbb
        at jdk.internal.foreign.CABI.<clinit>(CABI.java:42)
        at jdk.internal.foreign.PlatformLayouts$1.<clinit>(PlatformLayouts.java:38)
        at jdk.internal.foreign.PlatformLayouts.pick(PlatformLayouts.java:38)
        at jdk.incubator.foreign.CLinker.<clinit>(CLinker.java:241)
        at io.gitlab.mhammons.slinc.compilerKiller$.<clinit>(compilerKiller.scala:8)
        at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
        at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1155)
        at java.base/jdk.internal.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:42)
        at java.base/jdk.internal.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:185)
        at java.base/java.lang.reflect.Field.acquireFieldAccessor(Field.java:1132)
        at java.base/java.lang.reflect.Field.getFieldAccessor(Field.java:1113)
        at java.base/java.lang.reflect.Field.get(Field.java:425)
        at dotty.tools.dotc.transform.Splicer$Interpreter.loadModule(Splicer.scala:393)
        at dotty.tools.dotc.transform.Splicer$Interpreter.liftedTree1$1(Splicer.scala:357)
        at dotty.tools.dotc.transform.Splicer$Interpreter.interpretedStaticMethodCall(Splicer.scala:364)
        at dotty.tools.dotc.transform.Splicer$Interpreter.interpretTree(Splicer.scala:260)
        at dotty.tools.dotc.transform.Splicer$Interpreter.interpretTree$$anonfun$2(Splicer.scala:281)
        at dotty.tools.dotc.transform.Splicer$.$anonfun$2(Splicer.scala:56)
        at scala.Option.fold(Option.scala:263)
        at dotty.tools.dotc.transform.Splicer$.splice(Splicer.scala:56)
        at dotty.tools.dotc.typer.Inliner.dotty$tools$dotc$typer$Inliner$$expandMacro(Inliner.scala:1773)
        at dotty.tools.dotc.typer.Inliner$InlineTyper.typedApply(Inliner.scala:1551)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2755)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2818)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:121)
        at dotty.tools.dotc.typer.Inliner$InlineTyper.typedUnadapted(Inliner.scala:1634)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2883)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2887)
        at dotty.tools.dotc.typer.ReTyper.typedTyped(ReTyper.scala:62)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2760)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2818)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:121)
        at dotty.tools.dotc.typer.Inliner$InlineTyper.typedUnadapted(Inliner.scala:1634)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2883)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2880)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2887)
        at dotty.tools.dotc.typer.Inliner.inlined(Inliner.scala:1019)
        at dotty.tools.dotc.typer.Inliner$.inlineCall(Inliner.scala:155)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:85)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform$$anonfun$1(Trees.scala:1500)
        at scala.collection.immutable.List.mapConserve(List.scala:472)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1500)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1396)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:120)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:93)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:103)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:79)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.traverse$1(TreeMapWithImplicits.scala:53)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transformStats(TreeMapWithImplicits.scala:60)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:111)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:79)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1473)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:120)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:79)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.traverse$1(TreeMapWithImplicits.scala:53)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transformStats(TreeMapWithImplicits.scala:60)
        at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1484)
        at dotty.tools.dotc.ast.TreeMapWithImplicits.transform(TreeMapWithImplicits.scala:120)
        at dotty.tools.dotc.transform.Inlining$InliningTreeMap.transform(Inlining.scala:93)
        at dotty.tools.dotc.transform.Inlining$$anon$2.transform(Inlining.scala:71)
        at dotty.tools.dotc.transform.MacroTransform.run(MacroTransform.scala:21)
        at dotty.tools.dotc.transform.Inlining.run(Inlining.scala:43)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:308)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:309)
        at dotty.tools.dotc.transform.Inlining.runOn(Inlining.scala:47)
        at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:261)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
        at dotty.tools.dotc.Run.runPhases$5(Run.scala:272)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:280)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:68)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:289)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:228)
        at dotty.tools.dotc.Driver.finish(Driver.scala:60)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:40)
        at dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88)
        at dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
        at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
        at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:186)
        at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
        at sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:241)
        at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:176)
        at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:157)
        at sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
        at sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:157)
        at sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:204)
        at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
        at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
        at sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:174)
        at sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:172)
        at sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:457)
        at sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
        at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
        at sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
        at sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:261)
        at sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:412)
        at sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:499)
        at sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:399)
        at sbt.internal.inc.Incremental$.apply(Incremental.scala:166)
        at sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
        at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
        at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
        at sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
        at sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
        at mill.scalalib.worker.ZincWorkerImpl.compileInternal(ZincWorkerImpl.scala:512)
        at mill.scalalib.worker.ZincWorkerImpl.$anonfun$compileMixed0$1(ZincWorkerImpl.scala:311)
        at mill.api.FixSizedCache.withCachedValue(FixSizedCache.scala:62)
        at mill.scalalib.worker.ZincWorkerImpl.withCompilers(ZincWorkerImpl.scala:397)
        at mill.scalalib.worker.ZincWorkerImpl.compileMixed0(ZincWorkerImpl.scala:310)
        at mill.scalalib.worker.ZincWorkerImpl.compileMixed(ZincWorkerImpl.scala:280)
        at mill.scalalib.ScalaModule.$anonfun$compile$2(ScalaModule.scala:174)
        at mill.define.ApplyerGenerated.$anonfun$zipMap$9(ApplicativeGenerated.scala:21)
        at mill.define.Task$MappedDest.evaluate(Task.scala:396)
        at mill.eval.Evaluator.$anonfun$evaluateGroup$13(Evaluator.scala:496)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at scala.Console$.withErr(Console.scala:193)
        at mill.eval.Evaluator.$anonfun$evaluateGroup$12(Evaluator.scala:496)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at scala.Console$.withOut(Console.scala:164)
        at mill.eval.Evaluator.$anonfun$evaluateGroup$11(Evaluator.scala:495)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at scala.Console$.withIn(Console.scala:227)
        at mill.eval.Evaluator.$anonfun$evaluateGroup$8(Evaluator.scala:494)
        at mill.eval.Evaluator.$anonfun$evaluateGroup$8$adapted(Evaluator.scala:445)
        at scala.collection.immutable.Vector.foreach(Vector.scala:1856)
        at mill.eval.Evaluator.evaluateGroup(Evaluator.scala:445)
        at mill.eval.Evaluator.$anonfun$evaluateGroupCached$13(Evaluator.scala:342)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at mill.eval.Evaluator.evaluateGroupCached(Evaluator.scala:333)
        at mill.eval.Evaluator.$anonfun$sequentialEvaluate$2(Evaluator.scala:116)
        at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
        at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
        at scala.collection.AbstractIterator.foreach(Iterator.scala:1288)
        at mill.eval.Evaluator.sequentialEvaluate(Evaluator.scala:93)
        at mill.eval.Evaluator.evaluate(Evaluator.scala:79)
        at mill.main.RunScript$.evaluate(RunScript.scala:203)
        at mill.main.RunScript$.$anonfun$evaluateTasks$1(RunScript.scala:189)
        at scala.util.Either.map(Either.scala:382)
        at mill.main.RunScript$.evaluateTasks(RunScript.scala:188)
        at mill.main.RunScript$.$anonfun$runScript$4(RunScript.scala:67)
        at ammonite.util.Res$Success.flatMap(Res.scala:62)
        at mill.main.RunScript$.runScript(RunScript.scala:66)
        at mill.main.MainRunner.$anonfun$runScript$1(MainRunner.scala:119)
        at mill.main.MainRunner.watchLoop2(MainRunner.scala:68)
        at mill.main.MainRunner.runScript(MainRunner.scala:94)
        at mill.MillMain$.main0(MillMain.scala:299)
        at mill.main.MillServerMain$.main0(MillServerMain.scala:68)
        at mill.main.Server.$anonfun$handleRun$1(MillServerMain.scala:155)
        at java.base/java.lang.Thread.run(Thread.java:833)```
</details>

It seems this crash is caused by scala trying to peek behind the curtain. The question is why it's trying to introspect on instance when I don't use it in the macro code. In any case, with later versions of java, this kind of access blocking is probably going to get worse.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
markehammonscommented, Jan 18, 2022

It is in fact an issue with mill, and it’s much the same issue with sbt, but mill handles it better somehow. The issue is that the compiler itself needs to be launched with access to the foreign api, which with sbt entails adding .jvmopts:

--add-modules
jdk.incubator.foreign
--enable-native-access
ALL-UNNAMED

For some reason, mill doesn’t always need these flags to compile the code. In any case, I’m filing a bug with mill and closing this one.

1reaction
markehammonscommented, Nov 2, 2021

Sure thing. The project I got this error in is being developed in a docker/podman container, so I’ll put the dockerfile for that in there too (hopefully for a bit of ease in testing)

edit: here’s the minimal project for this issue https://github.com/markehammons/MacroBugExample

you trigger the bug with mill core.compile

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compilation failure is confirmed at the moment of accessing ...
In this way, it seems that an error occurs when accessing a typeSygnature for a specific symbol. I want to ignore this, hide...
Read more >
scala-internals - Alpha version of macros - Google Groups
It means two things: 1. Files defining macros cannot use them. 2. Software that uses self-defined macros need to be built with two...
Read more >
bazelbuild_rules_scala/Lobby - Gitter
I 'm trying to compile some scala code that uses ScalaTest, but I'm seeing errors like the following: error: exception during macro expansion:...
Read more >
Def Macros - Scala Documentation
Both in Scala IDE and in Intellij IDEA macros are known to work fine, given they are moved to a separate project. Debugging...
Read more >
Rust: A Scala Engineer's Perspective - BeachApe.
Or, you end up with potentially huge thread pools (à la old school Java Servlet apps..), which seems to go against Rust's spirit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found