-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
nicolasstucki
merged 2 commits into
scala:master
from
dotty-staging:add-quoted-liftable-derivation-macro-2
Mar 8, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
tests/run-macros/quoted-ToExpr-derivation-macro/Derivation_1.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
tests/run-macros/quoted-ToExpr-derivation-macro/LibA_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
tests/run-macros/quoted-ToExpr-derivation-macro/LibB_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
tests/run-macros/quoted-ToExpr-derivation-macro/LibC_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
tests/run-macros/quoted-ToExpr-derivation-macro/Test_3.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/run-macros/quoted-liftable-derivation-macro-2/Derivation_1.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} | ||
|
||
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
31
tests/run-macros/quoted-liftable-derivation-macro-2/Lib_2.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
tests/run-macros/quoted-liftable-derivation-macro-2/Test_3.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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
.