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 May 11, 2021
1 parent 4410752 commit 9d2fd94
Show file tree
Hide file tree
Showing 59 changed files with 189 additions and 126 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ 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.StopMacroExpansion =>
if !ctx.reporter.hasErrors then
report.error("A StopMacroExpansion was thrown without any errors reported while expanding a macro. This macro has a bug.", splicePos)
// errors have been emitted
EmptyTree
case ex: StopInterpretation =>
report.error(ex.msg, ex.pos)
Expand Down
26 changes: 24 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ 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

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

override def valueOrError(using FromExpr[T]): T = self.valueOrAbort

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 @@ -2744,14 +2757,23 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
dotc.report.error(msg, pos)

def throwError(msg: String): Nothing =
errorAndAbort(msg)

def throwError(msg: String, expr: Expr[Any]): Nothing =
errorAndAbort(msg, expr)

def throwError(msg: String, pos: Position): Nothing =
errorAndAbort(msg, pos)

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ 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).
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
42 changes: 31 additions & 11 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ 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)
// TODO: deprecate in 3.1.0 and remove @experimental from valueOrAbort
// @deprecated("Use valueOrThrow", "3.1.0")
def valueOrError(using FromExpr[T]): T

/** 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.
*/
@experimental
def valueOrAbort(using FromExpr[T]): T

end extension

Expand Down Expand Up @@ -4133,12 +4135,30 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def error(msg: String, pos: Position): Unit

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

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

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

/** Report an error at the position of the macro expansion and throw a StopMacroExpansion */
// TODO: deprecate in 3.1.0 and remove @experimental from errorAndAbort
// @deprecated("Use errorAndAbort", "3.1.0")
def throwError(msg: String): Nothing

/** Report an error at the position of `expr` */
/** Report an error at the position of `expr` and throw a StopMacroExpansion */
// TODO: deprecate in 3.1.0 and remove @experimental from errorAndAbort
// @deprecated("Use errorAndAbort", "3.1.0")
def throwError(msg: String, expr: Expr[Any]): Nothing

/** Report an error message at the given position and throw a StopMacroExpansion */
// TODO: deprecate in 3.1.0 and remove @experimental from errorAndAbort
// @deprecated("Use errorAndAbort", "3.1.0")
def throwError(msg: String, pos: Position): Nothing

/** Report a warning at the position of the macro expansion */
Expand Down
9 changes: 7 additions & 2 deletions library/src/scala/quoted/runtime/StopMacroExpansion.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package scala.quoted.runtime

/** Throwable used to stop the expansion of a macro after an error was reported */
class StopMacroExpansion extends Throwable
/** Throwable used to abort the expansion of a macro after an error was reported */
class StopMacroExpansion 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
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!")
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
| ^^^^^^
| A StopMacroExpansion 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.StopMacroExpansion
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
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 @@ -7,5 +7,5 @@ object X:
private def _blah(b: Expr[Boolean])(using Quotes): Expr[Unit] =
import quotes.reflect.*
b.asTerm
b.valueOrError
b.valueOrAbort
'{()}
2 changes: 1 addition & 1 deletion tests/pos-macros/power-macro/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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
4 changes: 2 additions & 2 deletions tests/pos-macros/quote-nested-object/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ object Macro {
inline def plus(inline n: Int, m: Int): Int = ${ plus('n, 'm) }

def plus(n: Expr[Int], m: Expr[Int]) (using Quotes): Expr[Int] =
if (n.valueOrError == 0) m
if (n.valueOrAbort == 0) m
else '{ ${n} + $m }

object Implementation2 {

inline def plus(inline n: Int, m: Int): Int = ${ plus('n, 'm) }

def plus(n: Expr[Int], m: Expr[Int]) (using Quotes): Expr[Int] =
if (n.valueOrError == 0) m
if (n.valueOrAbort == 0) m
else '{ ${n} + $m }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/quote-whitebox-2/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Macro {
transparent inline def charOrString(inline str: String): Any = ${ impl('str) }

def impl(strExpr: Expr[String]) (using Quotes)=
val str = strExpr.valueOrError
val str = strExpr.valueOrAbort
if (str.length == 1) Expr(str.charAt(0)) else Expr(str)

}
Loading

0 comments on commit 9d2fd94

Please sign in to comment.