diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index 4100ff336ec4..f213fa0bbd98 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -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 = @@ -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 => @@ -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) } diff --git a/tests/neg-macros/i6976.check b/tests/neg-macros/i6976.check new file mode 100644 index 000000000000..64cf0ee8eef0 --- /dev/null +++ b/tests/neg-macros/i6976.check @@ -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 diff --git a/tests/neg-macros/i6976/Macro_1.scala b/tests/neg-macros/i6976/Macro_1.scala new file mode 100644 index 000000000000..2ebb07974225 --- /dev/null +++ b/tests/neg-macros/i6976/Macro_1.scala @@ -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} } + } +} diff --git a/tests/neg-macros/i6976/Test_2.scala b/tests/neg-macros/i6976/Test_2.scala new file mode 100644 index 000000000000..26247b86e419 --- /dev/null +++ b/tests/neg-macros/i6976/Test_2.scala @@ -0,0 +1,6 @@ + +import playground.macros._ + +object Test { + def main(args: Array[String]): Unit = mcr { 2 } // error +}