Skip to content

Commit 85751aa

Browse files
Merge pull request scala#8006 from dotty-staging/fix-#8005
Fix scala#8005: Check private[C] members for accessibility when overriding
2 parents 5dc00b1 + ff1a9f7 commit 85751aa

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ object RefChecks {
260260
def overrideAccessError() = {
261261
ctx.log(i"member: ${member.showLocated} ${member.flagsString}") // DEBUG
262262
ctx.log(i"other: ${other.showLocated} ${other.flagsString}") // DEBUG
263-
val otherAccess = (other.flags & AccessFlags).toString
263+
val otherAccess = (other.flags & AccessFlags).flagsString
264264
overrideError("has weaker access privileges; it should be " +
265265
(if (otherAccess == "") "public" else "at least " + otherAccess))
266266
}
@@ -325,13 +325,14 @@ object RefChecks {
325325
// todo: align accessibility implication checking with isAccessible in Contexts
326326
val ob = other.accessBoundary(member.owner)
327327
val mb = member.accessBoundary(member.owner)
328-
def isOverrideAccessOK = (
329-
(member.flags & AccessFlags).isEmpty // member is public
330-
|| // - or -
331-
(!other.is(Protected) || member.is(Protected)) && // if o is protected, so is m, and
332-
(ob.isContainedIn(mb) || other.isAllOf(JavaProtected)) // m relaxes o's access boundary,
333-
// or o is Java defined and protected (see #3946)
334-
)
328+
def isOverrideAccessOK =
329+
(member.flags & AccessFlags).isEmpty
330+
&& !member.privateWithin.exists // member is public, or
331+
|| (!other.is(Protected) || member.is(Protected))
332+
// if o is protected, so is m, and
333+
&& (ob.isContainedIn(mb) || other.isAllOf(JavaProtected))
334+
// m relaxes o's access boundary,
335+
// or o is Java defined and protected (see #3946)
335336
if (!isOverrideAccessOK)
336337
overrideAccessError()
337338
else if (other.isClass)

tests/neg/i8005.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package nio
2+
abstract class Buffer {
3+
val isReadOnly: Boolean
4+
def foo(): Unit
5+
def bar(): Unit
6+
}
7+
8+
class ByteBuffer extends Buffer { // error: ByteBufer needs to be abstract since `bar` is not defined
9+
private[nio] val isReadOnly: Boolean = false // error
10+
protected def foo(): Unit = () // error
11+
private def bar(): Unit = ()
12+
}

0 commit comments

Comments
 (0)