Skip to content

Add quoted.Type.valueOfTuple #12423

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
merged 1 commit into from
May 18, 2021
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
49 changes: 44 additions & 5 deletions library/src/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala.quoted

import scala.annotation.compileTimeOnly
import scala.annotation.{compileTimeOnly, experimental}

/** Type (or type constructor) `T` needed contextually when using `T` in a quoted expression `'{... T ...}` */
abstract class Type[T <: AnyKind] private[scala]:
Expand Down Expand Up @@ -35,11 +35,50 @@ object Type:
* @syntax markdown
*/
def valueOfConstant[T](using Type[T])(using Quotes): Option[T] =
ValueOf.unapply(quotes.reflect.TypeRepr.of[T]).asInstanceOf[Option[T]]

/** Extracts the value of a tuple of singleton constant types.
* Returns Some of the tuple type if it is a tuple singleton constant types.
* Returns None if the type is not a tuple singleton constant types.
*
* Example usage:
* ```scala
* ... match
* case '{ $mirrorExpr : Mirror.Sum { type MirroredElemLabels = label } } =>
* Type.valueOfTuple[label] // Option[Tuple]
* }
* ```
* @syntax markdown
*/
@experimental
def valueOfTuple[T <: Tuple](using Type[T])(using Quotes): Option[T] =
valueOfTuple(quotes.reflect.TypeRepr.of[T]).asInstanceOf[Option[T]]

private def valueOfTuple(using Quotes)(tpe: quotes.reflect.TypeRepr): Option[Tuple] =
import quotes.reflect.*
def valueOf(tpe: TypeRepr): Option[T] =
tpe.dealias.widenTermRefByName match
case ConstantType(const) => Some(const.value.asInstanceOf[T])
val cons = Symbol.classSymbol("scala.*:")
def rec(tpe: TypeRepr): Option[Tuple] =
tpe.widenTermRefByName.dealias match
case AppliedType(fn, tpes) if defn.isTupleClass(fn.typeSymbol) =>
tpes.foldRight(Option[Tuple](EmptyTuple)) {
case (_, None) => None
case (ValueOf(v), Some(acc)) => Some(v *: acc)
case _ => None
}
case AppliedType(tp, List(ValueOf(headValue), tail)) if tp.derivesFrom(cons) =>
rec(tail) match
case Some(tailValue) => Some(headValue *: tailValue)
case None => None
case tpe =>
if tpe.derivesFrom(Symbol.classSymbol("scala.EmptyTuple")) then Some(EmptyTuple)
else None
rec(tpe)

private object ValueOf:
def unapply(using Quotes)(tpe: quotes.reflect.TypeRepr): Option[Any] =
import quotes.reflect.*
tpe.widenTermRefByName.dealias match
case ConstantType(const) => Some(const.value)
case _ => None
valueOf(TypeRepr.of[T])

end Type
3 changes: 1 addition & 2 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ object MiMaFilters {
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeMember"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeMembers"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TermParamClauseMethods.isErased"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TermParamClauseMethods.isErased"),
exclude[MissingClassProblem]("scala.annotation.experimental"),
exclude[DirectMissingMethodProblem]("scala.quoted.Type.valueOfTuple"),
exclude[MissingClassProblem]("scala.annotation.experimental"),
exclude[MissingClassProblem]("scala.annotation.internal.ErasedParam"),
exclude[MissingClassProblem]("scala.annotation.internal.ErasedParam"),
Expand Down
13 changes: 13 additions & 0 deletions tests/run-macros/i12417/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.deriving.Mirror
import scala.compiletime.{constValue, error}
import scala.quoted.*

object TestMacro {
inline def test1[CASE_CLASS <: Product](using m: Mirror.ProductOf[CASE_CLASS]): String =
${ code('m) }

def code[CASE_CLASS <: Product: Type](m: Expr[Mirror.ProductOf[CASE_CLASS]])(using Quotes): Expr[String] =
m match
case '{ type t <: Tuple; $_ : Mirror { type MirroredElemLabels = `t` } } =>
Expr(Type.valueOfTuple[t].toString)
}
7 changes: 7 additions & 0 deletions tests/run-macros/i12417/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.deriving.Mirror
import scala.compiletime.{constValue, error}

object Test extends App {
case class A(x: String, y: Int)
assert(TestMacro.test1[A] == "Some((x,y))")
}
6 changes: 6 additions & 0 deletions tests/run-macros/i12417b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Some(())
Some((1))
Some((1,2))
Some((1,2,3))
Some((1,2,3,4))
Some((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26))
18 changes: 18 additions & 0 deletions tests/run-macros/i12417b/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import scala.deriving.Mirror
import scala.compiletime.{constValue, error}
import scala.quoted.*

object TestMacro {
inline def test1: Unit =
${ code() }

def code()(using Quotes): Expr[Unit] =
'{
println(${Expr(Type.valueOfTuple[EmptyTuple].toString)})
println(${Expr(Type.valueOfTuple[1 *: EmptyTuple].toString)})
println(${Expr(Type.valueOfTuple[(1, 2)].toString)})
println(${Expr(Type.valueOfTuple[(1, 2, 3)].toString)})
println(${Expr(Type.valueOfTuple[(1, 2, 3, 4)].toString)})
println(${Expr(Type.valueOfTuple[(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)].toString)})
}
}
1 change: 1 addition & 0 deletions tests/run-macros/i12417b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = TestMacro.test1