diff --git a/library/src-bootstrapped/scala/quoted/Unliftable.scala b/library/src-bootstrapped/scala/quoted/Unliftable.scala index 1df394dadfa6..dbff93558d91 100644 --- a/library/src-bootstrapped/scala/quoted/Unliftable.scala +++ b/library/src-bootstrapped/scala/quoted/Unliftable.scala @@ -28,58 +28,58 @@ object Unliftable { * - Unlifts `'{false}` into `Some(false)` * - Otherwise unlifts to `None` */ - given BooleanUnliftable as Unliftable[Boolean] = new PrimitiveUnliftable + given BooleanUnliftable[T <: Boolean] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Byte * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Byte` * - Otherwise unlifts to `None` */ - given ByteUnliftable as Unliftable[Byte] = new PrimitiveUnliftable + given ByteUnliftable[T <: Byte] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Short * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Short` * - Otherwise unlifts to `None` */ - given ShortUnliftable as Unliftable[Short] = new PrimitiveUnliftable + given ShortUnliftable[T <: Short] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Int * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Int` * - Otherwise unlifts to `None` */ - given IntUnliftable as Unliftable[Int] = new PrimitiveUnliftable + given IntUnliftable[T <: Int] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Long * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Long` * - Otherwise unlifts to `None` */ - given LongUnliftable as Unliftable[Long] = new PrimitiveUnliftable + given LongUnliftable[T <: Long] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Float * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Float` * - Otherwise unlifts to `None` */ - given FloatUnliftable as Unliftable[Float] = new PrimitiveUnliftable + given FloatUnliftable[T <: Float] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Double * - Unlifts `'{n}` into `Some(n)` for a literal `n` of type `Double` * - Otherwise unlifts to `None` */ - given DoubleUnliftable as Unliftable[Double] = new PrimitiveUnliftable + given DoubleUnliftable[T <: Double] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for Char * - Unlifts `'{c}` into `Some(c)` for a literal `c` of type `Char` * - Otherwise unlifts to `None` */ - given CharUnliftable as Unliftable[Char] = new PrimitiveUnliftable + given CharUnliftable[T <: Char] as Unliftable[T] = new PrimitiveUnliftable /** Default unliftable for String * - Unlifts `'{str}` into `Some(str)` for a literal `str` of type `String` * - Otherwise unlifts to `None` */ - given StringUnliftable as Unliftable[String] = new PrimitiveUnliftable + given StringUnliftable[T <: String] as Unliftable[T] = new PrimitiveUnliftable /** Lift a quoted primitive value `'{ x }` into `x` */ - private class PrimitiveUnliftable[T <: Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] { + private class PrimitiveUnliftable[T <: Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Unliftable[T] { def fromExpr(expr: Expr[T]) = import quotes.reflect._ def rec(tree: Term): Option[T] = tree match { diff --git a/tests/run-macros/expr-map-3.check b/tests/run-macros/expr-map-3.check new file mode 100644 index 000000000000..8b345dc10854 --- /dev/null +++ b/tests/run-macros/expr-map-3.check @@ -0,0 +1,3 @@ +foo +foofoo +apply diff --git a/tests/run-macros/expr-map-3/Macro_1.scala b/tests/run-macros/expr-map-3/Macro_1.scala new file mode 100644 index 000000000000..5eb4e70aa794 --- /dev/null +++ b/tests/run-macros/expr-map-3/Macro_1.scala @@ -0,0 +1,18 @@ +import scala.quoted._ + + +inline def rewrite[T](inline x: Any): Any = ${ stringRewriter('x) } + +private def stringRewriter(e: Expr[Any])(using Quotes): Expr[Any] = + StringRewriter.transform(e) + +private object StringRewriter extends ExprMap { + + def transform[T](e: Expr[T])(using Type[T])(using Quotes): Expr[T] = e match + case '{ ${Unlifted(s)}: String } => + // checkIfValid(s) + val s2: String & T = s + Expr(s2) + case _ => transformChildren(e) + +} diff --git a/tests/run-macros/expr-map-3/Test_2.scala b/tests/run-macros/expr-map-3/Test_2.scala new file mode 100644 index 000000000000..39be2f52b5a0 --- /dev/null +++ b/tests/run-macros/expr-map-3/Test_2.scala @@ -0,0 +1,12 @@ +object Test { + + def main(args: Array[String]): Unit = { + println(rewrite("foo")) + println(rewrite("foo" + "foo")) + + rewrite { + println("apply") + } + } + +} diff --git a/tests/run-staging/unliftables.check b/tests/run-staging/unliftables.check index b28424f347f4..731e31e17c7e 100644 --- a/tests/run-staging/unliftables.check +++ b/tests/run-staging/unliftables.check @@ -8,8 +8,14 @@ Some(6) Some(7.1) Some(8.1) +Some(4) +Some(5) +Some(8.2) + Some(a) +Some(b) Some(abc) +Some(def) Some(List(1, 2, 3)) Some(List(1, 2, 3)) diff --git a/tests/run-staging/unliftables.scala b/tests/run-staging/unliftables.scala index fbc76762c50d..893817fa0885 100644 --- a/tests/run-staging/unliftables.scala +++ b/tests/run-staging/unliftables.scala @@ -13,8 +13,14 @@ object Test { println('{ 7.1: Float }.unlift) println('{ 8.1: Double }.unlift) println() + println(('{ 4 }: Expr[4]).unlift) + println(('{ 5L }: Expr[5L]).unlift) + println(('{ 8.2 }: Expr[8.2]).unlift) + println() println('{ 'a' }.unlift) + println(('{ 'b' }: Expr['b']).unlift) println('{ "abc" }.unlift) + println(('{ "def" }: Expr["def"]).unlift) println() // TODO? // println('{ classOf[String] }.unlift)