diff --git a/library/src/scala/quoted/Liftable.scala b/library/src/scala/quoted/Liftable.scala index b6626d3643ce..dd6cf91b5d84 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]] }) } @@ -232,4 +239,15 @@ object Liftable { } } + given as Liftable[BigInt] = new Liftable[BigInt] { + def toExpr(x: BigInt): given QuoteContext => Expr[BigInt] = + '{ BigInt(${x.toByteArray.toExpr}) } + } + + /** Lift a BigDecimal using the default MathContext */ + given as Liftable[BigDecimal] = new Liftable[BigDecimal] { + def toExpr(x: BigDecimal): given QuoteContext => Expr[BigDecimal] = + '{ BigDecimal(${x.toString.toExpr}) } + } + } 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 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) } + }) +} 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) } + }) +}