@@ -122,7 +122,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
122
122
end extension
123
123
124
124
extension [ThisTree <: Tree ](self : ThisTree ):
125
- def changeOwner ( newOwner : Symbol ): ThisTree =
125
+ def adaptOwner ( using newOwner : Symbol ): ThisTree =
126
126
tpd.TreeOps (self).changeNonLocalOwners(newOwner).asInstanceOf [ThisTree ]
127
127
end extension
128
128
@@ -278,13 +278,14 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
278
278
def unapply (vdef : ValDef ): Option [(String , TypeTree , Option [Term ])] =
279
279
Some ((vdef.name.toString, vdef.tpt, optional(vdef.rhs)))
280
280
281
- def let (name : String , rhs : Term )(body : Ident => Term ): Term =
282
- val vdef = tpd.SyntheticValDef (name.toTermName, rhs)
281
+ def let (name : String , rhs : Term )(body : Ident => Term )( using owner : Owner ) : Term =
282
+ val vdef = tpd.SyntheticValDef (name.toTermName, rhs)( using ctx.withOwner(owner))
283
283
val ref = tpd.ref(vdef.symbol).asInstanceOf [Ident ]
284
284
Block (List (vdef), body(ref))
285
285
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 (terms : List [Term ])(body : List [Ident ] => Term )(using owner : Owner ): Term =
287
+ val ctx1 = ctx.withOwner(owner)
288
+ val vdefs = terms.map(term => tpd.SyntheticValDef (" x" .toTermName, term)(using ctx1))
288
289
val refs = vdefs.map(vdef => tpd.ref(vdef.symbol).asInstanceOf [Ident ])
289
290
Block (vdefs, body(refs))
290
291
end ValDef
@@ -365,15 +366,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
365
366
def tpe : TypeRepr = self.tpe
366
367
def underlyingArgument : Term = new tpd.TreeOps (self).underlyingArgument
367
368
def underlying : Term = new tpd.TreeOps (self).underlying
368
- def etaExpand (owner : Symbol ): Term = self.tpe.widen match {
369
+ def etaExpand (using owner : Owner ): Term = self.tpe.widen match {
369
370
case mtpe : Types .MethodType if ! mtpe.isParamDependent =>
370
371
val closureResType = mtpe.resType match {
371
372
case t : Types .MethodType => t.toFunctionType()
372
373
case t => t
373
374
}
374
375
val closureTpe = Types .MethodType (mtpe.paramNames, mtpe.paramInfos, closureResType)
375
376
val closureMethod = dotc.core.Symbols .newSymbol(owner, nme.ANON_FUN , Synthetic | Method , closureTpe)
376
- tpd.Closure (closureMethod, tss => new tpd.TreeOps (self).appliedToArgs(tss.head).etaExpand(closureMethod))
377
+ tpd.Closure (closureMethod, tss => new tpd.TreeOps (self).appliedToArgs(tss.head).etaExpand(using closureMethod))
377
378
case _ => self
378
379
}
379
380
@@ -735,9 +736,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
735
736
end ClosureMethodsImpl
736
737
737
738
object Lambda extends LambdaModule :
738
- def apply (owner : Symbol , tpe : MethodType , rhsFn : ( Symbol , List [Tree ]) => Tree ): Block =
739
+ def apply (tpe : MethodType , rhsFn : Owner ?=> List [Tree ] => Tree )( using owner : Owner ): Block =
739
740
val meth = dotc.core.Symbols .newSymbol(owner, nme.ANON_FUN , Synthetic | Method , tpe)
740
- tpd.Closure (meth, tss => yCheckedOwners(rhsFn(meth, tss.head), meth))
741
+ tpd.Closure (meth, tss => yCheckedOwners(rhsFn(using meth)( tss.head), meth))
741
742
742
743
def unapply (tree : Block ): Option [(List [ValDef ], Term )] = tree match {
743
744
case Block ((ddef @ DefDef (_, _, params :: Nil , _, Some (body))) :: Nil , Closure (meth, _))
@@ -2200,22 +2201,35 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
2200
2201
case _ => None
2201
2202
end AmbiguousImplicitsTypeTest
2202
2203
2204
+ type Owner = dotc.core.Symbols .Symbol
2205
+
2206
+ object Owner extends OwnerModule :
2207
+ def apply (sym : Symbol ): Owner =
2208
+ assert(sym.exists, " Cannot convert NoSymbol into an Owner" )
2209
+ sym
2210
+ def spliceOwner : Owner = ctx.owner
2211
+ end Owner
2212
+
2213
+ object OwnerMethodsImpl extends OwnerMethods :
2214
+ extension (self : Owner ):
2215
+ def symbol : Symbol = self
2216
+ end OwnerMethodsImpl
2217
+
2203
2218
type Symbol = dotc.core.Symbols .Symbol
2204
2219
2205
2220
object Symbol extends SymbolModule :
2206
- def currentOwner (using ctx : Context ): Symbol = ctx.owner
2207
2221
def requiredPackage (path : String ): Symbol = dotc.core.Symbols .requiredPackage(path)
2208
2222
def requiredClass (path : String ): Symbol = dotc.core.Symbols .requiredClass(path)
2209
2223
def requiredModule (path : String ): Symbol = dotc.core.Symbols .requiredModule(path)
2210
2224
def requiredMethod (path : String ): Symbol = dotc.core.Symbols .requiredMethod(path)
2211
2225
def classSymbol (fullName : String ): Symbol = dotc.core.Symbols .requiredClass(fullName)
2212
- def newMethod (owner : Symbol , name : String , tpe : TypeRepr ): Symbol =
2213
- newMethod(owner, name, tpe, Flags .EmptyFlags , noSymbol)
2214
- def newMethod (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2226
+ def newMethod (name : String , tpe : TypeRepr )( using owner : Owner ): Symbol =
2227
+ newMethod(name, tpe, Flags .EmptyFlags , noSymbol)
2228
+ def newMethod (name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol )( using owner : Owner ): Symbol =
2215
2229
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | dotc.core.Flags .Method , tpe, privateWithin)
2216
- def newVal (owner : Symbol , name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol ): Symbol =
2230
+ def newVal (name : String , tpe : TypeRepr , flags : Flags , privateWithin : Symbol )( using owner : Owner ): Symbol =
2217
2231
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags, tpe, privateWithin)
2218
- def newBind (owner : Symbol , name : String , flags : Flags , tpe : TypeRepr ): Symbol =
2232
+ def newBind (name : String , flags : Flags , tpe : TypeRepr )( using owner : Owner ): Symbol =
2219
2233
dotc.core.Symbols .newSymbol(owner, name.toTermName, flags | Case , tpe)
2220
2234
def noSymbol : Symbol = dotc.core.Symbols .NoSymbol
2221
2235
end Symbol
@@ -2593,6 +2607,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
2593
2607
|which has the AST representation
2594
2608
| ${TreeMethods .showExtractors(tree)}
2595
2609
|
2610
+ |Owners can be adapted using `Tree.adaptOwner`.
2596
2611
| """ .stripMargin)
2597
2612
case _ => traverseChildren(t)
2598
2613
}.traverse(tree)
0 commit comments