From 3311e22f42dea3a3c1d207e1271f8938663e73c8 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 27 Jul 2019 08:16:14 +0200 Subject: [PATCH 1/5] Add `quoted.Liftable[BigInt]` to the stdlib --- library/src/scala/quoted/Liftable.scala | 8 ++++++++ tests/run-with-compiler/quote-lift-BigInt.check | 1 + tests/run-with-compiler/quote-lift-BigInt.scala | 9 +++++++++ 3 files changed, 18 insertions(+) create mode 100644 tests/run-with-compiler/quote-lift-BigInt.check create mode 100644 tests/run-with-compiler/quote-lift-BigInt.scala diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index b6626d3643ce..ebbc8289a931 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -232,4 +232,12 @@ object Liftable { } } + given as Liftable[BigInt] = new Liftable[BigInt] { + def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = { + lazy val xInt: Int = x.intValue() + if (x.isValidInt && -1024 <= xInt && xInt < 1024) '{ BigInt(${xInt.toExpr}) } // Cached BigInt + else '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } // Compact string BigInt + } + } + } diff --git a/tests/run-with-compiler/quote-lift-BigInt.check b/tests/run-with-compiler/quote-lift-BigInt.check new file mode 100644 index 000000000000..f6c6c91c5603 --- /dev/null +++ b/tests/run-with-compiler/quote-lift-BigInt.check @@ -0,0 +1 @@ +(2,1005849025843905834908593485984390583429058574925) diff --git a/tests/run-with-compiler/quote-lift-BigInt.scala b/tests/run-with-compiler/quote-lift-BigInt.scala new file mode 100644 index 000000000000..bd28f360e4ed --- /dev/null +++ b/tests/run-with-compiler/quote-lift-BigInt.scala @@ -0,0 +1,9 @@ +import scala.quoted._ +object Test { + implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) + def main(args: Array[String]): Unit = println(run { + val a = BigInt(2).toExpr + val b = BigInt("1005849025843905834908593485984390583429058574925").toExpr + '{ ($a, $b) } + }) +} From 0b1fe9d219d7289faab37f59eb37a27f2570f7e3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 27 Jul 2019 08:27:45 +0200 Subject: [PATCH 2/5] Add `quoted.Liftable[BigInt]` to the stdlib --- library/src/scala/quoted/Liftable.scala | 9 +++++++++ tests/run-with-compiler/quote-lift-BigDecimal.check | 1 + tests/run-with-compiler/quote-lift-BigDecimal.scala | 9 +++++++++ 3 files changed, 19 insertions(+) create mode 100644 tests/run-with-compiler/quote-lift-BigDecimal.check create mode 100644 tests/run-with-compiler/quote-lift-BigDecimal.scala diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index ebbc8289a931..747f882d8816 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -240,4 +240,13 @@ object Liftable { } } + /** Lift a BigDecimal using the default MathContext */ + given as Liftable[BigDecimal] = new Liftable[BigDecimal] { + def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = { + lazy val xInt: Int = x.intValue() + if (x.isValidInt && -512 <= xInt && xInt < 512) '{ BigDecimal(${xInt.toExpr}) } // Cached BigDecimal + else '{ BigDecimal(${x.toString.toExpr}) } + } + } + } diff --git a/tests/run-with-compiler/quote-lift-BigDecimal.check b/tests/run-with-compiler/quote-lift-BigDecimal.check new file mode 100644 index 000000000000..70b0ca75b20e --- /dev/null +++ b/tests/run-with-compiler/quote-lift-BigDecimal.check @@ -0,0 +1 @@ +(2.3,1005849025843905834908593485984390583429058574925.95489543) diff --git a/tests/run-with-compiler/quote-lift-BigDecimal.scala b/tests/run-with-compiler/quote-lift-BigDecimal.scala new file mode 100644 index 000000000000..0b30e6c2658a --- /dev/null +++ b/tests/run-with-compiler/quote-lift-BigDecimal.scala @@ -0,0 +1,9 @@ +import scala.quoted._ +object Test { + implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader) + def main(args: Array[String]): Unit = println(run { + val a = BigDecimal(2.3).toExpr + val b = BigDecimal("1005849025843905834908593485984390583429058574925.95489543").toExpr + '{ ($a, $b) } + }) +} From 89ad39bd5811bbcb37c4359543b48784a6f6a292 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 27 Jul 2019 15:09:07 +0200 Subject: [PATCH 3/5] Remove optimizations based on implementation details --- library/src/scala/quoted/Liftable.scala | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index 747f882d8816..5ebc7d0e572d 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -233,20 +233,14 @@ object Liftable { } given as Liftable[BigInt] = new Liftable[BigInt] { - def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = { - lazy val xInt: Int = x.intValue() - if (x.isValidInt && -1024 <= xInt && xInt < 1024) '{ BigInt(${xInt.toExpr}) } // Cached BigInt - else '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } // Compact string BigInt - } + def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = + '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } } /** Lift a BigDecimal using the default MathContext */ given as Liftable[BigDecimal] = new Liftable[BigDecimal] { - def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = { - lazy val xInt: Int = x.intValue() - if (x.isValidInt && -512 <= xInt && xInt < 512) '{ BigDecimal(${xInt.toExpr}) } // Cached BigDecimal - else '{ BigDecimal(${x.toString.toExpr}) } - } + def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = + '{ BigDecimal(${x.toString.toExpr}) } } } From 52a237d40e198ec9e860cfdebef150ca3c39b6f6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 27 Jul 2019 16:28:27 +0200 Subject: [PATCH 4/5] Add `quoted.Liftable[Array[T]]` to the stdlib --- library/src/scala/quoted/Liftable.scala | 9 ++++++++- tests/run-with-compiler/quote-lib.scala | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index 5ebc7d0e572d..60af3f5eaeda 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -45,7 +45,14 @@ object Liftable { } } - given [T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] { + given ArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[Array[T]] = new Liftable[Array[T]] { + def toExpr(arr: Array[T]): given QuoteContext => Expr[Array[T]] = '{ + val array = new Array[T](${arr.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr})) + ${ Expr.block(List.tabulate(arr.length)(i => '{ array(${i.toExpr}) = ${arr(i).toExpr} }), '{ array }) } + } + } + + given IArrayIsLiftable[T: Type: Liftable: ClassTag] as Liftable[IArray[T]] = new Liftable[IArray[T]] { def toExpr(iarray: IArray[T]): given QuoteContext => Expr[IArray[T]] = '{ val array = new Array[T](${iarray.length.toExpr})(ClassTag(${the[ClassTag[T]].runtimeClass.toExpr})) ${ Expr.block(List.tabulate(iarray.length)(i => '{ array(${i.toExpr}) = ${iarray(i).toExpr} }), '{ array.asInstanceOf[IArray[T]] }) } diff --git a/tests/run-with-compiler/quote-lib.scala b/tests/run-with-compiler/quote-lib.scala index 94598f49d35f..a59122ad65fa 100644 --- a/tests/run-with-compiler/quote-lib.scala +++ b/tests/run-with-compiler/quote-lib.scala @@ -68,6 +68,9 @@ object Test { val iarray: IArray[Int] = IArray(1, 2, 3) val liftedIArray: Expr[IArray[Int]] = iarray + val array: Array[Int] = Array(1, 2, 3) + val liftedArray: Expr[Array[Int]] = array + val some: Option[Int] = Some(2) val none: Option[Int] = Some(2) val liftedSome: Expr[Option[Int]] = some From 2e083d572ab658977733c2f3e163a5259ce3bd2d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 27 Jul 2019 16:53:26 +0200 Subject: [PATCH 5/5] Use array of bytes co construnct BigInt --- library/src/scala/quoted/Liftable.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index 60af3f5eaeda..dd6cf91b5d84 100644 --- a/library/src/scala/quoted/Liftable.scala +++ b/library/src/scala/quoted/Liftable.scala @@ -241,7 +241,7 @@ object Liftable { given as Liftable[BigInt] = new Liftable[BigInt] { def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = - '{ BigInt(${x.toString(Character.MAX_RADIX).toExpr}, ${Character.MAX_RADIX.toExpr}) } + '{ BigInt(${x.toByteArray.toExpr}) } } /** Lift a BigDecimal using the default MathContext */