Skip to content

Fix #8005: Check private[C] members for accessibility when overriding #8006

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 2 commits into from
Jan 17, 2020
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
17 changes: 9 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ object RefChecks {
def overrideAccessError() = {
ctx.log(i"member: ${member.showLocated} ${member.flagsString}") // DEBUG
ctx.log(i"other: ${other.showLocated} ${other.flagsString}") // DEBUG
val otherAccess = (other.flags & AccessFlags).toString
val otherAccess = (other.flags & AccessFlags).flagsString
overrideError("has weaker access privileges; it should be " +
(if (otherAccess == "") "public" else "at least " + otherAccess))
}
Expand Down Expand Up @@ -325,13 +325,14 @@ object RefChecks {
// todo: align accessibility implication checking with isAccessible in Contexts
val ob = other.accessBoundary(member.owner)
val mb = member.accessBoundary(member.owner)
def isOverrideAccessOK = (
(member.flags & AccessFlags).isEmpty // 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)
)
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)
if (!isOverrideAccessOK)
overrideAccessError()
else if (other.isClass)
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i8005.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package nio
abstract class Buffer {
val isReadOnly: Boolean
def foo(): Unit
def bar(): Unit
}

class ByteBuffer extends Buffer { // error: ByteBufer needs to be abstract since `bar` is not defined
private[nio] val isReadOnly: Boolean = false // error
protected def foo(): Unit = () // error
private def bar(): Unit = ()
}