@@ -47,7 +47,8 @@ abstract class AccessProxies {
4747 if (passReceiverAsArg(accessor.name))
4848 (argss.head.head.select(accessed), tps.takeRight(numTypeParams), argss.tail)
4949 else
50- (ref(TermRef (cls.thisType, accessed)), tps, argss)
50+ (if (accessed.isStatic) ref(accessed) else ref(TermRef (cls.thisType, accessed)),
51+ tps, argss)
5152 val rhs =
5253 if (accessor.name.isSetterName &&
5354 forwardedArgss.nonEmpty && forwardedArgss.head.nonEmpty) // defensive conditions
@@ -70,6 +71,11 @@ abstract class AccessProxies {
7071 def accessorNameKind : ClassifiedNameKind
7172 def needsAccessor (sym : Symbol )(implicit ctx : Context ): Boolean
7273
74+ def ifNoHost (reference : RefTree )(implicit ctx : Context ): Tree = {
75+ assert(false , " no host found for $reference with ${reference.symbol.showLocated} from ${ctx.owner}" )
76+ reference
77+ }
78+
7379 /** A fresh accessor symbol */
7480 private def newAccessorSymbol (owner : Symbol , name : TermName , info : Type , pos : Position )(implicit ctx : Context ): TermSymbol = {
7581 val sym = ctx.newSymbol(owner, name, Synthetic | Method , info, coord = pos).entered
@@ -125,18 +131,14 @@ abstract class AccessProxies {
125131 def useAccessor (reference : RefTree )(implicit ctx : Context ): Tree = {
126132 val accessed = reference.symbol.asTerm
127133 var accessorClass = hostForAccessorOf(accessed : Symbol )
128- if (! accessorClass.exists) {
129- val curCls = ctx.owner.enclosingClass
130- transforms.println( i " ${curCls.ownersIterator.toList} %, % " )
131- ctx.error( i " illegal access to protected ${ accessed.showLocated} from $curCls " ,
132- reference.pos )
133- accessorClass = curCls
134+ if (accessorClass.exists) {
135+ val accessorName = accessorNameKind(accessed.name)
136+ val accessorInfo =
137+ accessed.info.ensureMethodic.asSeenFrom(accessorClass.thisType, accessed.owner)
138+ val accessor = accessorSymbol(accessorClass, accessorName, accessorInfo, accessed )
139+ rewire(reference, accessor)
134140 }
135- val accessorName = accessorNameKind(accessed.name)
136- val accessorInfo =
137- accessed.info.ensureMethodic.asSeenFrom(accessorClass.thisType, accessed.owner)
138- val accessor = accessorSymbol(accessorClass, accessorName, accessorInfo, accessed)
139- rewire(reference, accessor)
141+ else ifNoHost(reference)
140142 }
141143
142144 /** Replace tree with a reference to an accessor if needed */
@@ -156,6 +158,12 @@ object AccessProxies {
156158 /** Where an accessor for the `accessed` symbol should be placed.
157159 * This is the closest enclosing class that has `accessed` as a member.
158160 */
159- def hostForAccessorOf (accessed : Symbol )(implicit ctx : Context ): Symbol =
160- ctx.owner.ownersIterator.findSymbol(_.derivesFrom(accessed.owner))
161+ def hostForAccessorOf (accessed : Symbol )(implicit ctx : Context ): Symbol = {
162+ def recur (cls : Symbol ): Symbol =
163+ if (! cls.exists) NoSymbol
164+ else if (cls.derivesFrom(accessed.owner)) cls
165+ else if (cls.companionModule.moduleClass == accessed.owner) accessed.owner
166+ else recur(cls.owner)
167+ recur(ctx.owner)
168+ }
161169}
0 commit comments