Skip to content

Commit 663d77b

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`. Closes #13922
1 parent 0a834ff commit 663d77b

File tree

9 files changed

+174
-3
lines changed

9 files changed

+174
-3
lines changed

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

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

26262626
def show(using printer: Printer[Symbol]): String = printer.show(self)
26272627

2628+
def asQuotes: Nested = new QuotesImpl(using ctx.withOwner(self))
2629+
26282630
end extension
26292631

26302632
private def appliedTypeRef(sym: Symbol): TypeRepr =
@@ -2915,6 +2917,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
29152917
|which has the AST representation
29162918
|${Printer.TreeStructure.show(tree)}
29172919
|
2920+
|
2921+
|
2922+
|Tip: The owner of a tree can be changed using method `Tree.changeOwner`.
2923+
|Tip: The default owner of definitions created in quotes can be changed using method `Symbol.asQuotes`.
29182924
|""".stripMargin)
29192925
case _ => traverseChildren(t)
29202926
}.traverse(tree)

library/src/scala/quoted/Quotes.scala

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,45 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
607607
def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef
608608
def unapply(vdef: ValDef): (String, TypeTree, Option[Term])
609609

610-
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }` */
610+
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }`
611+
*
612+
* Usage:
613+
* ```
614+
* ValDef.let(owner, "x", rhs1) { x =>
615+
* ValDef.let(x.symbol.owner, "y", rhs2) { y =>
616+
* // use `x` and `y`
617+
* }
618+
* }
619+
* ```
620+
* @syntax markdown
621+
*/
611622
def let(owner: Symbol, name: String, rhs: Term)(body: Ref => Term): Term
612623

613-
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
624+
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }`
625+
*
626+
* Usage:
627+
* ```
628+
* ValDef.let(owner, rhs1) { x =>
629+
* ValDef.let(owner, rhs2) { y =>
630+
* // use `x` and `y`
631+
* }
632+
* }
633+
* ```
634+
* @syntax markdown
635+
*/
614636
def let(owner: Symbol, rhs: Term)(body: Ref => Term): Term =
615637
let(owner, "x", rhs)(body)
616638

617-
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
639+
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }`
640+
*
641+
* Usage:
642+
* ```
643+
* ValDef.let(owner, rhsList) { xs =>
644+
* ...
645+
* }
646+
* ```
647+
* @syntax markdown
648+
*/
618649
def let(owner: Symbol, terms: List[Term])(body: List[Ref] => Term): Term
619650
}
620651

@@ -1327,6 +1358,28 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
13271358
* ```scala sc:nocompile
13281359
* Block((DefDef(_, _, params :: Nil, _, Some(rhsFn(meth, paramRefs)))) :: Nil, Closure(meth, _))
13291360
* ```
1361+
*
1362+
* Usage:
1363+
* ```
1364+
* val mtpe = MethodType(List("arg1"))(_ => List(TypeRepr.of[Int]), _ => TypeRepr.of[Int])
1365+
* Lambda(owner, mtpe, {
1366+
* case (methSym, List(arg1: Term)) =>
1367+
* ValDef.let(methSym, f(arg1)) { ... }
1368+
* }
1369+
* )
1370+
* ```
1371+
*
1372+
* Usage with quotes:
1373+
* ```
1374+
* val mtpe = MethodType(List("arg1"))(_ => List(TypeRepr.of[Int]), _ => TypeRepr.of[Int])
1375+
* Lambda(owner, mtpe, {
1376+
* case (methSym, List(arg1: Term)) =>
1377+
* given Quotes = methSym.asQuotes
1378+
* '{ ... }
1379+
* }
1380+
* )
1381+
* ```
1382+
*
13301383
* @param owner: owner of the generated `meth` symbol
13311384
* @param tpe: Type of the definition
13321385
* @param rhsFn: Function that receives the `meth` symbol and the a list of references to the `params`
@@ -3792,6 +3845,27 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37923845

37933846
/** Case class or case object children of a sealed trait or cases of an `enum`. */
37943847
def children: List[Symbol]
3848+
3849+
/** Returns a nested quote with this symbol as splice owner (`Symbol.spliceOwner`).
3850+
*
3851+
* Changes the owner under which the definition in a quote are created.
3852+
*
3853+
* Usage:
3854+
* ```scala
3855+
* new TreeMap:
3856+
* override def transformTerm(tree: Term)(owner: Symbol): Term =
3857+
* tree match
3858+
* case tree: Ident =>
3859+
* given Quotes = owner.asQuotes
3860+
* // Definitions contained in the quote will be owned by `owner`.
3861+
* // No need to use `changeOwner` in this case.
3862+
* '{ val x = ???; x }.asTerm
3863+
* ```
3864+
* @syntax markdown
3865+
*/
3866+
@experimental
3867+
def asQuotes: Nested
3868+
37953869
end extension
37963870
}
37973871

project/MiMaFilters.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ object MiMaFilters {
77
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.init"),
88
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.last"),
99
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
10+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
11+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.asQuotes"),
1012
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
1113
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
1214
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.typeArgs"),

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+
}

tests/pos-macros/i13922/Macro_1.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted.*
2+
3+
inline def optPrettyPrinter[T]: Option[T] => Option[T] =
4+
${ optPrettyPrinterImpl[T] }
5+
6+
private def optPrettyPrinterImpl[T: Type](using Quotes): Expr[Option[T] => Option[T]] = {
7+
import quotes.reflect.*
8+
9+
val tpe = TypeRepr.of[T]
10+
11+
val fn = Lambda(
12+
Symbol.spliceOwner,
13+
MethodType(List("macroVal"))(
14+
_ => List(tpe),
15+
_ => tpe
16+
),
17+
{
18+
case (m, List(arg: Term)) =>
19+
given Quotes = m.asQuotes
20+
ValDef.let(m, "v", arg) { v =>
21+
'{
22+
val vv = ${ v.asExprOf[T] }
23+
println("v=" + vv.toString())
24+
vv
25+
}.asTerm
26+
}
27+
28+
case _ =>
29+
report.errorAndAbort("Fails compile")
30+
}
31+
).asExprOf[T => T]
32+
33+
'{ (_: Option[T]).map(${ fn }) }
34+
}

tests/pos-macros/i13922/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test = optPrettyPrinter(Some("foo"))

0 commit comments

Comments
 (0)