File tree 4 files changed +18
-4
lines changed
src-bootstrapped/scala/quoted
4 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -80,7 +80,16 @@ object Unliftable {
80
80
81
81
/** Lift a quoted primitive value `'{ x }` into `x` */
82
82
private class PrimitiveUnliftable [T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String ] extends Unliftable [T ] {
83
- def fromExpr (x : Expr [T ]) = Const .unapply(x)
83
+ def fromExpr (expr : Expr [T ]) =
84
+ import quotes .reflect ._
85
+ def rec (tree : Term ): Option [T ] = tree match {
86
+ case Literal (c) if c.value != null => Some (c.value.asInstanceOf [T ])
87
+ case Block (Nil , e) => rec(e)
88
+ case Typed (e, _) => rec(e)
89
+ case Inlined (_, Nil , e) => rec(e)
90
+ case _ => None
91
+ }
92
+ rec(Term .of(expr))
84
93
}
85
94
86
95
/** Default unliftable for Option
Original file line number Diff line number Diff line change @@ -21,7 +21,12 @@ object Const {
21
21
def unapply [T ](expr : Expr [T ])(using Quotes ): Option [T ] = {
22
22
import quotes .reflect ._
23
23
def rec (tree : Term ): Option [T ] = tree match {
24
- case Literal (c) => Some (c.value.asInstanceOf [T ])
24
+ case Literal (c) =>
25
+ c match
26
+ case Constant .Null () => None
27
+ case Constant .Unit () => None
28
+ case Constant .ClassOf (_) => None
29
+ case _ => Some (c.value.asInstanceOf [T ])
25
30
case Block (Nil , e) => rec(e)
26
31
case Typed (e, _) => rec(e)
27
32
case Inlined (_, Nil , e) => rec(e)
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ object Macros {
14
14
Expr (3 ) match { case Const (n) => stagedPrintln(n) }
15
15
' {4 } match { case Const (n) => stagedPrintln(n) }
16
16
' {" abc" } match { case Const (n) => stagedPrintln(n) }
17
- ' {null } match { case Const (n) => stagedPrintln(n ) }
17
+ ' {null } match { case ' { null } => stagedPrintln(null ) }
18
18
19
19
' {new Object } match { case Const (n) => println(n); case _ => stagedPrintln(" OK" ) }
20
20
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ Some(Some(3))
38
38
Some(None)
39
39
Some(Some(3))
40
40
Some(Some(3))
41
- Some( None)
41
+ None
42
42
43
43
Some(Left(1))
44
44
Some(Right(2))
You can’t perform that action at this time.
0 commit comments