Skip to content

Commit ae85c48

Browse files
committed
Allow nested Quotes with a different owners
This makes it possible to create `Expr`s with different owners to avoid a call to `changeOwner`.
1 parent a771c86 commit ae85c48

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26172617

26182618
def show(using printer: Printer[Symbol]): String = printer.show(self)
26192619

2620+
def asQuotes: Nested = new QuotesImpl(using ctx.withOwner(self))
2621+
26202622
end extension
26212623

26222624
private def appliedTypeRef(sym: Symbol): TypeRepr =

library/src/scala/quoted/Quotes.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3774,6 +3774,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37743774

37753775
/** Case class or case object children of a sealed trait or cases of an `enum`. */
37763776
def children: List[Symbol]
3777+
3778+
/** Returns a nested quote with this symbol as an owner */
3779+
@experimental
3780+
def asQuotes: Nested
3781+
37773782
end extension
37783783
}
37793784

project/MiMaFilters.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ import com.typesafe.tools.mima.core.ProblemFilters._
44

55
object MiMaFilters {
66
val Library: Seq[ProblemFilter] = Seq(
7+
// New APIs that will be introduced in 3.2.0
8+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
9+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
710
)
811
}

tests/pos-macros/i13571/Macro_1.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
inline def checked2[A](inline n: A): A =
4+
${ checkedImpl2[A]('{n}) }
5+
6+
private def checkedImpl2[A](n: Expr[A])(using Quotes, Type[A]): Expr[A] =
7+
import quotes.reflect.*
8+
val tree: Term = n.asTerm
9+
val acc = new TreeMap:
10+
override def transformTerm(tree: Term)(owner: Symbol): Term =
11+
tree match
12+
case Apply(Select(x, "*"), List(y)) =>
13+
given Quotes = owner.asQuotes
14+
'{
15+
val xt = ${x.asExprOf[Long]}
16+
xt
17+
}.asTerm
18+
case _ =>
19+
super.transformTerm(tree)(owner)
20+
acc.transformTerm(tree)(Symbol.spliceOwner).asExprOf[A]

tests/pos-macros/i13571/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test = {
2+
val u = 3L
3+
checked2(List(1L, 2L).map { k =>
4+
u * 2L
5+
})
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import scala.quoted.*
2+
3+
inline def checked2[A](inline n: A): A =
4+
${ checkedImpl2[A]('{n}) }
5+
6+
private def checkedImpl2[A](n: Expr[A])(using Quotes, Type[A]): Expr[A] =
7+
import quotes.reflect.*
8+
val tree: Term = n.asTerm
9+
val acc = new TreeMap:
10+
override def transformTerm(tree: Term)(owner: Symbol): Term =
11+
tree match
12+
case Apply(Select(x, "*"), List(y)) =>
13+
bindLong(x.asExprOf[Long])(using owner.asQuotes).asTerm
14+
case _ =>
15+
super.transformTerm(tree)(owner)
16+
acc.transformTerm(tree)(Symbol.spliceOwner).asExprOf[A]
17+
18+
def bindLong(expr: Expr[Long])(using Quotes): Expr[Long] =
19+
'{
20+
val xt = $expr
21+
xt
22+
}

tests/pos-macros/i13571b/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test = {
2+
val u = 3L
3+
checked2(List(1L, 2L).map { k =>
4+
u * 2L
5+
})
6+
}

0 commit comments

Comments
 (0)