Skip to content

Commit 1472b7a

Browse files
committed
Improve error message for inaccessible protected members
1 parent 1920336 commit 1472b7a

File tree

6 files changed

+19
-28
lines changed

6 files changed

+19
-28
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -915,17 +915,17 @@ object SymDenotations {
915915
true
916916
else
917917
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"
918919
fail(i"""
919-
| Access to protected $this not permitted because enclosing ${encl.showLocated}
920-
| is not a subclass of ${owner.showLocated} where target is defined""")
920+
| Protected $this can only be accessed from $location.""")
921921
else if isType || pre.derivesFrom(cls) || isConstructor || owner.is(ModuleClass) then
922922
// allow accesses to types from arbitrary subclasses fixes #4737
923923
// don't perform this check for static members
924924
true
925925
else
926+
val location = if cls.is(Final) then cls.showLocated else cls.showLocated + " or one of its subclasses"
926927
fail(i"""
927-
| Access to protected ${symbol.show} not permitted because prefix type ${pre.widen.show}
928-
| does not conform to ${cls.showLocated} where the access takes place""")
928+
| Protected $this can only be accessed from $location.""")
929929
end isProtectedAccessOK
930930

931931
if pre eq NoPrefix then true

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
11231123
nameString(sym)
11241124

11251125
if sym.is(ModuleClass) && sym.isTopLevelDefinitionsObject then
1126-
"top-level definition in package " + nameString(sym.owner.name)
1126+
"the top-level definitions in package " + nameString(sym.owner.name)
11271127
else (keywordText(kindString(sym)) ~~ {
11281128
if (sym.isAnonymousClass)
11291129
toTextParents(sym.info.parents) ~~ "{...}"

tests/neg-macros/annot-result-owner.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- Error: tests/neg-macros/annot-result-owner/Test_2.scala:1:0 ---------------------------------------------------------
33
1 |@insertVal // error
44
|^^^^^^^^^^
5-
|macro annotation @insertVal added value definitionWithWrongOwner$macro$1 with an inconsistent owner. Expected it to be owned by top-level definition in package <empty> but was owned by method foo.
5+
|macro annotation @insertVal added value definitionWithWrongOwner$macro$1 with an inconsistent owner. Expected it to be owned by the top-level definitions in package <empty> but was owned by method foo.
66
-- Error: tests/neg-macros/annot-result-owner/Test_2.scala:5:2 ---------------------------------------------------------
77
5 | @insertVal // error
88
| ^^^^^^^^^^

tests/neg/i12573.check

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
|value getDFType is not a member of DFBits[(8 : Int)].
55
|Extension methods were tried, but the search failed with:
66
|
7-
| method getDFType cannot be accessed as a member of DFType.type from top-level definition in package <empty>.
8-
| Access to protected method getDFType not permitted because enclosing top-level definition in package <empty>
9-
| is not a subclass of object DFType where target is defined
7+
| method getDFType cannot be accessed as a member of DFType.type from the top-level definitions in package <empty>.
8+
| Protected method getDFType can only be accessed from object DFType.

tests/neg/i7709.check

+8-16
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,39 @@
22
5 | class B extends X.Y // error
33
| ^^^
44
| class Y cannot be accessed as a member of X.type from class B.
5-
| Access to protected class Y not permitted because enclosing object A
6-
| is not a subclass of object X where target is defined
5+
| Protected class Y can only be accessed from object X.
76
-- [E173] Reference Error: tests/neg/i7709.scala:6:21 ------------------------------------------------------------------
87
6 | class B2 extends X.Y: // error
98
| ^^^
109
| class Y cannot be accessed as a member of X.type from class B2.
11-
| Access to protected class Y not permitted because enclosing object A
12-
| is not a subclass of object X where target is defined
10+
| Protected class Y can only be accessed from object X.
1311
-- [E173] Reference Error: tests/neg/i7709.scala:9:28 ------------------------------------------------------------------
1412
9 | class B4 extends B3(new X.Y) // error
1513
| ^^^
1614
| class Y cannot be accessed as a member of X.type from class B4.
17-
| Access to protected class Y not permitted because enclosing object A
18-
| is not a subclass of object X where target is defined
15+
| Protected class Y can only be accessed from object X.
1916
-- [E173] Reference Error: tests/neg/i7709.scala:11:34 -----------------------------------------------------------------
2017
11 | def this(n: Int) = this(new X.Y().toString) // error
2118
| ^^^
2219
| class Y cannot be accessed as a member of X.type from class B5.
23-
| Access to protected class Y not permitted because enclosing object A
24-
| is not a subclass of object X where target is defined
20+
| Protected class Y can only be accessed from object X.
2521
-- [E173] Reference Error: tests/neg/i7709.scala:13:20 -----------------------------------------------------------------
2622
13 | class B extends X.Y // error
2723
| ^^^
2824
| class Y cannot be accessed as a member of X.type from class B.
29-
| Access to protected class Y not permitted because enclosing trait T
30-
| is not a subclass of object X where target is defined
25+
| Protected class Y can only be accessed from object X.
3126
-- [E173] Reference Error: tests/neg/i7709.scala:18:18 -----------------------------------------------------------------
3227
18 | def y = new xx.Y // error
3328
| ^^^^
3429
| class Y cannot be accessed as a member of XX from class C.
35-
| Access to protected class Y not permitted because enclosing class C
36-
| is not a subclass of class XX where target is defined
30+
| Protected class Y can only be accessed from class XX or one of its subclasses.
3731
-- [E173] Reference Error: tests/neg/i7709.scala:23:20 -----------------------------------------------------------------
3832
23 | def y = new xx.Y // error
3933
| ^^^^
4034
| class Y cannot be accessed as a member of XX from class D.
41-
| Access to protected class Y not permitted because enclosing class D
42-
| is not a subclass of class XX where target is defined
35+
| Protected class Y can only be accessed from class XX or one of its subclasses.
4336
-- [E173] Reference Error: tests/neg/i7709.scala:31:20 -----------------------------------------------------------------
4437
31 | class Q extends X.Y // error
4538
| ^^^
4639
| class Y cannot be accessed as a member of p.X.type from class Q.
47-
| Access to protected class Y not permitted because enclosing package p
48-
| is not a subclass of object X in package p where target is defined
40+
| Protected class Y can only be accessed from object X in package p.

tests/neg/not-accessible.check

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
-- [E173] Reference Error: tests/neg/not-accessible.scala:13:23 --------------------------------------------------------
1010
13 | def test(a: A) = a.x // error
1111
| ^^^
12-
| value x cannot be accessed as a member of (a : foo.A) from top-level definition in package bar.
12+
| value x cannot be accessed as a member of (a : foo.A) from the top-level definitions in package bar.
1313
-- [E173] Reference Error: tests/neg/not-accessible.scala:5:21 ---------------------------------------------------------
1414
5 | def test(a: A) = a.x // error
1515
| ^^^
16-
| value x cannot be accessed as a member of (a : foo.A) from top-level definition in package foo.
16+
| value x cannot be accessed as a member of (a : foo.A) from the top-level definitions in package foo.
1717
-- [E173] Reference Error: tests/neg/not-accessible.scala:15:23 --------------------------------------------------------
1818
15 |def test(a: foo.A) = a.x // error
1919
| ^^^
20-
| value x cannot be accessed as a member of (a : foo.A) from top-level definition in package <empty>.
20+
| value x cannot be accessed as a member of (a : foo.A) from the top-level definitions in package <empty>.

0 commit comments

Comments
 (0)