Skip to content

Commit 6a81479

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 fa56a4e commit 6a81479

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

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

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

26202620
def show(using printer: Printer[Symbol]): String = printer.show(self)
26212621

2622+
def asQuotes: Nested = new QuotesImpl(using ctx.withOwner(self))
2623+
26222624
end extension
26232625

26242626
private def appliedTypeRef(sym: Symbol): TypeRepr =
@@ -2909,6 +2911,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
29092911
|which has the AST representation
29102912
|${Printer.TreeStructure.show(tree)}
29112913
|
2914+
|
2915+
|
2916+
|Tip: The owner of a tree can be changed using method `Tree.changeOwner`.
2917+
|Tip: The default owner of definitions created in quotes can be changed using method `Symbol.asQuotes`.
29122918
|""".stripMargin)
29132919
case _ => traverseChildren(t)
29142920
}.traverse(tree)

library/src/scala/quoted/Quotes.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,6 +3780,27 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37803780

37813781
/** Case class or case object children of a sealed trait or cases of an `enum`. */
37823782
def children: List[Symbol]
3783+
3784+
/** Returns a nested quote with this symbol as splice owner (`Symbol.spliceOwner`).
3785+
*
3786+
* Changes the owner under which the definition in a quote are created.
3787+
*
3788+
* Usage:
3789+
* ```scala
3790+
* new TreeMap:
3791+
* override def transformTerm(tree: Term)(owner: Symbol): Term =
3792+
* tree match
3793+
* case tree: Ident =>
3794+
* given Quotes = owner.asQuotes
3795+
* // Definitions contained in the quote will be owned by `owner`.
3796+
* // No need to use `changeOwner` in this case.
3797+
* '{ val x = ???; x }.asTerm
3798+
* ```
3799+
* @syntax markdown
3800+
*/
3801+
@experimental
3802+
def asQuotes: Nested
3803+
37833804
end extension
37843805
}
37853806

project/MiMaFilters.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ object MiMaFilters {
99
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
1010
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
1111
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
12+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
13+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
1214

1315
// Should have been added in 3.1.0
1416
// These are only allowed on imports and therefore should not be present in binaries emitted before

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)