Skip to content

Commit

Permalink
Improve error handling of missuses of AbortExpansion
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 13, 2021
1 parent 2705092 commit cd70cb2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ object Splicer {
catch {
case ex: CompilationUnit.SuspendException =>
throw ex
case ex: scala.quoted.runtime.AbortExpansion if ctx.reporter.hasErrors =>
case ex: scala.quoted.runtime.AbortExpansion =>
if !ctx.reporter.hasErrors then
report.error("An AbortExpansion was thrown without any errors reported while expanding a macro. This macro has a bug.", splicePos)
// errors have been emitted
EmptyTree
case ex: StopInterpretation =>
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* Emits an error and throws if the expression does not represent a value or possibly contains side effects.
* Otherwise returns the value.
*/
@deprecated("Use valueOrThrow", "3.0.0-RC3")
@deprecated("Use valueOrThrow", "3.1.0")
def valueOrError(using FromExpr[T]): T = valueOrAbort

/** Return the value of this expression.
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/quoted/runtime/StopMacroExpansion.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package scala.quoted.runtime

/** Throwable used to stop the expansion of a macro after an error was reported */
@deprecated("Use AbortExpansion", "3.0.0-RC3")
class StopMacroExpansion extends Throwable
@deprecated("Use AbortExpansion", "3.1.0")
class StopMacroExpansion extends AbortExpansion
6 changes: 6 additions & 0 deletions tests/neg-macros/ill-abort.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

-- Error: tests/neg-macros/ill-abort/quoted_2.scala:1:15 ---------------------------------------------------------------
1 |def test = fail() // error
| ^^^^^^
| An AbortExpansion was thrown without any errors reported while expanding a macro. This macro has a bug.
| This location contains code that was inlined from quoted_1.scala:3
7 changes: 7 additions & 0 deletions tests/neg-macros/ill-abort/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

inline def fail(): Unit = ${ impl }

private def impl(using Quotes) : Expr[Unit] =
// should never be done without reporting error before (see docs)
throw new scala.quoted.runtime.AbortExpansion
1 change: 1 addition & 0 deletions tests/neg-macros/ill-abort/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = fail() // error

0 comments on commit cd70cb2

Please sign in to comment.