Skip to content

Commit 77415fe

Browse files
committed
Work directly on literals
1 parent 46ced9a commit 77415fe

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

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

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

209-
def liftedValue[T](const: Constant) = {
210-
val ltp = defn.LiftableClass.typeRef.appliedTo(ConstantType(const))
211-
val liftable = ctx.typer.inferImplicitArg(ltp, body.span)
212-
if (liftable.tpe.isInstanceOf[SearchFailureType])
213-
ctx.error(ctx.typer.missingArgMsg(liftable, ltp, "Could no optimize constant in quote"), ctx.source.atSpan(body.span))
214-
liftable.select("toExpr".toTermName).appliedTo(Literal(const))
215-
}
216-
217-
def pickleAsValue[T](value: T) =
218-
value match {
219-
case null => ref(defn.QuotedExprModule).select("nullExpr".toTermName)
220-
case _: Unit => ref(defn.QuotedExprModule).select("unitExpr".toTermName)
221-
case _ => liftedValue(Constant(value))
209+
def pickleAsLiteral(lit: Literal) =
210+
lit.const.tag match {
211+
case Constants.NullTag => ref(defn.QuotedExprModule).select("nullExpr".toTermName)
212+
case Constants.UnitTag => ref(defn.QuotedExprModule).select("unitExpr".toTermName)
213+
case _ => // Lifted literal
214+
val ltp = defn.LiftableClass.typeRef.appliedTo(ConstantType(lit.const))
215+
val liftable = ctx.typer.inferImplicitArg(ltp, body.span)
216+
if (liftable.tpe.isInstanceOf[SearchFailureType])
217+
ctx.error(ctx.typer.missingArgMsg(liftable, ltp, "Could no optimize constant in quote"), ctx.source.atSpan(body.span))
218+
liftable.select("toExpr".toTermName).appliedTo(lit)
222219
}
223220

224221
def pickleAsTasty() = {
@@ -238,8 +235,8 @@ class ReifyQuotes extends MacroTransform {
238235
if (splices.isEmpty && body.symbol.isPrimitiveValueClass) tag(s"${body.symbol.name}Tag")
239236
else pickleAsTasty().select(nme.apply).appliedTo(qctx)
240237
}
241-
else toValue(body) match {
242-
case Some(value) => pickleAsValue(value)
238+
else getLiteral(body) match {
239+
case Some(lit) => pickleAsLiteral(lit)
243240
case _ => pickleAsTasty()
244241
}
245242
}
@@ -424,10 +421,10 @@ object ReifyQuotes {
424421

425422
val name: String = "reifyQuotes"
426423

427-
def toValue(tree: tpd.Tree): Option[Any] = tree match {
428-
case Literal(Constant(c)) => Some(c)
429-
case Block(Nil, e) => toValue(e)
430-
case Inlined(_, Nil, e) => toValue(e)
424+
def getLiteral(tree: tpd.Tree): Option[Literal] = tree match {
425+
case tree: Literal => Some(tree)
426+
case Block(Nil, e) => getLiteral(e)
427+
case Inlined(_, Nil, e) => getLiteral(e)
431428
case _ => None
432429
}
433430

0 commit comments

Comments
 (0)