Skip to content

Add full tests suite for quoted.Liftable #9512

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 2 commits into from
Aug 7, 2020
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ class ReifyQuotes extends MacroTransform {
def pickleAsTasty() = {
val meth =
if (isType) ref(defn.Unpickler_unpickleType).appliedToType(originalTp)
else ref(defn.Unpickler_unpickleExpr).appliedToType(originalTp.widen.dealias)
else
val tpe =
if originalTp =:= defn.NilModule.termRef then originalTp // Workaround #4987
else originalTp.widen.dealias
ref(defn.Unpickler_unpickleExpr).appliedToType(tpe)
val pickledQuoteStrings = liftList(PickledQuotes.pickleQuote(body).map(x => Literal(Constant(x))), defn.StringType)
val splicesList = liftList(splices, defn.FunctionType(1).appliedTo(defn.SeqType.appliedTo(defn.AnyType), defn.AnyType))
meth.appliedTo(pickledQuoteStrings, splicesList)
Expand Down
2 changes: 1 addition & 1 deletion library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object Expr {
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
*/
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
if (xs.isEmpty) '{ Nil } else '{ List(${Varargs(xs)}: _*) }
if (xs.isEmpty) Expr(Nil) else '{ List(${Varargs(xs)}: _*) }

/** Lifts this sequence of expressions into an expression of a tuple
*
Expand Down
52 changes: 45 additions & 7 deletions library/src-bootstrapped/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ trait Liftable[T] {
*/
object Liftable {

// IMPORTANT Keep in sync with tests/run-staging/liftables.scala

given BooleanIsLiftable[T <: Boolean] as Liftable[T] = new PrimitiveLiftable
given ByteIsLiftable[T <: Byte] as Liftable[T] = new PrimitiveLiftable
given ShortIsLiftable[T <: Short] as Liftable[T] = new PrimitiveLiftable
Expand Down Expand Up @@ -103,7 +105,7 @@ object Liftable {
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
}

given iArrayIsLiftable[T: Type](using ltArray: Liftable[Array[T]]) as Liftable[IArray[T]] {
given IArrayIsLiftable[T: Type](using ltArray: Liftable[Array[T]]) as Liftable[IArray[T]] {
def toExpr(iarray: IArray[T]): QuoteContext ?=> Expr[IArray[T]] =
'{ ${ltArray.toExpr(iarray.asInstanceOf[Array[T]])}.asInstanceOf[IArray[T]] }
}
Expand All @@ -118,6 +120,11 @@ object Liftable {
Expr.ofList(xs.map(summon[Liftable[T]].toExpr))
}

given NilIsLiftable as Liftable[Nil.type] = new Liftable[Nil.type] {
def toExpr(xs: Nil.type): QuoteContext ?=> Expr[Nil.type] =
'{ Nil }
}

given [T: Type: Liftable] as Liftable[Set[T]] = new Liftable[Set[T]] {
def toExpr(set: Set[T]): QuoteContext ?=> Expr[Set[T]] =
'{ Set(${Expr(set.toSeq)}: _*) }
Expand All @@ -130,16 +137,40 @@ object Liftable {

given [T: Type: Liftable] as Liftable[Option[T]] = new Liftable[Option[T]] {
def toExpr(x: Option[T]): QuoteContext ?=> Expr[Option[T]] = x match {
case Some(x) => '{ Some[T](${Expr(x)}) }
case None => '{ None: Option[T] }
case x: Some[T] => Expr(x)
case None => Expr(None)
}
}

given [T: Type: Liftable] as Liftable[Some[T]] = new Liftable[Some[T]] {
def toExpr(x: Some[T]): QuoteContext ?=> Expr[Some[T]] =
'{ Some[T](${Expr(x.get)}) }
}

given Liftable[None.type] = new Liftable[None.type] {
def toExpr(x: None.type): QuoteContext ?=> Expr[None.type] =
'{ None }
}

given [L: Type: Liftable, R: Type: Liftable] as Liftable[Either[L, R]] = new Liftable[Either[L, R]] {
def toExpr(x: Either[L, R]): QuoteContext ?=> Expr[Either[L, R]] = x match {
case Left(x) => '{ Left[L, R](${Expr(x)}) }
case Right(x) => '{ Right[L, R](${Expr(x)}) }
}
def toExpr(x: Either[L, R]): QuoteContext ?=> Expr[Either[L, R]] = x match
case x: Left[L, R] => Expr(x)
case x: Right[L, R] => Expr(x)
}

given [L: Type: Liftable, R: Type] as Liftable[Left[L, R]] = new Liftable[Left[L, R]] {
def toExpr(x: Left[L, R]): QuoteContext ?=> Expr[Left[L, R]] =
'{ Left[L, R](${Expr(x.value)}) }
}

given [L: Type, R: Type: Liftable] as Liftable[Right[L, R]] = new Liftable[Right[L, R]] {
def toExpr(x: Right[L, R]): QuoteContext ?=> Expr[Right[L, R]] =
'{ Right[L, R](${Expr(x.value)}) }
}

given EmptyTupleIsLiftable as Liftable[EmptyTuple.type] = new {
def toExpr(tup: EmptyTuple.type) =
'{ EmptyTuple }
}

given [T1: Type: Liftable] as Liftable[Tuple1[T1]] = new {
Expand Down Expand Up @@ -305,4 +336,11 @@ object Liftable {
'{ BigDecimal(${Expr(x.toString)}) }
}

/** Lift a StringContext */
given Liftable[StringContext] = new Liftable[StringContext] {
def toExpr(stringContext: StringContext): QuoteContext ?=> Expr[StringContext] =
val parts = Varargs(stringContext.parts.map(Expr(_)))
'{ StringContext($parts: _*) }
}

}
7 changes: 7 additions & 0 deletions tests/pos-macros/nil-liftable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted._

class Test:
given NilIsLiftable as Liftable[Nil.type] = new Liftable[Nil.type] {
def toExpr(xs: Nil.type): QuoteContext ?=> Expr[Nil.type] =
'{ Nil }
}
1 change: 1 addition & 0 deletions tests/pos-macros/quote-liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test(using QuoteContext) = {
Expr(1.0f)
Expr(1.0)
Expr("abc")
Expr(StringContext("a", "b", "c"))

val xs: Expr[List[Int]] = Expr(1 :: 2 :: 3 :: Nil)
}
Loading