Skip to content

Commit 8aec15b

Browse files
committed
Refactor CannotBeAccessed/isAccessibleFrom
1 parent 231ca72 commit 8aec15b

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ object SymDenotations {
877877
* As a side effect, drop Local flags of members that are not accessed via the ThisType
878878
* of their owner.
879879
*/
880-
final def isAccessibleFrom(pre: Type, superAccess: Boolean = false, whyNot: StringBuffer | Null = null)(using Context): Boolean = {
880+
final def isAccessibleFrom(pre: Type, superAccess: Boolean = false)(using Context): Boolean = {
881881

882882
/** Are we inside definition of `boundary`?
883883
* If this symbol is Java defined, package structure is interpreted to be flat.
@@ -906,26 +906,13 @@ object SymDenotations {
906906

907907
/** Is protected access to target symbol permitted? */
908908
def isProtectedAccessOK: Boolean =
909-
inline def fail(str: String): false =
910-
if whyNot != null then whyNot.nn.append(str)
911-
false
912909
val cls = owner.enclosingSubClass
913910
if !cls.exists then
914-
if pre.termSymbol.isPackageObject && accessWithin(pre.termSymbol.owner) then
915-
true
916-
else
917-
val encl = if ctx.owner.isConstructor then ctx.owner.enclosingClass.owner.enclosingClass else ctx.owner.enclosingClass
918-
val location = if owner.is(Final) then owner.showLocated else owner.showLocated + " or one of its subclasses"
919-
fail(i"""
920-
| Protected $this can only be accessed from $location.""")
921-
else if isType || pre.derivesFrom(cls) || isConstructor || owner.is(ModuleClass) then
911+
pre.termSymbol.isPackageObject && accessWithin(pre.termSymbol.owner)
912+
else
922913
// allow accesses to types from arbitrary subclasses fixes #4737
923914
// don't perform this check for static members
924-
true
925-
else
926-
val location = if cls.is(Final) then cls.showLocated else cls.showLocated + " or one of its subclasses"
927-
fail(i"""
928-
| Protected $this can only be accessed from $location.""")
915+
isType || pre.derivesFrom(cls) || isConstructor || owner.is(ModuleClass)
929916
end isProtectedAccessOK
930917

931918
if pre eq NoPrefix then true

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ extends Message(PatternMatchExhaustivityID) {
876876

877877
val pathes = List(
878878
ActionPatch(
879-
srcPos = endPos,
879+
srcPos = endPos,
880880
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
881881
.mkString("\n", "\n", "")
882882
),
@@ -2986,7 +2986,13 @@ extends ReferenceMsg(CannotBeAccessedID):
29862986
i"none of the overloaded alternatives named $name can"
29872987
val where = if (ctx.owner.exists) i" from ${ctx.owner.enclosingClass}" else ""
29882988
val whyNot = new StringBuffer
2989-
alts.foreach(_.isAccessibleFrom(pre, superAccess, whyNot))
2989+
for alt <- alts do
2990+
if alt.is(Protected) then
2991+
val cls = alt.owner.enclosingSubClass
2992+
val owner = if cls.exists then cls else alt.owner
2993+
val location = if owner.is(Final) then owner.showLocated else owner.showLocated + " or one of its subclasses"
2994+
whyNot.append(i"""
2995+
| Protected $alt can only be accessed from $location.""")
29902996
i"$whatCanNot be accessed as a member of $pre$where.$whyNot"
29912997
def explain(using Context) = ""
29922998

tests/neg/i18686.check

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- [E173] Reference Error: tests/neg/i18686.scala:9:16 -----------------------------------------------------------------
2+
9 | println(Foo.Bar1) // error
3+
| ^^^^^^^^
4+
| value Bar1 cannot be accessed as a member of Foo.type from object Main.
5+
-- [E173] Reference Error: tests/neg/i18686.scala:10:16 ----------------------------------------------------------------
6+
10 | println(Foo.Bar2) // error
7+
| ^^^^^^^^
8+
| value Bar2 cannot be accessed as a member of Foo.type from object Main.
9+
-- [E173] Reference Error: tests/neg/i18686.scala:11:16 ----------------------------------------------------------------
10+
11 | println(Foo.Bar3) // error
11+
| ^^^^^^^^
12+
| value Bar3 cannot be accessed as a member of Foo.type from object Main.
13+
| Protected value Bar3 can only be accessed from object Foo.

tests/neg/i18686.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Foo:
2+
private val Bar1: Int = 3
3+
private[Foo] val Bar2: Int = 3
4+
protected val Bar3: Int = 3
5+
end Foo
6+
7+
object Main:
8+
def main(args: Array[String]): Unit =
9+
println(Foo.Bar1) // error
10+
println(Foo.Bar2) // error
11+
println(Foo.Bar3) // error
12+
end main
13+
end Main

0 commit comments

Comments
 (0)