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 26, 2021
1 parent c94b333 commit 536778b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
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
29 changes: 29 additions & 0 deletions tests/pos/t12494.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

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
// restriction in Scala 3 under -from-tasty
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 536778b

Please sign in to comment.