Skip to content

Commit 616308d

Browse files
Merge pull request #14808 from dotty-staging/add-regression-tests
Add regression tests
2 parents 011df7d + 2b597e8 commit 616308d

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed

tests/run-macros/simple-switch.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
word
3+
bye
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted.*
2+
3+
inline def switch[T](n: Int)(inline xs: T*): T = ${ switchExpr('n, 'xs) }
4+
5+
private def switchExpr[T: Type](n: Expr[Int], casesExpr: Expr[Seq[T]])(using Quotes): Expr[T] =
6+
casesExpr match
7+
case Varargs(caseExprs) => switchExpr(n, caseExprs)
8+
case _ => quotes.reflect.report.errorAndAbort("switch does not support varargs unpacking")
9+
10+
private def switchExpr[T: Type](n: Expr[Int], caseExprs: Seq[Expr[T]])(using Quotes): Expr[T] =
11+
import quotes.reflect.*
12+
val cases =
13+
for (caseExpr, i) <- caseExprs.zipWithIndex
14+
yield CaseDef(Literal(IntConstant(i)), None, caseExpr.asTerm)
15+
Match(n.asTerm, cases.toList).asExprOf[T]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@main def Test: Unit =
2+
println(f(0))
3+
println(f(1))
4+
println(f(2))
5+
6+
def f(i: Int): String =
7+
switch(i)(
8+
"hello",
9+
"word",
10+
"bye"
11+
)

tests/run-macros/switch-match.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(4,ArraySeq(a, b))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted.*
2+
3+
inline def unswitch[T](inline x: T): (Int, Seq[T]) = ${ unswitchExpr('x) }
4+
5+
private def unswitchExpr[T: Type](x: Expr[T])(using Quotes): Expr[(Int, Seq[T])] =
6+
import quotes.reflect.*
7+
x.asTerm match
8+
case Inlined(_, _, Block(Nil, Match(scrut, cases))) =>
9+
val exprs: List[Expr[T]] = cases.zipWithIndex.map {
10+
case (CaseDef(Literal(IntConstant(i)), None, body), j) if i == j => body.asExprOf[T]
11+
case (cse, _) => report.errorAndAbort("unexpected case: ", cse.pos)
12+
}
13+
scrut.asExpr match
14+
case '{ $scrutExpr: Int } => '{ ($scrutExpr, ${Expr.ofSeq(exprs)}) }
15+
case _ => report.errorAndAbort("not Int scrutinee", scrut.pos)
16+
case _ => report.errorAndAbort("not a match", x)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@main def Test: Unit =
2+
println(f(4))
3+
4+
def f(x: Int): (Int, Seq[String]) =
5+
unswitch {
6+
x match
7+
case 0 => "a"
8+
case 1 => "b"
9+
}

0 commit comments

Comments
 (0)