Skip to content

Add quoted liftable macro derivation regression test #8343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import scala.compiletime.{erasedValue, summonFrom}
import scala.deriving._
import scala.quoted._

trait Lft[T]:
def toExpr(x: T)(using Type[T], Quotes): Expr[T] // TODO remove `Type[T]`

object Lft {
given Lft[Int] with
def toExpr(x: Int)(using Type[Int], Quotes) = Expr(x)

inline given derived[T](using inline m: Mirror.Of[T]): Lft[T] = ${ derivedExpr('m) }

private def derivedExpr[T](mirrorExpr: Expr[Mirror.Of[T]])(using qctx: Quotes, tpe: Type[T]): Expr[Lft[T]] = {
mirrorExpr match {
case '{ $mirrorExpr : Mirror.Sum { type MirroredElemTypes = mirroredElemTypes } } =>
val liftables = Expr.ofSeq(elemTypesLfts[mirroredElemTypes])
'{ new LiftableSum[T, mirroredElemTypes]($mirrorExpr, $liftables) }
case '{ $mirrorExpr : Mirror.Product { type MirroredElemTypes = mirroredElemTypes } } =>
val liftableExprs = Expr.ofSeq(elemTypesLfts[mirroredElemTypes])
'{ new LiftableProduct[T, mirroredElemTypes]($mirrorExpr, $liftableExprs) }
}
}

class LiftableSum[T, MElemTypes](
mirror: Mirror.Sum { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Seq[Lft[_]] // TODO make Lft creation lazy
) extends Lft[T]:
def toExpr(x: T)(using Type[T], Quotes): Expr[T] =
val ordinal = mirror.ordinal(x)
val tp = Expr.summon[Mirror.SumOf[T]].get match
case '{ $mirrorExpr : Mirror.Sum { type MirroredElemTypes = mirroredElemTypes } } =>
elemType[mirroredElemTypes](ordinal)
val liftable = liftables.apply(ordinal).asInstanceOf[Lft[T]]
liftable.toExpr(x)(using tp.asInstanceOf[Type[T]], summon[Quotes])
end LiftableSum

class LiftableProduct[T, MElemTypes](
mirror: Mirror.Product { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Seq[Lft[_]]
) extends Lft[T]:
def toExpr(x: T)(using Type[T], Quotes): Expr[T] =
val mirrorExpr = Expr.summon[Mirror.ProductOf[T]].get
val elemExprs =
x.asInstanceOf[Product].productIterator.zip(liftables.iterator).map { (elem, lift) =>
lift.asInstanceOf[Lft[Any]].toExpr(elem)
}.toSeq
val elemsTupleExpr = Expr.ofTupleFromSeq(elemExprs)
'{ $mirrorExpr.fromProduct($elemsTupleExpr) }
end LiftableProduct

private def elemTypesLfts[X: Type](using Quotes): List[Expr[Lft[_]]] =
Type.of[X] match
case '[ head *: tail ] =>
Expr.summon[Lft[head]].getOrElse(quotes.reflect.report.throwError(s"Could not find given Lft[${Type.show[head]}]")) :: elemTypesLfts[tail]
case '[ EmptyTuple ] => Nil

private def elemType[X: Type](ordinal: Int)(using Quotes): Type[_] =
Type.of[X] match
case '[ head *: tail ] =>
if ordinal == 0 then Type.of[head]
else elemType[tail](ordinal - 1)
}
24 changes: 24 additions & 0 deletions tests/run-macros/quoted-liftable-derivation-macro/Lib_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

sealed trait Opt[+T] derives Lft
case class Sm[T](t: T) extends Opt[T] derives Lft
case object Nn extends Opt[Nothing] derives Lft

object Lib {

import scala.quoted._
import Opt.*

inline def optTwo = ${optTwoExpr}
inline def smTwo = ${smTwoExpr}
inline def none = ${noneExpr}

private def optTwoExpr(using Quotes): Expr[Opt[Int]] =
summon[Lft[Opt[Int]]].toExpr(Sm(2))

private def smTwoExpr(using Quotes): Expr[Sm[Int]] =
summon[Lft[Sm[Int]]].toExpr(Sm(2))

private def noneExpr(using Quotes): Expr[Opt[Int]] =
summon[Lft[Nn.type]].toExpr(Nn)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test extends App {
import Opt._
assert(Lib.optTwo == Sm(2))
assert(Lib.smTwo == Sm(2))
assert(Lib.none == Nn)
}