From cfc7347f18bdb98adacdd28092a39ffbc9ccf409 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 27 Oct 2021 10:29:30 +0200 Subject: [PATCH] Add type class power macro regression test --- tests/pos-macros/power-macro-2/Macro_1.scala | 22 +++++++++++++++++ tests/pos-macros/power-macro-2/Test_2.scala | 2 ++ tests/pos-macros/power-macro-3/Macro_1.scala | 25 ++++++++++++++++++++ tests/pos-macros/power-macro-3/Test_2.scala | 2 ++ 4 files changed, 51 insertions(+) create mode 100644 tests/pos-macros/power-macro-2/Macro_1.scala create mode 100644 tests/pos-macros/power-macro-2/Test_2.scala create mode 100644 tests/pos-macros/power-macro-3/Macro_1.scala create mode 100644 tests/pos-macros/power-macro-3/Test_2.scala diff --git a/tests/pos-macros/power-macro-2/Macro_1.scala b/tests/pos-macros/power-macro-2/Macro_1.scala new file mode 100644 index 000000000000..57f7764f0969 --- /dev/null +++ b/tests/pos-macros/power-macro-2/Macro_1.scala @@ -0,0 +1,22 @@ + +import scala.quoted.* + +import math.Numeric.Implicits.infixNumericOps + +inline def power[Num](x: Num, inline n: Int)(using num: Numeric[Num]) = ${powerCode('x, 'n)(using 'num)} + +private def powerCode[Num: Type](x: Expr[Num], n: Expr[Int])(using Expr[Numeric[Num]])(using Quotes): Expr[Num] = + powerCode(x, n.valueOrAbort) + +private def powerCode[Num: Type](x: Expr[Num], n: Int)(using num: Expr[Numeric[Num]])(using Quotes): Expr[Num] = + if (n == 0) '{ $num.one } + else if (n % 2 == 0) '{ + given Numeric[Num] = $num + val y = $x * $x + ${ powerCode('y, n / 2) } + } + else '{ + given Numeric[Num] = $num + $x * ${powerCode(x, n - 1)} + } + diff --git a/tests/pos-macros/power-macro-2/Test_2.scala b/tests/pos-macros/power-macro-2/Test_2.scala new file mode 100644 index 000000000000..59644c3d51fb --- /dev/null +++ b/tests/pos-macros/power-macro-2/Test_2.scala @@ -0,0 +1,2 @@ +def test(x: Int) = power(x, 5) +def test(x: Double) = power(x, 5) diff --git a/tests/pos-macros/power-macro-3/Macro_1.scala b/tests/pos-macros/power-macro-3/Macro_1.scala new file mode 100644 index 000000000000..d8273c65faf8 --- /dev/null +++ b/tests/pos-macros/power-macro-3/Macro_1.scala @@ -0,0 +1,25 @@ + +import scala.quoted.* + +import math.Numeric.Implicits.infixNumericOps + +inline def power[Num](x: Num, inline n: Int)(using num: Numeric[Num]) = ${powerCode('x, 'n)(using 'num)} + +private def powerCode[Num: Type](x: Expr[Num], n: Expr[Int])(using Expr[Numeric[Num]])(using Quotes): Expr[Num] = + powerCode(x, n.valueOrAbort) + +private def powerCode[Num: Type](x: Expr[Num], n: Int)(using num: Expr[Numeric[Num]])(using Quotes): Expr[Num] = + if (n == 0) '{ $num.one } + else if (n % 2 == 0) '{ + withGiven($num) { + val y = $x * $x + ${ powerCode('y, n / 2) } + } + } + else '{ + withGiven($num) { + $x * ${powerCode(x, n - 1)} + } + } + +inline def withGiven[U, T](inline x: T)(inline body: T ?=> U): U = body(using x) diff --git a/tests/pos-macros/power-macro-3/Test_2.scala b/tests/pos-macros/power-macro-3/Test_2.scala new file mode 100644 index 000000000000..59644c3d51fb --- /dev/null +++ b/tests/pos-macros/power-macro-3/Test_2.scala @@ -0,0 +1,2 @@ +def test(x: Int) = power(x, 5) +def test(x: Double) = power(x, 5)