Skip to content

Commit 0cfe6e6

Browse files
Merge pull request #6399 from dotty-staging/avoid-ambigouities-with-expr-and-tasty-extractors
Rename extractors in quoted.matching to avoid conflicts with tasty.Reflect
2 parents 8ddba1b + 18c61b6 commit 0cfe6e6

File tree

9 files changed

+44
-43
lines changed

9 files changed

+44
-43
lines changed

library/src/scala/quoted/matching/Literal.scala renamed to library/src/scala/quoted/matching/Const.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ import scala.quoted.Expr
44

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

7-
/** Matches expressions containing literal values and extracts the value.
7+
/** Matches expressions containing literal constant values and extracts the value.
88
* It may match expressions of type Boolean, Byte, Short, Int, Long,
99
* Float, Double, Char, String, ClassTag, scala.Symbol, Null and Unit.
1010
*
1111
* Usage:
1212
* ```
1313
* (x: Expr[B]) match {
14-
* case Literal(value: B) => ...
14+
* case Const(value: B) => ...
1515
* }
1616
* ```
1717
*/
18-
object Literal {
18+
object Const {
1919

2020
def unapply[T](expr: Expr[T])(implicit reflect: Reflection): Option[T] = {
21-
import reflect.{Literal => LiteralTree, _} // TODO rename reflect.Literal to avoid this clash
22-
def literal(tree: Term): Option[T] = tree match {
23-
case LiteralTree(c) => Some(c.value.asInstanceOf[T])
24-
case Block(Nil, e) => literal(e)
25-
case Inlined(_, Nil, e) => literal(e)
21+
import reflect._
22+
def rec(tree: Term): Option[T] = tree match {
23+
case Literal(c) => Some(c.value.asInstanceOf[T])
24+
case Block(Nil, e) => rec(e)
25+
case Inlined(_, Nil, e) => rec(e)
2626
case _ => None
2727
}
28-
literal(expr.unseal)
28+
rec(expr.unseal)
2929
}
3030

3131
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package scala.quoted.matching
2+
3+
import scala.quoted.Expr
4+
5+
import scala.tasty.Reflection // TODO do not depend on reflection directly
6+
7+
/** Literal sequence of expressions */
8+
object ExprSeq {
9+
10+
/** Matches a literal sequence of expressions */
11+
def unapply[T](expr: Expr[Seq[T]])(implicit reflect: Reflection): Option[Seq[Expr[T]]] = {
12+
import reflect._
13+
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
14+
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]]))
15+
case Block(Nil, e) => rec(e)
16+
case Inlined(_, Nil, e) => rec(e)
17+
case _ => None
18+
}
19+
rec(expr.unseal)
20+
}
21+
22+
}

library/src/scala/quoted/matching/Repeated.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/run-macros/quote-matcher-string-interpolator-2/quoted_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ object Macros {
99

1010
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = {
1111
(self, args) match {
12-
case ('{ StringContext(${Repeated(parts)}: _*) }, Repeated(args1)) =>
13-
val strParts = parts.map { case Literal(str) => str.reverse }
14-
val strArgs = args1.map { case Literal(str) => str }
12+
case ('{ StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args1)) =>
13+
val strParts = parts.map { case Const(str) => str.reverse }
14+
val strArgs = args1.map { case Const(str) => str }
1515
StringContext(strParts: _*).s(strArgs: _*).toExpr
1616
case _ => ???
1717
}

tests/run-macros/quote-matcher-string-interpolator/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macros {
99

1010
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(implicit reflect: Reflection): Expr[String] = {
1111
self match {
12-
case '{ StringContext(${Repeated(parts)}: _*) } =>
12+
case '{ StringContext(${ExprSeq(parts)}: _*) } =>
1313
val parts2 = parts.map(x => '{ $x.reverse }).toList.toExprOfList
1414
'{ StringContext($parts2: _*).s($args: _*) }
1515
case _ =>

tests/run-macros/quote-matcher-symantics-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Macros {
1313

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

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

tests/run-macros/quote-matcher-symantics-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object Macros {
1515

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

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

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

tests/run-macros/tasty-extractors-constants-1/quoted_1.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ object Macros {
1515
val buff = new StringBuilder
1616
def stagedPrintln(x: Any): Unit = buff append java.util.Objects.toString(x) append "\n"
1717

18-
3.toExpr match { case Literal(n) => stagedPrintln(n) }
19-
'{4} match { case Literal(n) => stagedPrintln(n) }
20-
'{"abc"} match { case Literal(n) => stagedPrintln(n) }
21-
'{null} match { case Literal(n) => stagedPrintln(n) }
18+
3.toExpr match { case Const(n) => stagedPrintln(n) }
19+
'{4} match { case Const(n) => stagedPrintln(n) }
20+
'{"abc"} match { case Const(n) => stagedPrintln(n) }
21+
'{null} match { case Const(n) => stagedPrintln(n) }
2222

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

2525
'{print(${buff.result()})}
2626
}

tests/run-with-compiler/tasty-extractors-constants-2/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ object Macros {
4545
}
4646

4747
def power(n: Expr[Int], x: Expr[Double])(implicit reflect: Reflection): Expr[Double] = {
48-
import quoted.matching.Literal
48+
import quoted.matching.Const
4949
n match {
50-
case Literal(n1) => powerCode(n1, x)
50+
case Const(n1) => powerCode(n1, x)
5151
case _ => '{ dynamicPower($n, $x) }
5252
}
5353
}

0 commit comments

Comments
 (0)