Skip to content

Commit 7cb4d57

Browse files
committed
Make toplevel protected qualified protected
For private toplevel definitions we expand `private` to `private[p]` where `p` is the enclosing package. This PR applies the same scheme to protected toplevel definitions. Rationale: #18057 fixed an issue where toplevel protected members were always accessible because explicit package object prefixes were added after the accessibility check was done, and would re-establish the previous members without doing an accessibility check. The fix was done by adding package objects first, then doing he rest of the checks. But that also means that protected toplevel objects now get checked as members of their synthetic package object instead of as members of their package. To avoid that we make the package explicit as qalifier. Thsi shouls also make specs2 compile again.
1 parent 0a21ecf commit 7cb4d57

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

community-build/test/scala/dotty/communitybuild/CommunityBuildTest.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ class CommunityBuildTestC:
9393
@Test def sconfig = projects.sconfig.run()
9494
@Test def shapeless = projects.shapeless.run()
9595
@Test def sourcecode = projects.sourcecode.run()
96-
97-
// Disabled. Currently fails in FutureMatchers.scala. The call to
98-
// `checkResultFailure` goes to a protected method which is not accessible.
99-
// I tried to fix it, but get test failures.
100-
// @Test def specs2 = projects.specs2.run()
96+
@Test def specs2 = projects.specs2.run()
10197

10298
@Test def stdLib213 = projects.stdLib213.run()
10399
@Test def ujson = projects.ujson.run()

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ object Flags {
600600
val TermParamOrAccessor: FlagSet = Param | ParamAccessor
601601
val PrivateParamAccessor: FlagSet = ParamAccessor | Private
602602
val PrivateOrArtifact: FlagSet = Private | Artifact
603+
val PrivateOrProtected: FlagSet = Private | Protected
603604
val ClassTypeParam: FlagSet = Private | TypeParam
604605
val Scala2Trait: FlagSet = Scala2x | Trait
605606
val SyntheticArtifact: FlagSet = Synthetic | Artifact

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,10 @@ class Namer { typer: Typer =>
223223
var flags1 = flags
224224
var privateWithin = privateWithinClass(tree.mods)
225225
val effectiveOwner = owner.skipWeakOwner
226-
if (flags.is(Private) && effectiveOwner.is(Package)) {
227-
// If effective owner is a package p, widen private to private[p]
228-
flags1 = flags1 &~ PrivateLocal
226+
if (flags.isOneOf(PrivateOrProtected) && !privateWithin.exists) && effectiveOwner.is(Package) then
227+
// If effective owner is a package p, widen private to private[p] and protected to protected[p]
228+
flags1 = flags1 &~ PrivateLocal // drop private but keep protected
229229
privateWithin = effectiveOwner
230-
}
231230

232231
val sym =
233232
if (prev.exists) {

tests/pos/i18124/definition.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// definition.scala
2+
package oolong.bson:
3+
4+
trait BsonValue
5+
protected def merge(
6+
base: BsonValue,
7+
patch: BsonValue,
8+
arraySubvalues: Boolean = false
9+
): BsonValue = ???
10+
11+
private def foo: Int = 1
12+
13+
package inner:
14+
protected[bson] def bar = 2
15+

tests/pos/i18124/usage.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// usage.scala
2+
package oolong.bson
3+
4+
extension (bv: BsonValue)
5+
def :+(other: BsonValue): BsonValue = merge(other, bv, false)
6+
7+
val x = foo
8+
val y = inner.bar

0 commit comments

Comments
 (0)