Skip to content

Add quoted ExprOps toExprOfSeq #6935

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
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/SeqLiterals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class SeqLiterals extends MiniPhase {
val arr = JavaSeqLiteral(tree.elems, tree.elemtpt)
//println(i"trans seq $tree, arr = $arr: ${arr.tpe} ${arr.tpe.elemType}")
val elemtp = tree.elemtpt.tpe
wrapArray(arr, elemtp)
wrapArray(arr, elemtp).withSpan(tree.span)
}
}
30 changes: 27 additions & 3 deletions library/src-bootstrapped/scala/quoted/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,34 @@ package object quoted {
implicit object ExprOps {
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)

def (list: List[Expr[T]]) toExprOfList[T: Type] given QuoteContext: Expr[List[T]] = list match {
case x :: xs => '{ $x :: ${xs.toExprOfList} }
case Nil => '{ Nil }
/** Lifts this sequence of expressions into an expression of a sequence
*
* Transforms a list of expression
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
* to an expression equivalent to
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
*
* Usage:
* ```scala
* '{ List(${List(1, 2, 3).toExprOfSeq}: _*) } // equvalent to '{ List(1, 2, 3) }
* ```
*/
def (seq: Seq[Expr[T]]) toExprOfSeq[T] given (tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
import qctx.tasty._
Repeated(seq.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
}

/** Lifts this list of expressions into an expression of a list
*
* Transforms a list of expression
* `List(e1, e2, ...)` where `ei: Expr[T]`
* to an expression equivalent to
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
*/
def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] =
if (list.isEmpty) '{ Nil } else '{ List(${list.toExprOfSeq}: _*) }


}

}
10 changes: 2 additions & 8 deletions tests/run-macros/i6765-b.check
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
{
val x$3: java.lang.String = "One"
scala.Nil.::[java.lang.String](x$3)
}
{
val x$3: java.lang.String = "One"
scala.Nil.::[java.lang.String](x$3)
}
scala.List.apply[java.lang.String]("One": _*)
scala.List.apply[java.lang.String]("One": _*)
18 changes: 18 additions & 0 deletions tests/run-macros/i6765-c.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
scala.Nil
List()

scala.List.apply[java.lang.String]("#0": _*)
List(#0)

scala.List.apply[java.lang.String]("#0", "#1": _*)
List(#0, #1)

scala.List.apply[java.lang.String]("#0", "#1", "#2": _*)
List(#0, #1, #2)

scala.List.apply[java.lang.String]("#0", "#1", "#2", "#3": _*)
List(#0, #1, #2, #3)

scala.List.apply[java.lang.String]("#0", "#1", "#2", "#3", "#4": _*)
List(#0, #1, #2, #3, #4)

9 changes: 9 additions & 0 deletions tests/run-macros/i6765-c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._
import delegate scala.quoted._

inline def foo(inline n: Int) = ${fooImpl(n)}

def fooImpl(n: Int) given (qctx: QuoteContext) = {
val res = List.tabulate(n)(i => ("#" + i).toExpr).toExprOfList
'{ ${res.show.toExpr} + "\n" + $res.toString + "\n" }
}
10 changes: 10 additions & 0 deletions tests/run-macros/i6765-c/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test {
def main(args: Array[String]): Unit = {
println(foo(0))
println(foo(1))
println(foo(2))
println(foo(3))
println(foo(4))
println(foo(5))
}
}
10 changes: 2 additions & 8 deletions tests/run-macros/i6765.check
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
{
val x$3: java.lang.String = "One"
scala.Nil.::[java.lang.String](x$3)
}
{
val x$3: java.lang.String = "One"
scala.Nil.::[java.lang.String](x$3)
}
scala.List.apply[java.lang.String]("One": _*)
scala.List.apply[java.lang.String]("One": _*)
4 changes: 4 additions & 0 deletions tests/run-macros/quote-toExprOfSeq.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
WrappedArray(1, 2, 3)
WrappedArray(1, 2, 3)
List(1, 2, 3)
2
8 changes: 8 additions & 0 deletions tests/run-macros/quote-toExprOfSeq/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._
import delegate scala.quoted._

inline def seq = ${fooImpl}

def fooImpl given (qctx: QuoteContext) = {
List('{1}, '{2}, '{3}).toExprOfSeq
}
9 changes: 9 additions & 0 deletions tests/run-macros/quote-toExprOfSeq/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test {
def main(args: Array[String]): Unit = {
println(seq)
val s: Seq[Int] = seq
println(s)
println(List(seq: _*))
println(seq(1))
}
}