Skip to content

Commit 1719ec6

Browse files
Merge pull request #10402 from dotty-staging/add-missing-owner-to-ValDef.let
Add missing owner for `ValDef.let`
2 parents 761f2a2 + dc03cb6 commit 1719ec6

File tree

14 files changed

+66
-50
lines changed

14 files changed

+66
-50
lines changed

compiler/src/scala/quoted/internal/impl/QuoteContextImpl.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,14 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
278278
def unapply(vdef: ValDef): Option[(String, TypeTree, Option[Term])] =
279279
Some((vdef.name.toString, vdef.tpt, optional(vdef.rhs)))
280280

281-
def let(name: String, rhs: Term)(body: Ident => Term): Term =
282-
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)
281+
def let(owner: Symbol, name: String, rhs: Term)(body: Ident => Term): Term =
282+
val vdef = tpd.SyntheticValDef(name.toTermName, rhs)(using ctx.withOwner(owner))
283283
val ref = tpd.ref(vdef.symbol).asInstanceOf[Ident]
284284
Block(List(vdef), body(ref))
285285

286-
def let(terms: List[Term])(body: List[Ident] => Term): Term =
287-
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term))
286+
def let(owner: Symbol, terms: List[Term])(body: List[Ident] => Term): Term =
287+
val ctx1 = ctx.withOwner(owner)
288+
val vdefs = terms.map(term => tpd.SyntheticValDef("x".toTermName, term)(using ctx1))
288289
val refs = vdefs.map(vdef => tpd.ref(vdef.symbol).asInstanceOf[Ident])
289290
Block(vdefs, body(refs))
290291
end ValDef
@@ -2203,6 +2204,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
22032204
type Symbol = dotc.core.Symbols.Symbol
22042205

22052206
object Symbol extends SymbolModule:
2207+
def spliceOwner: Symbol = ctx.owner
22062208
def currentOwner(using ctx: Context): Symbol = ctx.owner
22072209
def requiredPackage(path: String): Symbol = dotc.core.Symbols.requiredPackage(path)
22082210
def requiredClass(path: String): Symbol = dotc.core.Symbols.requiredClass(path)

library/src/scala/quoted/QuoteContext.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,14 @@ trait QuoteContext { self: internal.QuoteUnpickler & internal.QuoteMatching =>
386386
def unapply(vdef: ValDef): Option[(String, TypeTree, Option[Term])]
387387

388388
/** Creates a block `{ val <name> = <rhs: Term>; <body(x): Term> }` */
389-
def let(name: String, rhs: Term)(body: Ident => Term): Term
389+
def let(owner: Symbol, name: String, rhs: Term)(body: Ident => Term): Term
390390

391391
/** Creates a block `{ val x = <rhs: Term>; <body(x): Term> }` */
392-
def let(rhs: Term)(body: Ident => Term): Term = let("x", rhs)(body)
392+
def let(owner: Symbol, rhs: Term)(body: Ident => Term): Term =
393+
let(owner, "x", rhs)(body)
393394

394395
/** Creates a block `{ val x1 = <terms(0): Term>; ...; val xn = <terms(n-1): Term>; <body(List(x1, ..., xn)): Term> }` */
395-
def let(terms: List[Term])(body: List[Ident] => Term): Term
396+
def let(owner: Symbol, terms: List[Term])(body: List[Ident] => Term): Term
396397
}
397398

398399
given ValDefMethods as ValDefMethods = ValDefMethodsImpl
@@ -2673,6 +2674,17 @@ trait QuoteContext { self: internal.QuoteUnpickler & internal.QuoteMatching =>
26732674

26742675
trait SymbolModule { this: Symbol.type =>
26752676

2677+
/** Symbol of the definition that encloses the current splicing context.
2678+
*
2679+
* For example, the following call to `spliceOwner` would return the symbol `x`.
2680+
* ```
2681+
* val x = ${ ... Symbol.spliceOwner ... }
2682+
* ```
2683+
*
2684+
* For a macro splice, it is the symbol of the definition where the macro expansion happens.
2685+
*/
2686+
def spliceOwner: Symbol
2687+
26762688
/** Returns the symbol of the current enclosing definition */
26772689
def currentOwner(using ctx: Context): Symbol
26782690

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ object scalatest {
1111

1212
Term.of(cond).underlyingArgument match {
1313
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
14-
let(lhs) { left =>
15-
let(rhs) { right =>
14+
let(Symbol.currentOwner, lhs) { left =>
15+
let(Symbol.currentOwner, rhs) { right =>
1616
val app = Select.overloaded(left, op, Nil, right :: Nil)
17-
let(app) { result =>
17+
let(Symbol.currentOwner, app) { result =>
1818
val l = left.asExpr
1919
val r = right.asExpr
2020
val b = result.asExprOf[Boolean]

tests/pos-macros/i8866/Macro_1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ object Macro {
1515
import qctx.reflect._
1616

1717
ValDef.let(
18+
Symbol.currentOwner,
1819
Select.unique(
1920
Term.of('{ OtherMacro }),
2021
"apply"

tests/pos-macros/i8866b/Macro_1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ object Macro {
1010
import qctx.reflect._
1111

1212
ValDef.let(
13+
Symbol.spliceOwner,
1314
Select.unique(
1415
Term.of('{ Other }),
1516
"apply"

tests/run-macros/i6171/Macro_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
ValDef.let(app) { result =>
20+
ValDef.let(Symbol.spliceOwner, app) { result =>
2121
val l = left.asExpr
2222
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.asExprOf[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
ValDef.let(lhs) { left =>
32-
ValDef.let(rhs) { right =>
31+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
32+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
ValDef.let(Apply(app, implicits)) { result =>
34+
ValDef.let(Symbol.spliceOwner, Apply(app, implicits)) { result =>
3535
val l = left.asExpr
3636
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]

tests/run-macros/reflect-dsl/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
1919
val app = left.select(sel.symbol).appliedTo(right)
20-
ValDef.let(app) { result =>
20+
ValDef.let(Symbol.spliceOwner, app) { result =>
2121
val l = left.asExpr
2222
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.asExprOf[Unit]
2929
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
ValDef.let(lhs) { left =>
32-
ValDef.let(rhs) { right =>
31+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
32+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
3333
val app = qual.appliedTo(left).select(sel.symbol).appliedTo(right)
34-
ValDef.let(Apply(app, implicits)) { result =>
34+
ValDef.let(Symbol.spliceOwner, Apply(app, implicits)) { result =>
3535
val l = left.asExpr
3636
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]

tests/run-macros/reflect-pos-fun/assert_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object scalatest {
1010

1111
Term.of(cond).underlyingArgument match {
1212
case t @ Apply(TypeApply(Select(lhs, op), targs), rhs) =>
13-
ValDef.let(lhs) { left =>
14-
ValDef.let(rhs) { rs =>
13+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
14+
ValDef.let(Symbol.spliceOwner, rhs) { rs =>
1515
val app = Select.overloaded(left, op, targs.map(_.tpe), rs)
1616
val b = app.asExprOf[Boolean]
1717
Term.of('{ scala.Predef.assert($b) })

tests/run-macros/reflect-select-constructor/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
ValDef.let(app) { result =>
20+
ValDef.let(Symbol.spliceOwner, app) { result =>
2121
val l = left.asExpr
2222
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.asExprOf[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
ValDef.let(lhs) { left =>
32-
ValDef.let(rhs) { right =>
31+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
32+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
ValDef.let(Apply(app, implicits)) { result =>
34+
ValDef.let(Symbol.spliceOwner, Apply(app, implicits)) { result =>
3535
val l = left.asExpr
3636
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]

tests/run-macros/reflect-select-copy-2/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
19-
ValDef.let(Apply(Select.copy(sel)(left, op), right :: Nil)) { result =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
19+
ValDef.let(Symbol.spliceOwner, Apply(Select.copy(sel)(left, op), right :: Nil)) { result =>
2020
val l = left.asExpr
2121
val r = right.asExpr
2222
val b = result.asExprOf[Boolean]
@@ -27,9 +27,9 @@ object scalatest {
2727
}.asExprOf[Unit]
2828
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
2929
if isImplicitMethodType(f.tpe) =>
30-
ValDef.let(lhs) { left =>
31-
ValDef.let(rhs) { right =>
32-
ValDef.let(Apply(Apply(Select.copy(sel)(Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
30+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
31+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
32+
ValDef.let(Symbol.spliceOwner, Apply(Apply(Select.copy(sel)(Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
3333
val l = left.asExpr
3434
val r = right.asExpr
3535
val b = result.asExprOf[Boolean]

tests/run-macros/reflect-select-symbol-constructor/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
1919
val app = Apply(Select(left, sel.symbol), right :: Nil)
20-
ValDef.let(app) { result =>
20+
ValDef.let(Symbol.spliceOwner, app) { result =>
2121
val l = left.asExpr
2222
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.asExprOf[Unit]
2929
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
ValDef.let(lhs) { left =>
32-
ValDef.let(rhs) { right =>
31+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
32+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
3333
val app = Apply(Select(Apply(qual, left :: Nil), sel.symbol), right :: Nil)
34-
ValDef.let(Apply(app, implicits)) { result =>
34+
ValDef.let(Symbol.spliceOwner, Apply(app, implicits)) { result =>
3535
val l = left.asExpr
3636
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]

tests/run-macros/reflect-select-value-class/assert_1.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object scalatest {
1414

1515
Term.of(cond).underlyingArgument match {
1616
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
17-
ValDef.let(lhs) { left =>
18-
ValDef.let(rhs) { right =>
17+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
18+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
20-
ValDef.let(app) { result =>
20+
ValDef.let(Symbol.spliceOwner, app) { result =>
2121
val l = left.asExpr
2222
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
@@ -28,10 +28,10 @@ object scalatest {
2828
}.asExprOf[Unit]
2929
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
3030
if isImplicitMethodType(f.tpe) =>
31-
ValDef.let(lhs) { left =>
32-
ValDef.let(rhs) { right =>
31+
ValDef.let(Symbol.spliceOwner, lhs) { left =>
32+
ValDef.let(Symbol.spliceOwner, rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
34-
ValDef.let(Apply(app, implicits)) { result =>
34+
ValDef.let(Symbol.spliceOwner, Apply(app, implicits)) { result =>
3535
val l = left.asExpr
3636
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]

tests/run-macros/tasty-unsafe-let/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Macros {
1111
val rhsTerm = Term.of(rhs)
1212

1313
import qctx.reflect._
14-
ValDef.let(rhsTerm) { rhsId =>
14+
ValDef.let(Symbol.spliceOwner, rhsTerm) { rhsId =>
1515
Term.of(Expr.betaReduce('{$body(${rhsId.asExpr.asInstanceOf[Expr[T]]})})) // Dangerous uncheked cast!
1616
}.asExprOf[Unit]
1717
}

0 commit comments

Comments
 (0)