Skip to content

Commit 056a233

Browse files
committed
Context for constructor error is outer context
1 parent 8a9e0e9 commit 056a233

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -824,16 +824,16 @@ object SymDenotations {
824824
}
825825

826826
/** Is protected access to target symbol permitted? */
827-
def isProtectedAccessOK = {
828-
def fail(str: => String): Boolean = {
829-
if (whyNot != null) whyNot append str
827+
def isProtectedAccessOK: Boolean =
828+
def fail(str: => String): false =
829+
if whyNot != null then whyNot.append(str)
830830
false
831-
}
832831
val cls = owner.enclosingSubClass
833-
if (!cls.exists)
832+
if !cls.exists then
833+
val encl = if !ctx.owner.isConstructor then ctx else ctx.outersIterator.dropWhile(_.owner.isConstructor).next()
834834
fail(
835835
i"""
836-
| Access to protected $this not permitted because enclosing ${ctx.owner.enclosingClass.showLocated}
836+
| Access to protected $this not permitted because enclosing ${encl.owner.enclosingClass.showLocated}
837837
| is not a subclass of ${owner.showLocated} where target is defined""")
838838
else if
839839
!( isType // allow accesses to types from arbitrary subclasses fixes #4737
@@ -847,7 +847,7 @@ object SymDenotations {
847847
| Access to protected ${symbol.show} not permitted because prefix type ${pre.widen.show}
848848
| does not conform to ${cls.showLocated} where the access takes place""")
849849
else true
850-
}
850+
end isProtectedAccessOK
851851

852852
if pre eq NoPrefix then true
853853
else if isAbsent() then false

tests/neg/i7709.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg/i7709.scala:5:20 -----------------------------------------------------------------------------------
2+
5 | class B extends X.Y // error
3+
| ^^^
4+
| class Y in object X 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
7+
-- Error: tests/neg/i7709.scala:10:18 ----------------------------------------------------------------------------------
8+
10 | def y = new xx.Y // error
9+
| ^^^^
10+
| class Y cannot be accessed as a member of XX from class C.
11+
| Access to protected class Y not permitted because enclosing class C
12+
| is not a subclass of class XX where target is defined

tests/neg/i7709.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
object X:
3+
protected class Y
4+
object A:
5+
class B extends X.Y // error
6+
class XX:
7+
protected class Y
8+
class C:
9+
def xx = new XX
10+
def y = new xx.Y // error
11+
class YY extends XX:
12+
def y = new Y

0 commit comments

Comments
 (0)