Skip to content

Rename extractors in quoted.matching to avoid conflicts with tasty.Reflect #6399

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import scala.quoted.Expr

import scala.tasty.Reflection // TODO do not depend on reflection directly

/** Matches expressions containing literal values and extracts the value.
/** Matches expressions containing literal constant values and extracts the value.
* It may match expressions of type Boolean, Byte, Short, Int, Long,
* Float, Double, Char, String, ClassTag, scala.Symbol, Null and Unit.
*
* Usage:
* ```
* (x: Expr[B]) match {
* case Literal(value: B) => ...
* case Const(value: B) => ...
* }
* ```
*/
object Literal {
object Const {

def unapply[T](expr: Expr[T])(implicit reflect: Reflection): Option[T] = {
import reflect.{Literal => LiteralTree, _} // TODO rename reflect.Literal to avoid this clash
def literal(tree: Term): Option[T] = tree match {
case LiteralTree(c) => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => literal(e)
case Inlined(_, Nil, e) => literal(e)
import reflect._
def rec(tree: Term): Option[T] = tree match {
case Literal(c) => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => rec(e)
case Inlined(_, Nil, e) => rec(e)
case _ => None
}
literal(expr.unseal)
rec(expr.unseal)
}

}
22 changes: 22 additions & 0 deletions library/src/scala/quoted/matching/ExprSeq.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package scala.quoted.matching

import scala.quoted.Expr

import scala.tasty.Reflection // TODO do not depend on reflection directly

/** Literal sequence of expressions */
object ExprSeq {

/** Matches a literal sequence of expressions */
def unapply[T](expr: Expr[Seq[T]])(implicit reflect: Reflection): Option[Seq[Expr[T]]] = {
import reflect._
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]]))
case Block(Nil, e) => rec(e)
case Inlined(_, Nil, e) => rec(e)
case _ => None
}
rec(expr.unseal)
}

}
21 changes: 0 additions & 21 deletions library/src/scala/quoted/matching/Repeated.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ object Macros {

private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = {
(self, args) match {
case ('{ StringContext(${Repeated(parts)}: _*) }, Repeated(args1)) =>
val strParts = parts.map { case Literal(str) => str.reverse }
val strArgs = args1.map { case Literal(str) => str }
case ('{ StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args1)) =>
val strParts = parts.map { case Const(str) => str.reverse }
val strArgs = args1.map { case Const(str) => str }
StringContext(strParts: _*).s(strArgs: _*).toExpr
case _ => ???
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Macros {

private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = {
self match {
case '{ StringContext(${Repeated(parts)}: _*) } =>
case '{ StringContext(${ExprSeq(parts)}: _*) } =>
val parts2 = parts.map(x => '{ $x.reverse }).toList.toExprOfList
'{ StringContext($parts2: _*).s($args: _*) }
case _ =>
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quote-matcher-symantics-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Macros {

def lift(e: Expr[DSL]): Expr[T] = e match {

case '{ LitDSL(${ Literal(c) }) } =>
case '{ LitDSL(${ Const(c) }) } =>
// case scala.internal.quoted.Matcher.unapply[Tuple1[Expr[Int]]](Tuple1(Literal(c)))(/*implicits*/ '{ LitDSL(patternHole[Int]) }, reflect) =>
'{ $sym.value(${c.toExpr}) }

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Macros {

def lift(e: Expr[DSL])(implicit env: Map[Bind[DSL], Expr[T]]): Expr[T] = e match {

case '{ LitDSL(${Literal(c)}) } => sym.value(c)
case '{ LitDSL(${Const(c)}) } => sym.value(c)

case '{ ($x: DSL) + ($y: DSL) } => sym.plus(lift(x), lift(y))

Expand Down
10 changes: 5 additions & 5 deletions tests/run-macros/tasty-extractors-constants-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ object Macros {
val buff = new StringBuilder
def stagedPrintln(x: Any): Unit = buff append java.util.Objects.toString(x) append "\n"

3.toExpr match { case Literal(n) => stagedPrintln(n) }
'{4} match { case Literal(n) => stagedPrintln(n) }
'{"abc"} match { case Literal(n) => stagedPrintln(n) }
'{null} match { case Literal(n) => stagedPrintln(n) }
3.toExpr match { case Const(n) => stagedPrintln(n) }
'{4} match { case Const(n) => stagedPrintln(n) }
'{"abc"} match { case Const(n) => stagedPrintln(n) }
'{null} match { case Const(n) => stagedPrintln(n) }

'{new Object} match { case Literal(n) => println(n); case _ => stagedPrintln("OK") }
'{new Object} match { case Const(n) => println(n); case _ => stagedPrintln("OK") }

'{print(${buff.result()})}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ object Macros {
}

def power(n: Expr[Int], x: Expr[Double])(implicit reflect: Reflection): Expr[Double] = {
import quoted.matching.Literal
import quoted.matching.Const
n match {
case Literal(n1) => powerCode(n1, x)
case Const(n1) => powerCode(n1, x)
case _ => '{ dynamicPower($n, $x) }
}
}
Expand Down