Skip to content

Commit

Permalink
Allow override protected[C] in companion
Browse files Browse the repository at this point in the history
When extending C in its companion, allow
override protected[C] where C denotes the
enclosing companion module of C.
  • Loading branch information
som-snytt committed Dec 13, 2021
1 parent dcb123d commit b1196f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Expand Up @@ -414,13 +414,13 @@ object RefChecks {
val ob = other.accessBoundary(member.owner)
val mb = member.accessBoundary(member.owner)
def isOverrideAccessOK =
(member.flags & AccessFlags).isEmpty
&& !member.privateWithin.exists // member is public, or
|| (!other.is(Protected) || member.is(Protected))
// if o is protected, so is m, and
&& (ob.isContainedIn(mb) || other.isAllOf(JavaProtected))
// m relaxes o's access boundary,
// or o is Java defined and protected (see #3946)
val memberIsPublic = (member.flags & AccessFlags).isEmpty && !member.privateWithin.exists
def protectedOK = !other.is(Protected) || member.is(Protected) // if o is protected, so is m
def companionBoundaryOK = ob.isClass && mb.is(Module) && (ob.companionModule eq mb.companionModule)
def accessBoundaryOK = ob.isContainedIn(mb) || companionBoundaryOK // m relaxes o's access boundary,
def otherIsJavaProtected = other.isAllOf(JavaProtected) // or o is Java defined and protected (see #3946)
memberIsPublic || protectedOK && (accessBoundaryOK || otherIsJavaProtected)
end isOverrideAccessOK
if !member.hasTargetName(other.targetName) then
overrideTargetNameError()
else if (!isOverrideAccessOK)
Expand Down
26 changes: 26 additions & 0 deletions tests/pos/t12494.scala
@@ -0,0 +1,26 @@

trait Base {
protected[Base] def f: Int
}
object Base {
class Child extends Base {
protected[Base] def f: Int = 42 // ok
def test = f
}
}

object X {
// restriction in Scala 2 for local companions
def m: Int = {
trait C {
protected[C] def f: Int
}
object C {
class C2 extends C {
protected[C] def f: Int = 42 // ok
def test = f
}
}
C.C2().test
}
}

0 comments on commit b1196f3

Please sign in to comment.