Skip to content

Fix #6976: Improve macro exception stack handling #6983

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 2 commits into from
Aug 5, 2019
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
19 changes: 11 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ object Splicer {

val name = getDirectName(fn.info.finalResultType, fn.name.asTermName)
val method = getMethod(clazz, name, paramsSig(fn))
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*))
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*), method)
}

private def interpretModuleAccess(fn: Symbol)(implicit env: Env): Object =
Expand Down Expand Up @@ -339,7 +339,7 @@ object Splicer {

private def extraMsg = ". The most common reason for that is that you apply macros in the compilation run that defines them"

private def stopIfRuntimeException[T](thunk: => T): T = {
private def stopIfRuntimeException[T](thunk: => T, method: Method): T = {
try thunk
catch {
case ex: RuntimeException =>
Expand All @@ -352,13 +352,16 @@ object Splicer {
throw new StopInterpretation(sw.toString, pos)
case ex: InvocationTargetException =>
val sw = new StringWriter()
sw.write("An exception occurred while executing macro expansion: ")
sw.write(ex.getTargetException.getMessage)
sw.write("\n")
for (stack <- ex.getTargetException.getStackTrace.iterator.takeWhile(_.getClassName != this.getClass.getName).drop(1)) {
sw.write(stack.toString)
sw.write("\n")
sw.write("Exception occurred while executing macro expansion.\n")
val targetException = ex.getTargetException
if (!ctx.settings.Ydebug.value) {
val end = targetException.getStackTrace.lastIndexWhere { x =>
x.getClassName == method.getDeclaringClass.getCanonicalName && x.getMethodName == method.getName
}
val shortStackTrace = targetException.getStackTrace.take(end + 1)
targetException.setStackTrace(shortStackTrace)
}
targetException.printStackTrace(new PrintWriter(sw))
sw.write("\n")
throw new StopInterpretation(sw.toString, pos)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/neg-macros/i6976.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

-- Error: tests/neg-macros/i6976/Test_2.scala:5:44 ---------------------------------------------------------------------
5 | def main(args: Array[String]): Unit = mcr { 2 } // error
| ^^^^^^^^^
| Exception occurred while executing macro expansion.
| scala.MatchError: Inlined(EmptyTree,List(),Literal(Constant(2))) (of class dotty.tools.dotc.ast.Trees$Inlined)
| at playground.macros$.mcrImpl(Macro_1.scala:12)
|
| This location is in code that was inlined at Test_2.scala:5
14 changes: 14 additions & 0 deletions tests/neg-macros/i6976/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package playground

import scala.quoted._, scala.quoted.matching._
import delegate scala.quoted._
import scala.tasty._

object macros {
inline def mcr(x: => Any) = ${mcrImpl('x)}

def mcrImpl(body: Expr[Any]) given (ctx: QuoteContext): Expr[Any] = {
import ctx.tasty._
body.unseal match { case Block(_, _) => '{2} }
}
}
6 changes: 6 additions & 0 deletions tests/neg-macros/i6976/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import playground.macros._

object Test {
def main(args: Array[String]): Unit = mcr { 2 } // error
}