@@ -55,18 +55,36 @@ class SuperAccessors extends MacroTransform
55
55
/** the following two members override abstract members in Transform */
56
56
override def phaseName : String = " superaccessors"
57
57
58
+ override def transformPhase (implicit ctx : Context ) = thisTransformer.next
59
+
58
60
protected def newTransformer (implicit ctx : Context ): Transformer =
59
61
new SuperAccTransformer
60
62
61
63
class SuperAccTransformer extends Transformer {
62
64
63
- /** validCurrentOwner arrives undocumented, but I reverse engineer it to be
64
- * a flag for needsProtectedAccessor which is false while transforming either
65
- * a by-name argument block or a closure. This excludes them from being
66
- * considered able to access protected members via subclassing (why?) which in turn
67
- * increases the frequency with which needsProtectedAccessor will be true.
65
+ /** Some parts of trees will get a new owner in subsequent phases.
66
+ * These are value class methods, which will become extension methods.
67
+ * (By-name arguments used to be included also, but these
68
+ * don't get a new class anymore, they are just wrapped in a new method).
69
+ *
70
+ * These regions will have to be treated specially for the purpose
71
+ * of adding accessors. For instance, super calls from these regions
72
+ * always have to go through an accessor.
73
+ *
74
+ * The `invalidOwner` field, if different from NoSymbol,
75
+ * contains the symbol that is not a valid owner.
68
76
*/
69
- private var validCurrentOwner = true
77
+ private var invalidEnclClass : Symbol = NoSymbol
78
+
79
+ private def withInvalidCurrentClass [A ](trans : => A )(implicit ctx : Context ): A = {
80
+ val saved = invalidEnclClass
81
+ invalidEnclClass = ctx.owner
82
+ try trans
83
+ finally invalidEnclClass = saved
84
+ }
85
+
86
+ private def validCurrentClass (implicit ctx : Context ): Boolean =
87
+ ctx.owner.enclosingClass != invalidEnclClass
70
88
71
89
private val accDefs = mutable.Map [Symbol , ListBuffer [Tree ]]()
72
90
@@ -92,15 +110,7 @@ class SuperAccessors extends MacroTransform
92
110
This (clazz).select(superAcc).withPos(sel.pos)
93
111
}
94
112
95
- private def transformArgs (formals : List [Type ], args : List [Tree ])(implicit ctx : Context ) =
96
- args.zipWithConserve(formals) {(arg, formal) =>
97
- formal match {
98
- case _ : ExprType => withInvalidOwner(transform(arg))
99
- case _ => transform(arg)
100
- }
101
- }
102
-
103
- private def transformSuperSelect (sel : Select )(implicit ctx : Context ): Tree = {
113
+ private def transformSuperSelect (sel : Select )(implicit ctx : Context ): Tree = {
104
114
val Select (sup @ Super (_, mix), name) = sel
105
115
val sym = sel.symbol
106
116
assert(sup.symbol.exists, s " missing symbol in $sel: ${sup.tpe}" )
@@ -126,7 +136,7 @@ class SuperAccessors extends MacroTransform
126
136
127
137
}
128
138
if (name.isTermName && mix == tpnme.EMPTY &&
129
- ((clazz is Trait ) || clazz != ctx.owner.enclosingClass || ! validCurrentOwner ))
139
+ ((clazz is Trait ) || clazz != ctx.owner.enclosingClass || ! validCurrentClass ))
130
140
ensureAccessor(sel)(ctx.withPhase(thisTransformer.next))
131
141
else sel
132
142
}
@@ -246,7 +256,7 @@ class SuperAccessors extends MacroTransform
246
256
247
257
case tree : DefDef =>
248
258
cpy.DefDef (tree)(
249
- rhs = if (isMethodWithExtension(sym)) withInvalidOwner (transform(tree.rhs)) else transform(tree.rhs))
259
+ rhs = if (isMethodWithExtension(sym)) withInvalidCurrentClass (transform(tree.rhs)) else transform(tree.rhs))
250
260
251
261
case TypeApply (sel @ Select (qual, name), args) =>
252
262
mayNeedProtectedAccessor(sel, args, goToSuper = true )
@@ -265,12 +275,6 @@ class SuperAccessors extends MacroTransform
265
275
}
266
276
transformAssign
267
277
268
- case Apply (fn, args) =>
269
- val MethodType (_, formals) = fn.tpe.widen
270
- ctx.atPhase(thisTransformer.next) { implicit ctx =>
271
- cpy.Apply (tree)(transform(fn), transformArgs(formals, args))
272
- }
273
-
274
278
case _ =>
275
279
super .transform(tree)
276
280
}
@@ -284,13 +288,6 @@ class SuperAccessors extends MacroTransform
284
288
}
285
289
}
286
290
287
- private def withInvalidOwner [A ](trans : => A ): A = {
288
- val saved = validCurrentOwner
289
- validCurrentOwner = false
290
- try trans
291
- finally validCurrentOwner = saved
292
- }
293
-
294
291
/** Add a protected accessor, if needed, and return a tree that calls
295
292
* the accessor and returns the same member. The result is already
296
293
* typed.
@@ -382,7 +379,7 @@ class SuperAccessors extends MacroTransform
382
379
val host = hostForAccessorOf(sym, clazz)
383
380
val selfType = host.classInfo.selfType
384
381
def accessibleThroughSubclassing =
385
- validCurrentOwner && (selfType <:< sym.owner.typeRef) && ! clazz.is(Trait )
382
+ validCurrentClass && (selfType <:< sym.owner.typeRef) && ! clazz.is(Trait )
386
383
387
384
val isCandidate = (
388
385
sym.is(Protected )
0 commit comments