Skip to content

Fix #9801: Make sure the errors are reported #9813

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 1 commit into from
Sep 17, 2020
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ object Splicer {
EmptyTree
case ex: StopInterpretation =>
report.error(ex.msg, ex.pos)
EmptyTree
ref(defn.Predef_undefined).withType(ErrorType(ex.msg))
case NonFatal(ex) =>
val msg =
s"""Failed to evaluate macro.
| Caused by ${ex.getClass}: ${if (ex.getMessage == null) "" else ex.getMessage}
| ${ex.getStackTrace.takeWhile(_.getClassName != "dotty.tools.dotc.transform.Splicer$").drop(1).mkString("\n ")}
""".stripMargin
report.error(msg, pos)
EmptyTree
ref(defn.Predef_undefined).withType(ErrorType(msg))
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/neg-macros/i9801/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.quoted._

def f() = ()

def triggerStackOverflow(n: Int): Expr[Double] = {
val r = triggerStackOverflow(n - 1)
f()
r
}

inline def loop(inline prog: Double): Double = ${impl('prog)}

def impl(prog: Expr[Double])(using QuoteContext) : Expr[Double] =
try {
triggerStackOverflow(0)
} catch {
case e =>
qctx.tasty.error(e.getMessage, prog.unseal.pos)
'{ 42.0 }
}
1 change: 1 addition & 0 deletions tests/neg-macros/i9801/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test: Unit = loop(4) // error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, this reports correctly the NPE

-- Error: tests/pos-macros/i9801/Test_2.scala:1:21 ---------------------------------------------------------------------
1 |def test: Unit = loop(4)
  |                 ^^^^^^^
  |       Exception occurred while executing macro expansion.
  |       java.lang.NullPointerException
  |             at dotty.tools.dotc.reporting.Message.dropNonSensical(Message.scala:65)
  |             at dotty.tools.dotc.reporting.Message.message(Message.scala:78)
  |             at dotty.tools.dotc.reporting.Message.isNonSensical(Message.scala:90)
  |             at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden(HideNonSensicalMessages.scala:16)
  |             at dotty.tools.dotc.reporting.HideNonSensicalMessages.isHidden$(HideNonSensicalMessages.scala:10)
  |             at dotty.tools.dotc.reporting.TestReporter.isHidden(TestReporter.scala:18)
  |             at dotty.tools.dotc.reporting.Reporter.report(Reporter.scala:148)
  |             at dotty.tools.dotc.report$.error(report.scala:71)
  |             at dotty.tools.dotc.quoted.reflect.ReflectionCompilerInterface.error(ReflectionCompilerInterface.scala:90)
  |             at dotty.tools.dotc.quoted.QuoteContextImpl$$anon$1.error(QuoteContextImpl.scala:42)
  |             at Macro_1$package$.impl(Macro_1.scala:18)
  |
  | This location contains code that was inlined from Test_2.scala:2

inlined at Test_2.scala:1:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that in this particular case the user passed a null into the reporting which made the reporting itself fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean e.getMessage returns null?

14 changes: 14 additions & 0 deletions tests/neg-macros/i9801b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.quoted._

def f() = ()

def triggerStackOverflow(n: Int): Expr[Double] = {
val r = triggerStackOverflow(n - 1)
f()
r
}

inline def loop(inline prog: Double): Double = ${impl('prog)}

def impl(prog: Expr[Double])(using QuoteContext) : Expr[Double] =
triggerStackOverflow(0)
1 change: 1 addition & 0 deletions tests/neg-macros/i9801b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test: Unit = loop(4) // error