Skip to content

Commit bd6bc5d

Browse files
committed
Remove quoted.Const.unapply
This extractor was replaced with the more versatil extractor `quoted.Unlifted.unapply`. * Remove definition of `quoted.Const.unapply` * Replace uses of `Const` with `Unlifted` * Allow unlifting precise types for primitives * Remove `Unlifted[Unit]`
1 parent 96c8bca commit bd6bc5d

File tree

28 files changed

+73
-125
lines changed

28 files changed

+73
-125
lines changed

library/src-bootstrapped/scala/quoted/Unliftable.scala

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,46 @@ trait Unliftable[T] {
1414

1515
object Unliftable {
1616

17-
/** Default unliftable for Unit */
18-
given UnitUnliftable as Unliftable[Unit] = new PrimitiveUnliftable
19-
2017
/** Default unliftable for Boolean */
21-
given BooleanUnliftable as Unliftable[Boolean] = new PrimitiveUnliftable
18+
given BooleanUnliftable[T <: Boolean] as Unliftable[T] = new PrimitiveUnliftable
2219

2320
/** Default unliftable for Byte */
24-
given ByteUnliftable as Unliftable[Byte] = new PrimitiveUnliftable
21+
given ByteUnliftable[T <: Byte] as Unliftable[T] = new PrimitiveUnliftable
2522

2623
/** Default unliftable for Short */
27-
given ShortUnliftable as Unliftable[Short] = new PrimitiveUnliftable
24+
given ShortUnliftable[T <: Short] as Unliftable[T] = new PrimitiveUnliftable
2825

2926
/** Default unliftable for Int */
30-
given IntUnliftable as Unliftable[Int] = new PrimitiveUnliftable
27+
given IntUnliftable[T <: Int] as Unliftable[T] = new PrimitiveUnliftable
3128

3229
/** Default unliftable for Long */
33-
given LongUnliftable as Unliftable[Long] = new PrimitiveUnliftable
30+
given LongUnliftable[T <: Long] as Unliftable[T] = new PrimitiveUnliftable
3431

3532
/** Default unliftable for Float */
36-
given FloatUnliftable as Unliftable[Float] = new PrimitiveUnliftable
33+
given FloatUnliftable[T <: Float] as Unliftable[T] = new PrimitiveUnliftable
3734

3835
/** Default unliftable for Double */
39-
given DoubleUnliftable as Unliftable[Double] = new PrimitiveUnliftable
36+
given DoubleUnliftable[T <: Double] as Unliftable[T] = new PrimitiveUnliftable
4037

4138
/** Default unliftable for Char */
42-
given CharUnliftable as Unliftable[Char] = new PrimitiveUnliftable
39+
given CharUnliftable[T <: Char] as Unliftable[T] = new PrimitiveUnliftable
4340

4441
/** Default unliftable for String */
45-
given StringUnliftable as Unliftable[String] = new PrimitiveUnliftable
42+
given StringUnliftable[T <: String] as Unliftable[T] = new PrimitiveUnliftable
4643

47-
private class PrimitiveUnliftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
44+
private class PrimitiveUnliftable[T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] {
4845
/** Lift a quoted primitive value `'{ n }` into `n` */
49-
def fromExpr(x: Expr[T]) = Const.unapply(x)
46+
def fromExpr(expr: Expr[T]) = {
47+
import quotes.reflect._
48+
def rec(tree: Term): Option[T] = tree match {
49+
case Literal(c) => Some(c.value.asInstanceOf[T])
50+
case Block(Nil, e) => rec(e)
51+
case Typed(e, _) => rec(e)
52+
case Inlined(_, Nil, e) => rec(e)
53+
case _ => None
54+
}
55+
rec(Term.of(expr))
56+
}
5057
}
5158

5259
/** Default unliftable for Option */
@@ -79,8 +86,8 @@ object Unliftable {
7986
/** Default unliftable for StringContext */
8087
given StringContextUnliftable as Unliftable[StringContext] = new {
8188
def fromExpr(x: Expr[StringContext]) = x match {
82-
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
83-
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
89+
case '{ new StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
90+
case '{ StringContext(${Varargs(Unlifted(args))}: _*) } => Some(StringContext(args: _*))
8491
case _ => None
8592
}
8693
}

library/src/scala/quoted/Const.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

library/src/scala/quoted/Consts.scala

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/neg-macros/BigFloat/BigFloatFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted._
66
object BigFloatFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[BigFloat] =
88
digits match
9-
case Const(ds) =>
9+
case Unlifted(ds) =>
1010
try
1111
val BigFloat(m, e) = BigFloat(ds)
1212
'{BigFloat(${Expr(m)}, ${Expr(e)})}

tests/neg-macros/GenericNumLits/EvenFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Even._
55

66
object EvenFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[Even] = digits match {
8-
case Const(ds) =>
8+
case Unlifted(ds) =>
99
val ev =
1010
try evenFromDigits(ds)
1111
catch {

tests/neg-macros/i6432/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Unlifted(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

tests/neg-macros/i6432b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Unlifted(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

tests/neg-macros/inline-macro-staged-interpreter/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object E {
1010

1111
implicit def ev1[T: Type]: Unliftable[E[T]] = new Unliftable {
1212
def fromExpr(x: Expr[E[T]]) = x match {
13-
case '{ I(${Const(n)}) } => Some(I(n).asInstanceOf[E[T]])
13+
case '{ I(${Unlifted(n)}) } => Some(I(n).asInstanceOf[E[T]])
1414
case '{ Plus[T](${Value(x)}, ${Value(y)})(using $op) } if op.matches('{Plus2.IPlus}) => Some(Plus(x, y)(using Plus2.IPlus.asInstanceOf[Plus2[T]]).asInstanceOf[E[T]])
1515
case _ => None
1616
}

tests/run-macros/BigFloat/BigFloatFromDigitsImpl_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted._
66
object BigFloatFromDigitsImpl:
77
def apply(digits: Expr[String])(using Quotes): Expr[BigFloat] =
88
digits match
9-
case Const(ds) =>
9+
case Unlifted(ds) =>
1010
try
1111
val BigFloat(m, e) = BigFloat(ds)
1212
'{BigFloat(${Expr(m)}, ${Expr(e)})}

tests/run-macros/enum-nat-macro/Macros_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import Nat._
2525
case 0 => acc
2626
case n => inner[Succ[N]](n - 1, '{Succ($acc)})
2727

28-
val Const(i) = int
28+
val Unlifted(i) = int
2929
require(i >= 0)
3030
inner[Zero.type](i, '{Zero})

0 commit comments

Comments
 (0)