Skip to content
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
11 changes: 10 additions & 1 deletion library/src-bootstrapped/scala/quoted/Unliftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ object Unliftable {

/** Lift a quoted primitive value `'{ x }` into `x` */
private class PrimitiveUnliftable[T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
def fromExpr(x: Expr[T]) = Const.unapply(x)
def fromExpr(expr: Expr[T]) =
import quotes.reflect._
def rec(tree: Term): Option[T] = tree match {
case Literal(c) if c.value != null => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => rec(e)
case Typed(e, _) => rec(e)
case Inlined(_, Nil, e) => rec(e)
case _ => None
}
rec(Term.of(expr))
}

/** Default unliftable for Option
Expand Down
7 changes: 6 additions & 1 deletion library/src/scala/quoted/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ object Const {
def unapply[T](expr: Expr[T])(using Quotes): Option[T] = {
import quotes.reflect._
def rec(tree: Term): Option[T] = tree match {
case Literal(c) => Some(c.value.asInstanceOf[T])
case Literal(c) =>
c match
case Constant.Null() => None
case Constant.Unit() => None
case Constant.ClassOf(_) => None
case _ => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => rec(e)
case Typed(e, _) => rec(e)
case Inlined(_, Nil, e) => rec(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Macros {
Expr(3) match { case Const(n) => stagedPrintln(n) }
'{4} match { case Const(n) => stagedPrintln(n) }
'{"abc"} match { case Const(n) => stagedPrintln(n) }
'{null} match { case Const(n) => stagedPrintln(n) }
'{null} match { case '{null} => stagedPrintln(null) }

'{new Object} match { case Const(n) => println(n); case _ => stagedPrintln("OK") }

Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/unliftables.check
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Some(Some(3))
Some(None)
Some(Some(3))
Some(Some(3))
Some(None)
None

Some(Left(1))
Some(Right(2))
Expand Down