You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since whitebox macro expansion is forced here, when the typer is an implicit search all exceptions thrown by the macro implementation are caught and swallowed as "implicit not found", including fatal errors like out of memory or stack overflow.
This doesn't happen with blackbox macros, because they are delayed and expanded after the implicit search.
Example:
objectMacros {
traitBlackBox[A]
objectBlackBox {
implicitdefmaterialize[A]:BlackBox[A] = macro materializeImpl[A]
defmaterializeImpl[A](c: blackbox.Context): c.Tree=thrownewOutOfMemoryError
}
traitWhiteBox[A]
objectWhiteBox {
implicitdefmaterialize[A]:WhiteBox[A] = macro materializeImpl[A]
defmaterializeImpl[A](c: whitebox.Context): c.Tree=thrownewOutOfMemoryError
}
}
objectTest {
importMacros._
implicitly[BlackBox[Int]] // out of memory
implicitly[WhiteBox[Int]] // implicit not found
}
The behavior for both blackbox and whitebox macros is unexpected for me.
java.lang.reflect.InvocationTargetException
so-test.scala:3:error: exception during macro expansion:
java.lang.OutOfMemoryError
at Macros$BlackBox$.materializeImpl(so.scala:8)
implicitly[BlackBox[Int]] // out of memory
^
java.lang.reflect.InvocationTargetException
so-test.scala:4:error: could not find implicit value for parameter e: Macros.WhiteBox[Int]
implicitly[WhiteBox[Int]] // implicit not found
^
two errors found
The behavior for whitebox macros is arguably worse, but I would expect fatal errors to propagate instead of get reported. Is there a good reason to not use NonFatal where Throwable is caught?
I opened scala/scala#6220 changing the error handling to propagate fatal exceptions.
I have no idea how error handling works in the compiler. Does rethrowing the fatal error crash it? Does the stack trace then include the source location where the macro was called? In general it's a good idea to guard against poorly written macros, but "implicit not found" is just plain the incorrect error.
Since whitebox macro expansion is forced here, when the typer is an implicit search all exceptions thrown by the macro implementation are caught and swallowed as "implicit not found", including fatal errors like out of memory or stack overflow.
This doesn't happen with blackbox macros, because they are delayed and expanded after the implicit search.
Example:
This can cause spurious missing implicit errors (see. milessabin/shapeless#776).
The text was updated successfully, but these errors were encountered: