Skip to content

Fix #2780: Fix generation of protected accessors #2784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
}
val protectedAccessor = clazz.info.decl(accName).suchThat(_.signature == accType.signature).symbol orElse {
val newAcc = ctx.newSymbol(
clazz, accName, Artifact, accType, coord = sel.pos).enteredAfter(thisTransformer)
clazz, accName, Artifact | Method, accType, coord = sel.pos).enteredAfter(thisTransformer)
val code = polyDefDef(newAcc, trefs => vrefss => {
val (receiver :: _) :: tail = vrefss
val base = receiver.select(sym).appliedToTypes(trefs)
(base /: vrefss)(Apply(_, _))
(base /: tail)(Apply(_, _))
})
ctx.debuglog("created protected accessor: " + code)
accDefs(clazz) += code
Expand Down Expand Up @@ -300,9 +300,8 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
private def needsProtectedAccessor(sym: Symbol, pos: Position)(implicit ctx: Context): Boolean = {
val clazz = currentClass
val host = hostForAccessorOf(sym, clazz)
val selfType = host.classInfo.selfType
def accessibleThroughSubclassing =
validCurrentClass && (selfType <:< sym.owner.typeRef) && !clazz.is(Trait)
validCurrentClass && (clazz.classInfo.selfType <:< sym.owner.typeRef) && !clazz.is(Trait)

val isCandidate = (
sym.is(Protected)
Expand All @@ -312,9 +311,11 @@ class SuperAccessors(thisTransformer: DenotTransformer) {
&& (sym.enclosingPackageClass != currentClass.enclosingPackageClass)
&& (sym.enclosingPackageClass == sym.accessBoundary(sym.enclosingPackageClass))
)
def isSelfType = !(host.typeRef <:< selfType) && {
if (selfType.typeSymbol.is(JavaDefined))
ctx.restrictionError(s"cannot accesses protected $sym from within $clazz with self type $selfType", pos)
val hostSelfType = host.classInfo.selfType
def isSelfType = !(host.typeRef <:< hostSelfType) && {
if (hostSelfType.typeSymbol.is(JavaDefined))
ctx.restrictionError(
s"cannot accesses protected $sym from within $clazz with host self type $hostSelfType", pos)
true
}
def isJavaProtected = host.is(Trait) && sym.is(JavaDefined) && {
Expand Down
6 changes: 6 additions & 0 deletions tests/run/i2780/Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package bla;

public class Base {
protected String foo() { return ""; }
protected String bar = "";
}
13 changes: 13 additions & 0 deletions tests/run/i2780/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Foo extends bla.Base {
class Inner {
println(foo())
println(bar)
}
}

object Test {
def main(args: Array[String]): Unit = {
val f = new Foo
new f.Inner
}
}