Skip to content

Commit 09b45d3

Browse files
Merge pull request #7268 from dotty-staging/do-not-hardcode-primitive-liftables
Cleanup reification of primitive liftables
2 parents ad83120 + 186feba commit 09b45d3

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,23 @@ class Definitions {
630630

631631
@tu lazy val QuotedExprClass: ClassSymbol = ctx.requiredClass("scala.quoted.Expr")
632632
@tu lazy val QuotedExprModule: Symbol = QuotedExprClass.companionModule
633+
@tu lazy val QuotedExprModule_nullExpr: Symbol = QuotedExprModule.requiredMethod(nme.nullExpr)
634+
@tu lazy val QuotedExprModule_unitExpr: Symbol = QuotedExprModule.requiredMethod(nme.unitExpr)
633635

634636
@tu lazy val QuoteContextClass: ClassSymbol = ctx.requiredClass("scala.quoted.QuoteContext")
635637
@tu lazy val QuoteContextModule: Symbol = QuoteContextClass.companionModule
636638
@tu lazy val QuoteContext_macroContext: Symbol = QuoteContextModule.requiredMethod("macroContext")
637639

638640
@tu lazy val LiftableModule: Symbol = ctx.requiredModule("scala.quoted.Liftable")
641+
@tu lazy val LiftableModule_BooleanIsLiftable: Symbol = LiftableModule.requiredMethod("BooleanIsLiftable")
642+
@tu lazy val LiftableModule_ByteIsLiftable: Symbol = LiftableModule.requiredMethod("ByteIsLiftable")
643+
@tu lazy val LiftableModule_ShortIsLiftable: Symbol = LiftableModule.requiredMethod("ShortIsLiftable")
644+
@tu lazy val LiftableModule_IntIsLiftable: Symbol = LiftableModule.requiredMethod("IntIsLiftable")
645+
@tu lazy val LiftableModule_LongIsLiftable: Symbol = LiftableModule.requiredMethod("LongIsLiftable")
646+
@tu lazy val LiftableModule_FloatIsLiftable: Symbol = LiftableModule.requiredMethod("FloatIsLiftable")
647+
@tu lazy val LiftableModule_DoubleIsLiftable: Symbol = LiftableModule.requiredMethod("DoubleIsLiftable")
648+
@tu lazy val LiftableModule_CharIsLiftable: Symbol = LiftableModule.requiredMethod("CharIsLiftable")
649+
@tu lazy val LiftableModule_StringIsLiftable: Symbol = LiftableModule.requiredMethod("StringIsLiftable")
639650

640651
@tu lazy val InternalQuotedModule: Symbol = ctx.requiredModule("scala.internal.Quoted")
641652
@tu lazy val InternalQuoted_exprQuote : Symbol = InternalQuotedModule.requiredMethod("exprQuote")

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ object StdNames {
503503
val notifyAll_ : N = "notifyAll"
504504
val notify_ : N = "notify"
505505
val null_ : N = "null"
506+
val nullExpr: N = "nullExpr"
506507
val ofDim: N = "ofDim"
507508
val opaque: N = "opaque"
508509
val ordinal: N = "ordinal"
@@ -556,6 +557,7 @@ object StdNames {
556557
val thisPrefix : N = "thisPrefix"
557558
val throw_ : N = "throw"
558559
val toArray: N = "toArray"
560+
val toExpr: N = "toExpr"
559561
val toList: N = "toList"
560562
val toObjectArray : N = "toObjectArray"
561563
val toSeq: N = "toSeq"
@@ -569,6 +571,7 @@ object StdNames {
569571
val unapply: N = "unapply"
570572
val unapplySeq: N = "unapplySeq"
571573
val unbox: N = "unbox"
574+
val unitExpr: N = "unitExpr"
572575
val universe: N = "universe"
573576
val update: N = "update"
574577
val updateDynamic: N = "updateDynamic"

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,23 @@ class ReifyQuotes extends MacroTransform {
206206
qctx
207207
}
208208

209-
def liftedValue[T](value: T, name: TermName) =
210-
ref(defn.LiftableModule)
211-
.select(name).appliedToType(originalTp)
212-
.select("toExpr".toTermName).appliedTo(Literal(Constant(value)))
213-
214-
def pickleAsValue[T](value: T) =
215-
value match {
216-
case null => ref(defn.QuotedExprModule).select("nullExpr".toTermName)
217-
case _: Unit => ref(defn.QuotedExprModule).select("unitExpr".toTermName)
218-
case _: Boolean => liftedValue(value, "Liftable_Boolean_delegate".toTermName)
219-
case _: Byte => liftedValue(value, "Liftable_Byte_delegate".toTermName)
220-
case _: Short => liftedValue(value, "Liftable_Short_delegate".toTermName)
221-
case _: Int => liftedValue(value, "Liftable_Int_delegate".toTermName)
222-
case _: Long => liftedValue(value, "Liftable_Long_delegate".toTermName)
223-
case _: Float => liftedValue(value, "Liftable_Float_delegate".toTermName)
224-
case _: Double => liftedValue(value, "Liftable_Double_delegate".toTermName)
225-
case _: Char => liftedValue(value, "Liftable_Char_delegate".toTermName)
226-
case _: String => liftedValue(value, "Liftable_String_delegate".toTermName)
209+
def pickleAsLiteral(lit: Literal) = {
210+
def liftedValue(lifter: Symbol) =
211+
ref(lifter).appliedToType(originalTp).select(nme.toExpr).appliedTo(lit)
212+
lit.const.tag match {
213+
case Constants.NullTag => ref(defn.QuotedExprModule_nullExpr)
214+
case Constants.UnitTag => ref(defn.QuotedExprModule_unitExpr)
215+
case Constants.BooleanTag => liftedValue(defn.LiftableModule_BooleanIsLiftable)
216+
case Constants.ByteTag => liftedValue(defn.LiftableModule_ByteIsLiftable)
217+
case Constants.ShortTag => liftedValue(defn.LiftableModule_ShortIsLiftable)
218+
case Constants.IntTag => liftedValue(defn.LiftableModule_IntIsLiftable)
219+
case Constants.LongTag => liftedValue(defn.LiftableModule_LongIsLiftable)
220+
case Constants.FloatTag => liftedValue(defn.LiftableModule_FloatIsLiftable)
221+
case Constants.DoubleTag => liftedValue(defn.LiftableModule_DoubleIsLiftable)
222+
case Constants.CharTag => liftedValue(defn.LiftableModule_CharIsLiftable)
223+
case Constants.StringTag => liftedValue(defn.LiftableModule_StringIsLiftable)
227224
}
225+
}
228226

229227
def pickleAsTasty() = {
230228
val meth =
@@ -243,8 +241,8 @@ class ReifyQuotes extends MacroTransform {
243241
if (splices.isEmpty && body.symbol.isPrimitiveValueClass) tag(s"${body.symbol.name}Tag")
244242
else pickleAsTasty().select(nme.apply).appliedTo(qctx)
245243
}
246-
else toValue(body) match {
247-
case Some(value) => pickleAsValue(value)
244+
else getLiteral(body) match {
245+
case Some(lit) => pickleAsLiteral(lit)
248246
case _ => pickleAsTasty()
249247
}
250248
}
@@ -429,10 +427,10 @@ object ReifyQuotes {
429427

430428
val name: String = "reifyQuotes"
431429

432-
def toValue(tree: tpd.Tree): Option[Any] = tree match {
433-
case Literal(Constant(c)) => Some(c)
434-
case Block(Nil, e) => toValue(e)
435-
case Inlined(_, Nil, e) => toValue(e)
430+
def getLiteral(tree: tpd.Tree): Option[Literal] = tree match {
431+
case tree: Literal => Some(tree)
432+
case Block(Nil, e) => getLiteral(e)
433+
case Inlined(_, Nil, e) => getLiteral(e)
436434
case _ => None
437435
}
438436

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ trait Liftable[T] {
1919
*/
2020
object Liftable {
2121

22-
given Liftable_Boolean_delegate[T <: Boolean] : Liftable[T] = new PrimitiveLiftable
23-
given Liftable_Byte_delegate[T <: Byte] : Liftable[T] = new PrimitiveLiftable
24-
given Liftable_Short_delegate[T <: Short] : Liftable[T] = new PrimitiveLiftable
25-
given Liftable_Int_delegate[T <: Int] : Liftable[T] = new PrimitiveLiftable
26-
given Liftable_Long_delegate[T <: Long] : Liftable[T] = new PrimitiveLiftable
27-
given Liftable_Float_delegate[T <: Float] : Liftable[T] = new PrimitiveLiftable
28-
given Liftable_Double_delegate[T <: Double] : Liftable[T] = new PrimitiveLiftable
29-
given Liftable_Char_delegate[T <: Char] : Liftable[T] = new PrimitiveLiftable
30-
given Liftable_String_delegate[T <: String] : Liftable[T] = new PrimitiveLiftable
22+
given BooleanIsLiftable[T <: Boolean] : Liftable[T] = new PrimitiveLiftable
23+
given ByteIsLiftable[T <: Byte] : Liftable[T] = new PrimitiveLiftable
24+
given ShortIsLiftable[T <: Short] : Liftable[T] = new PrimitiveLiftable
25+
given IntIsLiftable[T <: Int] : Liftable[T] = new PrimitiveLiftable
26+
given LongIsLiftable[T <: Long] : Liftable[T] = new PrimitiveLiftable
27+
given FloatIsLiftable[T <: Float] : Liftable[T] = new PrimitiveLiftable
28+
given DoubleIsLiftable[T <: Double] : Liftable[T] = new PrimitiveLiftable
29+
given CharIsLiftable[T <: Char] : Liftable[T] = new PrimitiveLiftable
30+
given StringIsLiftable[T <: String] : Liftable[T] = new PrimitiveLiftable
3131

3232
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
3333
/** Lift a primitive value `n` into `'{ n }` */

0 commit comments

Comments
 (0)