Skip to content

Commit 1d05796

Browse files
authored
Merge pull request #2784 from dotty-staging/fix-i2780
Fix #2780: Fix generation of protected accessors
2 parents c792e95 + 4658cd3 commit 1d05796

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
187187
}
188188
val protectedAccessor = clazz.info.decl(accName).suchThat(_.signature == accType.signature).symbol orElse {
189189
val newAcc = ctx.newSymbol(
190-
clazz, accName, Artifact, accType, coord = sel.pos).enteredAfter(thisTransformer)
190+
clazz, accName, Artifact | Method, accType, coord = sel.pos).enteredAfter(thisTransformer)
191191
val code = polyDefDef(newAcc, trefs => vrefss => {
192192
val (receiver :: _) :: tail = vrefss
193193
val base = receiver.select(sym).appliedToTypes(trefs)
194-
(base /: vrefss)(Apply(_, _))
194+
(base /: tail)(Apply(_, _))
195195
})
196196
ctx.debuglog("created protected accessor: " + code)
197197
accDefs(clazz) += code
@@ -300,9 +300,8 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
300300
private def needsProtectedAccessor(sym: Symbol, pos: Position)(implicit ctx: Context): Boolean = {
301301
val clazz = currentClass
302302
val host = hostForAccessorOf(sym, clazz)
303-
val selfType = host.classInfo.selfType
304303
def accessibleThroughSubclassing =
305-
validCurrentClass && (selfType <:< sym.owner.typeRef) && !clazz.is(Trait)
304+
validCurrentClass && (clazz.classInfo.selfType <:< sym.owner.typeRef) && !clazz.is(Trait)
306305

307306
val isCandidate = (
308307
sym.is(Protected)
@@ -312,9 +311,11 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
312311
&& (sym.enclosingPackageClass != currentClass.enclosingPackageClass)
313312
&& (sym.enclosingPackageClass == sym.accessBoundary(sym.enclosingPackageClass))
314313
)
315-
def isSelfType = !(host.typeRef <:< selfType) && {
316-
if (selfType.typeSymbol.is(JavaDefined))
317-
ctx.restrictionError(s"cannot accesses protected $sym from within $clazz with self type $selfType", pos)
314+
val hostSelfType = host.classInfo.selfType
315+
def isSelfType = !(host.typeRef <:< hostSelfType) && {
316+
if (hostSelfType.typeSymbol.is(JavaDefined))
317+
ctx.restrictionError(
318+
s"cannot accesses protected $sym from within $clazz with host self type $hostSelfType", pos)
318319
true
319320
}
320321
def isJavaProtected = host.is(Trait) && sym.is(JavaDefined) && {

tests/run/i2780/Base.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package bla;
2+
3+
public class Base {
4+
protected String foo() { return ""; }
5+
protected String bar = "";
6+
}

tests/run/i2780/Test.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Foo extends bla.Base {
2+
class Inner {
3+
println(foo())
4+
println(bar)
5+
}
6+
}
7+
8+
object Test {
9+
def main(args: Array[String]): Unit = {
10+
val f = new Foo
11+
new f.Inner
12+
}
13+
}

0 commit comments

Comments
 (0)