Skip to content

Commit 731c55a

Browse files
committed
Revert #3735: Allow accesses to private package members from nested
Test case is i3339. In ``` package outer { private class A() { override def toString = "A" } package inner { object Test { def main(args: Array[String]): Unit = { println(new A()) // allow access? } } } } ``` should the access to private class `A` from a nested package be allowed? The usual rules for Scala say yes, since the access is from a nested scope. We changed this to in #3735 to "no", since we wanted to emulate Java's default package-private visibility. But this is unsystematic for two reasons: - it breaks the universal meaning of `private` in Scala - it uses a different name (i.e. private) for what is not named in Java at all, and is named `internal` in Kotlin. - it does not generalize to members of classes which could also profit from a Java-package-private visibility specifier. I believe it is better to leave `private` as it is, and, if we want to emulate `internal` (which might be a good idea), introduce a new modifier for it.
1 parent 5e8272b commit 731c55a

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,7 @@ object SymDenotations {
746746
( !(this is Local)
747747
|| (owner is ImplClass) // allow private local accesses to impl class members
748748
|| isCorrectThisType(pre)
749-
) &&
750-
(!(this.is(Private) && owner.is(Package)) ||
751-
owner == ctx.owner.enclosingPackageClass)
749+
)
752750
|| (this is Protected) &&
753751
( superAccess
754752
|| pre.isInstanceOf[ThisType]

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,27 +204,17 @@ trait TypeAssigner {
204204
var packageAccess = false
205205
val what = alts match {
206206
case Nil =>
207-
i"$name cannot be accessed as a member of $pre"
207+
name.toString
208208
case sym :: Nil =>
209-
if (sym.owner.is(Package)) {
210-
packageAccess = true
211-
i"${sym.showLocated} cannot be accessed"
212-
}
213-
else {
214-
val symStr = if (sym.owner == pre.typeSymbol) sym.show else sym.showLocated
215-
i"$symStr cannot be accessed as a member of $pre"
216-
}
209+
if (sym.owner == pre.typeSymbol) sym.show else sym.showLocated
217210
case _ =>
218-
em"none of the overloaded alternatives named $name can be accessed as members of $pre"
211+
em"none of the overloaded alternatives named $name"
219212
}
220-
val where =
221-
if (!ctx.owner.exists) ""
222-
else if (packageAccess) i" from nested ${ctx.owner.enclosingPackageClass}"
223-
else i" from ${ctx.owner.enclosingClass}"
213+
val where = if (ctx.owner.exists) s" from ${ctx.owner.enclosingClass}" else ""
224214
val whyNot = new StringBuffer
225215
alts foreach (_.isAccessibleFrom(pre, superAccess, whyNot))
226216
if (tpe.isError) tpe
227-
else errorType(ex"$what$where.$whyNot", pos)
217+
else errorType(ex"$what cannot be accessed as a member of $pre$where.$whyNot", pos)
228218
}
229219
}
230220
else ctx.makePackageObjPrefixExplicit(tpe withDenot d)

tests/neg/i3339.scala

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/run/i3339.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A

tests/run/i3339.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package outer {
2+
private class A() {
3+
override def toString = "A"
4+
}
5+
6+
package inner {
7+
object Test {
8+
def main(args: Array[String]): Unit = {
9+
println(new A()) // now ok
10+
}
11+
}
12+
}
13+
}
14+
15+
object Test extends App {
16+
outer.inner.Test.main(Array())
17+
}

0 commit comments

Comments
 (0)