Skip to content

Quoted liftable derivation macro regression test #11647

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
74 changes: 74 additions & 0 deletions tests/run-macros/quoted-ToExpr-derivation-macro/Derivation_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import scala.compiletime.{erasedValue, summonFrom}
import scala.deriving._
import scala.quoted._

object ToExprMaker {

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

private def derivedExpr[T](mirrorExpr: Expr[Mirror.Of[T]])(using Quotes, Type[T]): Expr[ToExpr[T]] = {
val tpe = summonExprOrError[Type[T]]
mirrorExpr match {
case '{ $mirrorExpr : Mirror.Sum { type MirroredElemTypes = mirroredElemTypes } } =>
val liftables = elemTypesToExprs[mirroredElemTypes]
val liftablesFn = '{ (x: Int) => ${ switchExpr('x, liftables) } }
'{ new SumToExpr[T, mirroredElemTypes]($mirrorExpr, $liftablesFn)(using $tpe) }
case '{ $mirrorExpr : Mirror.Product { type MirroredElemTypes = mirroredElemTypes } } =>
val liftableExprs = Expr.ofSeq(elemTypesToExprs[mirroredElemTypes])
'{ new ProductToExpr[T, mirroredElemTypes]($mirrorExpr, $liftableExprs)(using $tpe) }
}
}

// TODO hide from users
class SumToExpr[T, MElemTypes](
mirror: Mirror.Sum { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Int => ToExpr[_]
)(using Type[T]) extends ToExpr[T]:
def apply(x: T)(using Quotes): Expr[T] =
val ordinal = mirror.ordinal(x)
val liftable = liftables.apply(ordinal).asInstanceOf[ToExpr[T]]
liftable.apply(x)
end SumToExpr

// TODO hide from users
class ProductToExpr[T, MElemTypes](
mirror: Mirror.Product { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Seq[ToExpr[_]]
)(using Type[T]) extends ToExpr[T]:
def apply(x: T)(using Quotes): Expr[T] =
val mirrorExpr = summonExprOrError[Mirror.ProductOf[T]]
val xProduct = x.asInstanceOf[Product]
val anyToExprs = liftables.asInstanceOf[Seq[ToExpr[Any]]]
val elemExprs =
xProduct.productIterator.zip(anyToExprs.iterator).map {
(elem, lift) => lift(elem)
}.toSeq
val elemsTupleExpr = Expr.ofTupleFromSeq(elemExprs)
'{ $mirrorExpr.fromProduct($elemsTupleExpr) }
end ProductToExpr

private def elemTypesToExprs[X: Type](using Quotes): List[Expr[ToExpr[_]]] =
Type.of[X] match
case '[ head *: tail ] => summonExprOrError[ToExpr[head]] :: elemTypesToExprs[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)

private def summonExprOrError[T: Type](using Quotes): Expr[T] =
Expr.summon[T] match
case Some(expr) => expr
case None =>
quotes.reflect.report.throwError(s"Could not find implicit ${Type.show[T]}")

private def switchExpr(scrutinee: Expr[Int], seq: List[Expr[ToExpr[_]]])(using Quotes): Expr[ToExpr[_]] =
import quotes.reflect._
val cases = seq.zipWithIndex.map {
(expr, i) => CaseDef(Literal(IntConstant(i)), None, expr.asTerm)
}
Match(scrutinee.asTerm, cases).asExprOf[ToExpr[_]]

}
35 changes: 35 additions & 0 deletions tests/run-macros/quoted-ToExpr-derivation-macro/LibA_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import scala.quoted._

object LibA {

// Requires explicit inline given definition
// Advantage: simple definition of the extension (slightly smaller than quoted-ToExpr-derivation-macro-2)
// Drawback: the derived code will be duplicated at each use site

sealed trait Opt[+T]
object Opt:
inline given [T]: ToExpr[Opt[T]] = ToExprMaker.derived

case class Sm[T](t: T) extends Opt[T]
object Sm:
inline given [T]: ToExpr[Sm[T]] = ToExprMaker.derived

case object Nn extends Opt[Nothing]:
inline given ToExpr[Nn.type] = ToExprMaker.derived

import Opt.*

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

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

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

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

35 changes: 35 additions & 0 deletions tests/run-macros/quoted-ToExpr-derivation-macro/LibB_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import scala.quoted._

object LibB {

// Requires explicit given definition
// Advantage: No duplication of code
// Drawback: Need to explicitly add the Quotes, Type and ToExpr parameters (not too bad)

sealed trait Opt[+T]
object Opt:
given [T: Type: ToExpr](using Quotes): ToExpr[Opt[T]] = ToExprMaker.derived

case class Sm[T](t: T) extends Opt[T]
object Sm:
given [T: Type: ToExpr](using Quotes): ToExpr[Sm[T]] = ToExprMaker.derived

case object Nn extends Opt[Nothing]:
given (using Quotes): ToExpr[Nn.type] = ToExprMaker.derived

import Opt.*

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

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

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

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

32 changes: 32 additions & 0 deletions tests/run-macros/quoted-ToExpr-derivation-macro/LibC_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import scala.quoted._

object LibC {

// Requires explicit given definition
// Advantage: No duplication of code
// Drawback: Need to explicitly add the Quotes, Type and ToExpr parameters (not too bad)

enum Opt[+T]:
case Sm[+T](x: T) extends Opt[T]
case Nn extends Opt[Nothing]
object Opt:
given [T: Type: ToExpr](using Quotes): ToExpr[Opt[T]] = ToExprMaker.derived
given [T: Type: ToExpr](using Quotes): ToExpr[Sm[T]] = ToExprMaker.derived
given (using Quotes): ToExpr[Nn.type] = ToExprMaker.derived

import Opt.*

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

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

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

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

24 changes: 24 additions & 0 deletions tests/run-macros/quoted-ToExpr-derivation-macro/Test_3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
object Test extends App {
{
import LibA._
assert(optTwo == Sm(2))
assert(smTwo == Sm(2))
assert(none == Nn)
}

{
import LibB._
assert(optTwo == Sm(2))
assert(smTwo == Sm(2))
assert(none == Nn)
}


{
import LibC._
import Opt._
assert(optTwo == Sm(2))
assert(smTwo == Sm(2))
assert(none == Nn)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import scala.compiletime.{erasedValue, summonFrom}
import scala.deriving._
import scala.quoted._

trait Lft[T]:
def toExpr(x: T)(using Quotes): Expr[T]

object Lft {
given Lft[Int] with
def toExpr(x: Int)(using 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 Quotes, Type[T]): Expr[Lft[T]] = {
val tpe = summonExprOrError[Type[T]]
mirrorExpr match {
case '{ $mirrorExpr : Mirror.Sum { type MirroredElemTypes = mirroredElemTypes } } =>
val liftables = elemTypesLfts[mirroredElemTypes]
val liftablesFn = '{ (x: Int) => ${ switchExpr('x, liftables) } }
'{ new LiftableSum[T, mirroredElemTypes]($mirrorExpr, $liftablesFn)(using $tpe) }
case '{ $mirrorExpr : Mirror.Product { type MirroredElemTypes = mirroredElemTypes } } =>
val liftableExprs = Expr.ofSeq(elemTypesLfts[mirroredElemTypes])
'{ new LiftableProduct[T, mirroredElemTypes]($mirrorExpr, $liftableExprs)(using $tpe) }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful usage of quoted pattern matches 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last thing is to be able to write enum Opt[+T] derives ToExpr.

}

class LiftableSum[T, MElemTypes](
mirror: Mirror.Sum { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Int => Lft[_]
)(using Type[T]) extends Lft[T]:
def toExpr(x: T)(using Quotes): Expr[T] =
val ordinal = mirror.ordinal(x)
val liftable = liftables.apply(ordinal).asInstanceOf[Lft[T]]
liftable.toExpr(x)
end LiftableSum

class LiftableProduct[T, MElemTypes](
mirror: Mirror.Product { type MirroredElemTypes = MElemTypes; type MirroredMonoType = T },
liftables: Seq[Lft[_]]
)(using Type[T]) extends Lft[T]:
def toExpr(x: T)(using Quotes): Expr[T] =
val mirrorExpr = summonExprOrError[Mirror.ProductOf[T]]
val xProduct = x.asInstanceOf[Product]
val anyLiftables = liftables.asInstanceOf[Seq[Lft[Any]]]
val elemExprs =
xProduct.productIterator.zip(anyLiftables.iterator).map {
(elem, lift) => lift.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 ] => summonExprOrError[Lft[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)

private def summonExprOrError[T: Type](using Quotes): Expr[T] =
Expr.summon[T] match
case Some(expr) => expr
case None =>
quotes.reflect.report.throwError(s"Could not find implicit ${Type.show[T]}")

private def switchExpr(scrutinee: Expr[Int], seq: List[Expr[Lft[_]]])(using Quotes): Expr[Lft[_]] =
import quotes.reflect._
val cases = seq.zipWithIndex.map {
(expr, i) => CaseDef(Literal(IntConstant(i)), None, expr.asTerm)
}
Match(scrutinee.asTerm, cases).asExprOf[Lft[_]]

}
31 changes: 31 additions & 0 deletions tests/run-macros/quoted-liftable-derivation-macro-2/Lib_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

sealed trait Opt[+T]
object Opt:
inline given [T]: Lft[Opt[T]] = Lft.derived

case class Sm[T](t: T) extends Opt[T]
object Sm:
inline given [T]: Lft[Sm[T]] = Lft.derived

case object Nn extends Opt[Nothing]:
inline given Lft[Nn.type] = Lft.derived

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)
}