Skip to content

Commit

Permalink
Add Expr.valueOrAbort and reflect.report.errorAndAbort
Browse files Browse the repository at this point in the history
Provides a homogeneous and unambiguous concept for stopping the expansion of a macro.

The advantages of this naming are

* Consistent name suffixes `xyzAbort`
* `report.e` will show auto-completion for all variants of `error` and `errorAndAbort`
* `Abort` cannot be confused with other kinds of error handling in this API
  • Loading branch information
nicolasstucki committed Apr 12, 2021
1 parent 85a03ee commit 2705092
Show file tree
Hide file tree
Showing 59 changed files with 168 additions and 138 deletions.
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ object Splicer {
catch {
case ex: CompilationUnit.SuspendException =>
throw ex
case ex: scala.quoted.runtime.StopMacroExpansion if ctx.reporter.hasErrors =>
// errors have been emitted
case ex: scala.quoted.runtime.AbortExpansion if ctx.reporter.hasErrors =>
// errors have been emitted
EmptyTree
case ex: StopInterpretation =>
report.error(ex.msg, ex.pos)
Expand Down Expand Up @@ -423,7 +423,7 @@ object Splicer {
throw new StopInterpretation(sw.toString, pos)
case ex: InvocationTargetException =>
ex.getTargetException match {
case ex: scala.quoted.runtime.StopMacroExpansion =>
case ex: scala.quoted.runtime.AbortExpansion =>
throw ex
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
if (ctx.settings.XprintSuspension.value)
Expand Down
23 changes: 17 additions & 6 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def matches(that: scala.quoted.Expr[Any]): Boolean =
treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty

def value(using fromExpr: FromExpr[T]): Option[T] =
fromExpr.unapply(self)(using QuotesImpl.this)

def valueOrAbort(using fromExpr: FromExpr[T]): T =
def reportError =
val tree = reflect.asTerm(self)
val code = reflect.Printer.TreeCode.show(tree)
val msg = s"Expected a known value. \n\nThe value of: $code\ncould not be extracted using $fromExpr"
reflect.report.throwError(msg, self)
fromExpr.unapply(self)(using QuotesImpl.this).getOrElse(reportError)

end extension

extension (self: scala.quoted.Expr[Any])
Expand Down Expand Up @@ -2750,17 +2761,17 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def error(msg: String, pos: Position): Unit =
dotc.report.error(msg, pos)

def throwError(msg: String): Nothing =
def errorAndAbort(msg: String): Nothing =
error(msg)
throw new scala.quoted.runtime.StopMacroExpansion
throw new scala.quoted.runtime.AbortExpansion

def throwError(msg: String, expr: Expr[Any]): Nothing =
def errorAndAbort(msg: String, expr: Expr[Any]): Nothing =
error(msg, expr)
throw new scala.quoted.runtime.StopMacroExpansion
throw new scala.quoted.runtime.AbortExpansion

def throwError(msg: String, pos: Position): Nothing =
def errorAndAbort(msg: String, pos: Position): Nothing =
error(msg, pos)
throw new scala.quoted.runtime.StopMacroExpansion
throw new scala.quoted.runtime.AbortExpansion

def warning(msg: String): Unit =
dotc.report.warning(msg, Position.ofMacroExpansion)
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ either a constant or is a parameter that will be a constant when instantiated. T
aspect is also important for macro expansion.

To get values out of expressions containing constants `Expr` provides the method
`value` (or `valueOrError`). This will convert the `Expr[T]` into a `Some[T]` (or `T`) when the
expression contains value. Otherwise it will retrun `None` (or emit an error).
`value` (or `valueOrAbort`). This will convert the `Expr[T]` into a `Some[T]` (or `T`) when the
expression contains value. Otherwise it will return `None` (or emit an error).
To avoid having incidental val bindings generated by the inlining of the `def`
it is recommended to use an inline parameter. To illustrate this, consider an
implementation of the `power` function that makes use of a statically known exponent:
Expand Down Expand Up @@ -628,7 +628,7 @@ transparent inline def defaultOf(inline str: String) =
${ defaultOfImpl('str) }

def defaultOfImpl(strExpr: Expr[String])(using Quotes): Expr[Any] =
strExpr.valueOrError match
strExpr.valueOrAbort match
case "int" => '{1}
case "string" => '{"a"}

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Expr {
* // value: T
* ```
*
* To directly get the value of an expression `expr: Expr[T]` consider using `expr.value`/`expr.valueOrError` instead.
* To directly get the value of an expression `expr: Expr[T]` consider using `expr.value`/`expr.valueOrAbort` instead.
* @syntax markdown
*/
def unapply[T](x: Expr[T])(using FromExpr[T])(using Quotes): Option[T] =
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Exprs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Exprs:
* ...
* }
* ```
* To directly get the value of all expressions in a sequence `exprs: Seq[Expr[T]]` consider using `exprs.map(_.value)`/`exprs.map(_.valueOrError)` instead.
* To directly get the value of all expressions in a sequence `exprs: Seq[Expr[T]]` consider using `exprs.map(_.value)`/`exprs.map(_.valueOrAbort)` instead.
*/
def unapply[T](exprs: Seq[Expr[T]])(using FromExpr[T])(using Quotes): Option[Seq[T]] =
val builder = Seq.newBuilder[T]
Expand Down
41 changes: 25 additions & 16 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* Returns `None` if the expression does not represent a value or possibly contains side effects.
* Otherwise returns the `Some` of the value.
*/
def value(using FromExpr[T]): Option[T] =
given Quotes = Quotes.this
summon[FromExpr[T]].unapply(self)
def value(using FromExpr[T]): Option[T]

/** Return the value of this expression.
*
* Emits an error and throws if the expression does not represent a value or possibly contains side effects.
* Otherwise returns the value.
*/
def valueOrError(using FromExpr[T]): T =
val fromExpr = summon[FromExpr[T]]
def reportError =
val msg = s"Expected a known value. \n\nThe value of: ${self.show}\ncould not be extracted using $fromExpr"
reflect.report.throwError(msg, self)
given Quotes = Quotes.this
fromExpr.unapply(self).getOrElse(reportError)
@deprecated("Use valueOrThrow", "3.0.0-RC3")
def valueOrError(using FromExpr[T]): T = valueOrAbort

/** Return the value of this expression.
*
* Emits an error and aborts if the expression does not represent a value or possibly contains side effects.
* Otherwise returns the value.
*/
def valueOrAbort(using FromExpr[T]): T

end extension

Expand Down Expand Up @@ -4112,14 +4112,23 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Report an error message at the given position */
def error(msg: String, pos: Position): Unit

/** Report an error at the position of the macro expansion and throw a StopMacroExpansion */
def throwError(msg: String): Nothing
/** Report an error at the position of the macro expansion and throw a AbortExpansion */
def errorAndAbort(msg: String): Nothing

/** Report an error at the position of `expr` */
def throwError(msg: String, expr: Expr[Any]): Nothing
/** Report an error at the position of `expr` and throw a AbortExpansion */
def errorAndAbort(msg: String, expr: Expr[Any]): Nothing

/** Report an error message at the given position and throw a AbortExpansion */
def errorAndAbort(msg: String, pos: Position): Nothing

/** Report an error at the position of the macro expansion and throw a AbortExpansion */
def throwError(msg: String): Nothing = errorAndAbort(msg)

/** Report an error at the position of `expr` and throw a AbortExpansion */
def throwError(msg: String, expr: Expr[Any]): Nothing = errorAndAbort(msg, expr)

/** Report an error message at the given position and throw a StopMacroExpansion */
def throwError(msg: String, pos: Position): Nothing
/** Report an error message at the given position and throw a AbortExpansion */
def throwError(msg: String, pos: Position): Nothing = errorAndAbort(msg, pos)

/** Report a warning at the position of the macro expansion */
def warning(msg: String): Unit
Expand Down
9 changes: 9 additions & 0 deletions library/src/scala/quoted/runtime/AbortExpansion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scala.quoted.runtime

/** Throwable used to stop the quote expansion after an error was reported. */
class AbortExpansion extends Throwable:

// Do not fill the stacktrace for performance.
// We know that the stacktrace will be ignored
// and only the reported error message will be used.
override def fillInStackTrace(): Throwable = this
1 change: 1 addition & 0 deletions library/src/scala/quoted/runtime/StopMacroExpansion.scala
Original file line number Diff line number Diff line change
@@ -1,4 +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
2 changes: 1 addition & 1 deletion tests/bench/power-macro/PowerMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object PowerMacro {
inline def power(inline n: Long, x: Double) = ${ powerCode('n, 'x) }

def powerCode(n: Expr[Long], x: Expr[Double])(using Quotes): Expr[Double] =
powerCode(n.valueOrError, x)
powerCode(n.valueOrAbort, x)

def powerCode(n: Long, x: Expr[Double])(using Quotes): Expr[Double] =
if (n == 0) '{1.0}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i9014/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.quoted.*
trait Bar
inline given Bar = ${ impl }
def impl(using Quotes): Expr[Bar] = quotes.reflect.report.throwError("Failed to expand!")
def impl(using Quotes): Expr[Bar] = quotes.reflect.report.errorAndAbort("Failed to expand!")
2 changes: 1 addition & 1 deletion tests/neg-macros/i9014b/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.quoted._
trait Bar
transparent inline given Bar = ${ impl }
def impl(using Quotes): Expr[Bar] = quotes.reflect.report.throwError("Failed to expand!")
def impl(using Quotes): Expr[Bar] = quotes.reflect.report.errorAndAbort("Failed to expand!")
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object E {

inline def eval[T](inline x: E[T]): T = ${ impl('x) }

def impl[T: Type](x: Expr[E[T]]) (using Quotes): Expr[T] = x.valueOrError.lift
def impl[T: Type](x: Expr[E[T]]) (using Quotes): Expr[T] = x.valueOrAbort.lift

implicit def ev1[T: Type]: FromExpr[E[T]] = new FromExpr {
def unapply(x: Expr[E[T]])(using Quotes) = x match {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/inline-option/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import scala.quoted.*

object Macro {
def impl(opt: Expr[Option[Int]]) (using Quotes): Expr[Int] = opt.valueOrError match {
def impl(opt: Expr[Option[Int]]) (using Quotes): Expr[Int] = opt.valueOrAbort match {
case Some(i) => Expr(i)
case None => '{-1}
}
Expand Down
44 changes: 22 additions & 22 deletions tests/neg-macros/inline-tuples-1/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
import scala.quoted.*

object Macros {
def tup1(tup: Expr[Tuple1[Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup2(tup: Expr[Tuple2[Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup3(tup: Expr[Tuple3[Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup4(tup: Expr[Tuple4[Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup5(tup: Expr[Tuple5[Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup6(tup: Expr[Tuple6[Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup7(tup: Expr[Tuple7[Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup8(tup: Expr[Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup9(tup: Expr[Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup10(tup: Expr[Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup11(tup: Expr[Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup12(tup: Expr[Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup13(tup: Expr[Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup14(tup: Expr[Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup15(tup: Expr[Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup16(tup: Expr[Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup17(tup: Expr[Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup18(tup: Expr[Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup19(tup: Expr[Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup20(tup: Expr[Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup21(tup: Expr[Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup22(tup: Expr[Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrError.productIterator.map(_.asInstanceOf[Int]).sum)
def tup1(tup: Expr[Tuple1[Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup2(tup: Expr[Tuple2[Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup3(tup: Expr[Tuple3[Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup4(tup: Expr[Tuple4[Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup5(tup: Expr[Tuple5[Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup6(tup: Expr[Tuple6[Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup7(tup: Expr[Tuple7[Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup8(tup: Expr[Tuple8[Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup9(tup: Expr[Tuple9[Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup10(tup: Expr[Tuple10[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup11(tup: Expr[Tuple11[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup12(tup: Expr[Tuple12[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup13(tup: Expr[Tuple13[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup14(tup: Expr[Tuple14[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup15(tup: Expr[Tuple15[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup16(tup: Expr[Tuple16[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup17(tup: Expr[Tuple17[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup18(tup: Expr[Tuple18[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup19(tup: Expr[Tuple19[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup20(tup: Expr[Tuple20[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup21(tup: Expr[Tuple21[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
def tup22(tup: Expr[Tuple22[Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int]]) (using Quotes): Expr[Int] = Expr(tup.valueOrAbort.productIterator.map(_.asInstanceOf[Int]).sum)
}
2 changes: 1 addition & 1 deletion tests/neg-macros/quote-error-2/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import quoted.*
object Macro_1 {
inline def foo(inline b: Boolean): Unit = ${ fooImpl('b) }
def fooImpl(b: Expr[Boolean])(using Quotes): Expr[Unit] =
'{println(${msg(b.valueOrError)})}
'{println(${msg(b.valueOrAbort)})}

def msg(b: Boolean)(using Quotes): Expr[String] =
if (b) '{"foo(true)"}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/quote-error/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import quoted.*
object Macro_1 {
inline def foo(inline b: Boolean): Unit = ${fooImpl('b)}
def fooImpl(b: Expr[Boolean])(using Quotes) : Expr[Unit] =
if (b.valueOrError) '{println("foo(true)")}
if (b.valueOrAbort) '{println("foo(true)")}
else { quotes.reflect.report.error("foo cannot be called with false"); '{ ??? } }
}
2 changes: 1 addition & 1 deletion tests/neg-macros/quote-exception/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import quoted.*
object Macro_1 {
inline def foo(inline b: Boolean): Unit = ${fooImpl('b)}
def fooImpl(b: Expr[Boolean]) (using Quotes): Expr[Unit] =
if (b.valueOrError) '{println("foo(true)")}
if (b.valueOrAbort) '{println("foo(true)")}
else ???
}
2 changes: 1 addition & 1 deletion tests/neg-macros/quote-whitebox/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted.*

object Macros {
transparent inline def defaultOf(inline str: String): Any = ${ defaultOfImpl('str) }
def defaultOfImpl(str: Expr[String]) (using Quotes): Expr[Any] = str.valueOrError match {
def defaultOfImpl(str: Expr[String]) (using Quotes): Expr[Any] = str.valueOrAbort match {
case "int" => '{1}
case "string" => '{"a"}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/reflect-inline/assert_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ object api {
${ stripImpl('x) }

private def stripImpl(x: Expr[String])(using Quotes): Expr[String] =
Expr(x.valueOrError.stripMargin)
Expr(x.valueOrAbort.stripMargin)

}
2 changes: 1 addition & 1 deletion tests/pos-macros/i11835/X.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ object X:
import quotes.reflect.*
println("="*120)
println(b.asTerm)
println(b.valueOrError)
println(b.valueOrAbort)
'{()}
Loading

0 comments on commit 2705092

Please sign in to comment.