@@ -1296,6 +1296,9 @@ object Trees {
12961296 */
12971297 protected def inlineContext (call : Tree )(using Context ): Context = ctx
12981298
1299+ /** The context to use when mapping or accumulating over a tree */
1300+ def localCtx (tree : Tree )(using Context ): Context
1301+
12991302 abstract class TreeMap (val cpy : TreeCopier = inst.cpy) { self =>
13001303 def transform (tree : Tree )(using Context ): Tree = {
13011304 inContext(
@@ -1304,9 +1307,6 @@ object Trees {
13041307 else ctx
13051308 ){
13061309 Stats .record(s " TreeMap.transform/ $getClass" )
1307- def localCtx =
1308- if (tree.hasType && tree.symbol.exists) ctx.withOwner(tree.symbol) else ctx
1309-
13101310 if (skipTransform(tree)) tree
13111311 else tree match {
13121312 case Ident (name) =>
@@ -1362,11 +1362,11 @@ object Trees {
13621362 case AppliedTypeTree (tpt, args) =>
13631363 cpy.AppliedTypeTree (tree)(transform(tpt), transform(args))
13641364 case LambdaTypeTree (tparams, body) =>
1365- inContext(localCtx) {
1365+ inContext(localCtx(tree) ) {
13661366 cpy.LambdaTypeTree (tree)(transformSub(tparams), transform(body))
13671367 }
13681368 case TermLambdaTypeTree (params, body) =>
1369- inContext(localCtx) {
1369+ inContext(localCtx(tree) ) {
13701370 cpy.TermLambdaTypeTree (tree)(transformSub(params), transform(body))
13711371 }
13721372 case MatchTypeTree (bound, selector, cases) =>
@@ -1384,17 +1384,17 @@ object Trees {
13841384 case EmptyValDef =>
13851385 tree
13861386 case tree @ ValDef (name, tpt, _) =>
1387- inContext(localCtx) {
1387+ inContext(localCtx(tree) ) {
13881388 val tpt1 = transform(tpt)
13891389 val rhs1 = transform(tree.rhs)
13901390 cpy.ValDef (tree)(name, tpt1, rhs1)
13911391 }
13921392 case tree @ DefDef (name, paramss, tpt, _) =>
1393- inContext(localCtx) {
1393+ inContext(localCtx(tree) ) {
13941394 cpy.DefDef (tree)(name, transformParamss(paramss), transform(tpt), transform(tree.rhs))
13951395 }
13961396 case tree @ TypeDef (name, rhs) =>
1397- inContext(localCtx) {
1397+ inContext(localCtx(tree) ) {
13981398 cpy.TypeDef (tree)(name, transform(rhs))
13991399 }
14001400 case tree @ Template (constr, parents, self, _) if tree.derived.isEmpty =>
@@ -1404,7 +1404,10 @@ object Trees {
14041404 case Export (expr, selectors) =>
14051405 cpy.Export (tree)(transform(expr), selectors)
14061406 case PackageDef (pid, stats) =>
1407- cpy.PackageDef (tree)(transformSub(pid), transformStats(stats, pid.symbol.moduleClass)(using localCtx))
1407+ val pid1 = transformSub(pid)
1408+ inContext(localCtx(tree)) {
1409+ cpy.PackageDef (tree)(pid1, transformStats(stats, ctx.owner))
1410+ }
14081411 case Annotated (arg, annot) =>
14091412 cpy.Annotated (tree)(transform(arg), transform(annot))
14101413 case Thicket (trees) =>
@@ -1450,8 +1453,6 @@ object Trees {
14501453 foldOver(x, tree)(using ctx.withSource(tree.source))
14511454 else {
14521455 Stats .record(s " TreeAccumulator.foldOver/ $getClass" )
1453- def localCtx =
1454- if (tree.hasType && tree.symbol.exists) ctx.withOwner(tree.symbol) else ctx
14551456 tree match {
14561457 case Ident (name) =>
14571458 x
@@ -1506,11 +1507,11 @@ object Trees {
15061507 case AppliedTypeTree (tpt, args) =>
15071508 this (this (x, tpt), args)
15081509 case LambdaTypeTree (tparams, body) =>
1509- inContext(localCtx) {
1510+ inContext(localCtx(tree) ) {
15101511 this (this (x, tparams), body)
15111512 }
15121513 case TermLambdaTypeTree (params, body) =>
1513- inContext(localCtx) {
1514+ inContext(localCtx(tree) ) {
15141515 this (this (x, params), body)
15151516 }
15161517 case MatchTypeTree (bound, selector, cases) =>
@@ -1526,15 +1527,15 @@ object Trees {
15261527 case UnApply (fun, implicits, patterns) =>
15271528 this (this (this (x, fun), implicits), patterns)
15281529 case tree @ ValDef (_, tpt, _) =>
1529- inContext(localCtx) {
1530+ inContext(localCtx(tree) ) {
15301531 this (this (x, tpt), tree.rhs)
15311532 }
15321533 case tree @ DefDef (_, paramss, tpt, _) =>
1533- inContext(localCtx) {
1534+ inContext(localCtx(tree) ) {
15341535 this (this (paramss.foldLeft(x)(apply), tpt), tree.rhs)
15351536 }
15361537 case TypeDef (_, rhs) =>
1537- inContext(localCtx) {
1538+ inContext(localCtx(tree) ) {
15381539 this (x, rhs)
15391540 }
15401541 case tree @ Template (constr, parents, self, _) if tree.derived.isEmpty =>
@@ -1544,7 +1545,7 @@ object Trees {
15441545 case Export (expr, _) =>
15451546 this (x, expr)
15461547 case PackageDef (pid, stats) =>
1547- this (this (x, pid), stats)(using localCtx)
1548+ this (this (x, pid), stats)(using localCtx(tree) )
15481549 case Annotated (arg, annot) =>
15491550 this (this (x, arg), annot)
15501551 case Thicket (ts) =>
0 commit comments