Skip to content

Allow unlifting precise types for primitives #10588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions library/src-bootstrapped/scala/quoted/Unliftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/expr-map-3.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo
foofoo
apply
18 changes: 18 additions & 0 deletions tests/run-macros/expr-map-3/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)

}
12 changes: 12 additions & 0 deletions tests/run-macros/expr-map-3/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object Test {

def main(args: Array[String]): Unit = {
println(rewrite("foo"))
println(rewrite("foo" + "foo"))

rewrite {
println("apply")
}
}

}
6 changes: 6 additions & 0 deletions tests/run-staging/unliftables.check
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions tests/run-staging/unliftables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down