diff --git a/tests/run-macros/simple-switch.check b/tests/run-macros/simple-switch.check new file mode 100644 index 000000000000..7b155cd26b6d --- /dev/null +++ b/tests/run-macros/simple-switch.check @@ -0,0 +1,3 @@ +hello +word +bye diff --git a/tests/run-macros/simple-switch/Macro_1.scala b/tests/run-macros/simple-switch/Macro_1.scala new file mode 100644 index 000000000000..5e93cafea579 --- /dev/null +++ b/tests/run-macros/simple-switch/Macro_1.scala @@ -0,0 +1,15 @@ +import scala.quoted.* + +inline def switch[T](n: Int)(inline xs: T*): T = ${ switchExpr('n, 'xs) } + +private def switchExpr[T: Type](n: Expr[Int], casesExpr: Expr[Seq[T]])(using Quotes): Expr[T] = + casesExpr match + case Varargs(caseExprs) => switchExpr(n, caseExprs) + case _ => quotes.reflect.report.errorAndAbort("switch does not support varargs unpacking") + +private def switchExpr[T: Type](n: Expr[Int], caseExprs: Seq[Expr[T]])(using Quotes): Expr[T] = + import quotes.reflect.* + val cases = + for (caseExpr, i) <- caseExprs.zipWithIndex + yield CaseDef(Literal(IntConstant(i)), None, caseExpr.asTerm) + Match(n.asTerm, cases.toList).asExprOf[T] diff --git a/tests/run-macros/simple-switch/Main_2.scala b/tests/run-macros/simple-switch/Main_2.scala new file mode 100644 index 000000000000..af2eb42cd3bc --- /dev/null +++ b/tests/run-macros/simple-switch/Main_2.scala @@ -0,0 +1,11 @@ +@main def Test: Unit = + println(f(0)) + println(f(1)) + println(f(2)) + +def f(i: Int): String = + switch(i)( + "hello", + "word", + "bye" + ) diff --git a/tests/run-macros/switch-match.check b/tests/run-macros/switch-match.check new file mode 100644 index 000000000000..d5d15a1d5862 --- /dev/null +++ b/tests/run-macros/switch-match.check @@ -0,0 +1 @@ +(4,ArraySeq(a, b)) diff --git a/tests/run-macros/switch-match/Macro_1.scala b/tests/run-macros/switch-match/Macro_1.scala new file mode 100644 index 000000000000..90031c3a8200 --- /dev/null +++ b/tests/run-macros/switch-match/Macro_1.scala @@ -0,0 +1,16 @@ +import scala.quoted.* + +inline def unswitch[T](inline x: T): (Int, Seq[T]) = ${ unswitchExpr('x) } + +private def unswitchExpr[T: Type](x: Expr[T])(using Quotes): Expr[(Int, Seq[T])] = + import quotes.reflect.* + x.asTerm match + case Inlined(_, _, Block(Nil, Match(scrut, cases))) => + val exprs: List[Expr[T]] = cases.zipWithIndex.map { + case (CaseDef(Literal(IntConstant(i)), None, body), j) if i == j => body.asExprOf[T] + case (cse, _) => report.errorAndAbort("unexpected case: ", cse.pos) + } + scrut.asExpr match + case '{ $scrutExpr: Int } => '{ ($scrutExpr, ${Expr.ofSeq(exprs)}) } + case _ => report.errorAndAbort("not Int scrutinee", scrut.pos) + case _ => report.errorAndAbort("not a match", x) diff --git a/tests/run-macros/switch-match/Main_2.scala b/tests/run-macros/switch-match/Main_2.scala new file mode 100644 index 000000000000..88bc079bcf19 --- /dev/null +++ b/tests/run-macros/switch-match/Main_2.scala @@ -0,0 +1,9 @@ +@main def Test: Unit = + println(f(4)) + +def f(x: Int): (Int, Seq[String]) = + unswitch { + x match + case 0 => "a" + case 1 => "b" + }