From 2d6911676d27a483b8dcd006073fdc629410569c Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 30 Sep 2019 13:42:01 +1000 Subject: [PATCH] Enable -Xlog-implicits by default for whitebox macro exceptions ``` scala> import reflect.macros._, language.experimental.macros; class C1; implicit def c1: C1 = macro c1Impl; def c1Impl(c: whitebox.Context) = {import c.universe._; throw null} import reflect.macros._ import language.experimental.macros defined class C1 defined term macro c1: C1 c1Impl: (c: scala.reflect.macros.whitebox.Context)Nothing scala> implicitly[C1] :18: c1 is not a valid implicit value for C1 because: exception typechecking implicit candidate (a whitebox macro): exception during macro expansion: java.lang.NullPointerException at .c1Impl(:11) implicitly[C1] ^ :18: this. is not a valid implicit value for C1 because: exception typechecking implicit candidate (a whitebox macro): exception during macro expansion: java.lang.NullPointerException at .c1Impl(:11) implicitly[C1] ^ :18: error: could not find implicit value for parameter e: C1 implicitly[C1] ^ scala> :quit ``` --- .../scala/tools/nsc/typechecker/Implicits.scala | 15 ++++++++++++--- .../scala/tools/nsc/typechecker/Macros.scala | 5 ++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index c26da80b0db3..f38a40d6e3a1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -782,9 +782,18 @@ trait Implicits { } } - if (context.reporter.hasErrors) - fail("hasMatchingSymbol reported error: " + context.reporter.firstError.get.errMsg) - else if (itree3.isErroneous) + if (context.reporter.hasErrors) { + val msg = context.reporter.firstError.get.errMsg + val isTopLevelWhiteboxMacro = info.sym.isMacro && !isBlackbox(info.sym) && msg.contains("during macro expansion") && context.openImplicits.lengthCompare(1) == 0 + if (isTopLevelWhiteboxMacro) { + val saved = settings.XlogImplicits.value + if (info.sym.isMacro && !isBlackbox(info.sym) && msg.contains("during macro expansion") && context.openImplicits.lengthCompare(1) == 0) settings.XlogImplicits.value = true + try fail("exception typechecking implicit candidate (a whitebox macro): " + msg) + finally settings.XlogImplicits.value = saved + } else { + fail("error typechecking implicit candidate: " + msg) + } + } else if (itree3.isErroneous) fail("error typechecking implicit candidate") else if (isLocalToCallsite && !hasMatchingSymbol(itree2)) fail("candidate implicit %s is shadowed by %s".format( diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala index c72d6f570a49..1f05d4c3f26a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala @@ -868,7 +868,10 @@ trait Macros extends MacroRuntimes with Traces with Helpers { case ex: AbortMacroException => MacroGeneratedAbort(expandee, ex) case ex: ControlThrowable => throw ex case ex: TypeError => MacroGeneratedTypeError(expandee, ex) - case NonFatal(_) => MacroGeneratedException(expandee, realex) + case NonFatal(_) => + if (typer.context.isSearchingForImplicitParam) + typer.context.openImplicits + MacroGeneratedException(expandee, realex) case fatal => throw fatal } } finally {