File tree Expand file tree Collapse file tree 3 files changed +30
-2
lines changed
compiler/src/dotty/tools/dotc/transform
tests/neg/java-trait-access Expand file tree Collapse file tree 3 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import core.Annotations.BodyAnnotation
2020import typer .{NoChecking , LiftErased }
2121import typer .Inliner
2222import typer .ProtoTypes ._
23+ import typer .ErrorReporting .errorTree
2324import core .TypeErasure ._
2425import core .Decorators ._
2526import dotty .tools .dotc .ast .{tpd , untpd }
@@ -700,8 +701,17 @@ object Erasure {
700701 else
701702 val castTarget = // Avoid inaccessible cast targets, see i8661
702703 if sym.owner.isAccessibleFrom(qual1.tpe)(using preErasureCtx)
703- then sym.owner.typeRef
704- else erasure(tree.qualifier.typeOpt.widen)
704+ then
705+ sym.owner.typeRef
706+ else
707+ // If the owner is inaccessible, try going through the qualifier,
708+ // but be careful to not go in an infinite loop in case that doesn't
709+ // work either.
710+ val tp = erasure(tree.qualifier.typeOpt.widen)
711+ if tp =:= qual1.tpe.widen then
712+ return errorTree(qual1,
713+ ex " Unable to emit reference to ${sym.showLocated}, ${sym.owner} is not accessible in ${ctx.owner.enclosingClass}" )
714+ tp
705715 recur(cast(qual1, castTarget))
706716 }
707717 }
Original file line number Diff line number Diff line change 1+ package pkg ;
2+
3+ class A {
4+ public void foo () {}
5+ }
Original file line number Diff line number Diff line change 1+ package pkg {
2+ trait B extends A
3+ class C extends B
4+ }
5+
6+ object Test {
7+ def test1 : Unit = {
8+ val c = new pkg.C
9+ c.foo() // OK
10+ val b : pkg.B = c
11+ b.foo() // error: Unable to emit reference to method foo in class A, class A is not accessible in object Test
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments