Skip to content

Commit 4bfafbf

Browse files
Merge pull request #6935 from dotty-staging/add-quoted-ExprOps-toExprOfList
Add quoted ExprOps toExprOfSeq
2 parents 8095c23 + 8f85154 commit 4bfafbf

File tree

10 files changed

+90
-20
lines changed

10 files changed

+90
-20
lines changed

compiler/src/dotty/tools/dotc/transform/SeqLiterals.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ class SeqLiterals extends MiniPhase {
3131
val arr = JavaSeqLiteral(tree.elems, tree.elemtpt)
3232
//println(i"trans seq $tree, arr = $arr: ${arr.tpe} ${arr.tpe.elemType}")
3333
val elemtp = tree.elemtpt.tpe
34-
wrapArray(arr, elemtp)
34+
wrapArray(arr, elemtp).withSpan(tree.span)
3535
}
3636
}

library/src-bootstrapped/scala/quoted/package.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,34 @@ package object quoted {
5454
implicit object ExprOps {
5555
def (x: T) toExpr[T: Liftable] given QuoteContext: Expr[T] = the[Liftable[T]].toExpr(x)
5656

57-
def (list: List[Expr[T]]) toExprOfList[T: Type] given QuoteContext: Expr[List[T]] = list match {
58-
case x :: xs => '{ $x :: ${xs.toExprOfList} }
59-
case Nil => '{ Nil }
57+
/** Lifts this sequence of expressions into an expression of a sequence
58+
*
59+
* Transforms a list of expression
60+
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
61+
* to an expression equivalent to
62+
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
63+
*
64+
* Usage:
65+
* ```scala
66+
* '{ List(${List(1, 2, 3).toExprOfSeq}: _*) } // equvalent to '{ List(1, 2, 3) }
67+
* ```
68+
*/
69+
def (seq: Seq[Expr[T]]) toExprOfSeq[T] given (tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
70+
import qctx.tasty._
71+
Repeated(seq.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
6072
}
73+
74+
/** Lifts this list of expressions into an expression of a list
75+
*
76+
* Transforms a list of expression
77+
* `List(e1, e2, ...)` where `ei: Expr[T]`
78+
* to an expression equivalent to
79+
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
80+
*/
81+
def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] =
82+
if (list.isEmpty) '{ Nil } else '{ List(${list.toExprOfSeq}: _*) }
83+
84+
6185
}
6286

6387
}

tests/run-macros/i6765-b.check

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
{
2-
val x$3: java.lang.String = "One"
3-
scala.Nil.::[java.lang.String](x$3)
4-
}
5-
{
6-
val x$3: java.lang.String = "One"
7-
scala.Nil.::[java.lang.String](x$3)
8-
}
1+
scala.List.apply[java.lang.String]("One": _*)
2+
scala.List.apply[java.lang.String]("One": _*)

tests/run-macros/i6765-c.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
scala.Nil
2+
List()
3+
4+
scala.List.apply[java.lang.String]("#0": _*)
5+
List(#0)
6+
7+
scala.List.apply[java.lang.String]("#0", "#1": _*)
8+
List(#0, #1)
9+
10+
scala.List.apply[java.lang.String]("#0", "#1", "#2": _*)
11+
List(#0, #1, #2)
12+
13+
scala.List.apply[java.lang.String]("#0", "#1", "#2", "#3": _*)
14+
List(#0, #1, #2, #3)
15+
16+
scala.List.apply[java.lang.String]("#0", "#1", "#2", "#3", "#4": _*)
17+
List(#0, #1, #2, #3, #4)
18+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
import delegate scala.quoted._
3+
4+
inline def foo(inline n: Int) = ${fooImpl(n)}
5+
6+
def fooImpl(n: Int) given (qctx: QuoteContext) = {
7+
val res = List.tabulate(n)(i => ("#" + i).toExpr).toExprOfList
8+
'{ ${res.show.toExpr} + "\n" + $res.toString + "\n" }
9+
}

tests/run-macros/i6765-c/Test_2.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
println(foo(0))
4+
println(foo(1))
5+
println(foo(2))
6+
println(foo(3))
7+
println(foo(4))
8+
println(foo(5))
9+
}
10+
}

tests/run-macros/i6765.check

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
{
2-
val x$3: java.lang.String = "One"
3-
scala.Nil.::[java.lang.String](x$3)
4-
}
5-
{
6-
val x$3: java.lang.String = "One"
7-
scala.Nil.::[java.lang.String](x$3)
8-
}
1+
scala.List.apply[java.lang.String]("One": _*)
2+
scala.List.apply[java.lang.String]("One": _*)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
WrappedArray(1, 2, 3)
2+
WrappedArray(1, 2, 3)
3+
List(1, 2, 3)
4+
2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._
2+
import delegate scala.quoted._
3+
4+
inline def seq = ${fooImpl}
5+
6+
def fooImpl given (qctx: QuoteContext) = {
7+
List('{1}, '{2}, '{3}).toExprOfSeq
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
println(seq)
4+
val s: Seq[Int] = seq
5+
println(s)
6+
println(List(seq: _*))
7+
println(seq(1))
8+
}
9+
}

0 commit comments

Comments
 (0)