diff --git a/compiler/src/dotty/tools/dotc/transform/Erasure.scala b/compiler/src/dotty/tools/dotc/transform/Erasure.scala index b824dd839554..c66b4159b04f 100644 --- a/compiler/src/dotty/tools/dotc/transform/Erasure.scala +++ b/compiler/src/dotty/tools/dotc/transform/Erasure.scala @@ -639,28 +639,21 @@ object Erasure { val qual1 = typed(tree.qualifier, AnySelectionProto) - def mapOwner(sym: Symbol): Symbol = { - // PolyFunction apply Selects will not have a symbol, so deduce the owner - // from the typed qual. - def polyOwner: Symbol = - if (sym.exists || tree.name != nme.apply) NoSymbol - else { - val owner = qual1.tpe.widen.typeSymbol - if (defn.isFunctionClass(owner)) owner else NoSymbol - } - - polyOwner orElse { + def mapOwner(sym: Symbol): Symbol = + if !sym.exists && tree.name == nme.apply then + // PolyFunction apply Selects will not have a symbol, so deduce the owner + // from the typed qual. + val owner = qual1.tpe.widen.typeSymbol + if defn.isFunctionClass(owner) then owner else NoSymbol + else val owner = sym.maybeOwner - if (defn.specialErasure.contains(owner)) { + if defn.specialErasure.contains(owner) then assert(sym.isConstructor, s"${sym.showLocated}") defn.specialErasure(owner) - } - else if (defn.isSyntheticFunctionClass(owner)) + else if defn.isSyntheticFunctionClass(owner) defn.erasedFunctionClass(owner) else owner - } - } val origSym = tree.symbol @@ -710,7 +703,11 @@ object Erasure { if (qual1.tpe.derivesFrom(sym.owner) || qual1.isInstanceOf[Super]) select(qual1, sym) else - recur(cast(qual1, sym.owner.typeRef)) + val castTarget = // Avoid inaccessible cast targets, see i8661 + if sym.owner.isAccessibleFrom(qual1.tpe)(using preErasureCtx) + then sym.owner.typeRef + else erasure(tree.qualifier.typeOpt.widen) + recur(cast(qual1, castTarget)) } } diff --git a/tests/run/i8661.check b/tests/run/i8661.check new file mode 100644 index 000000000000..45b983be36b7 --- /dev/null +++ b/tests/run/i8661.check @@ -0,0 +1 @@ +hi diff --git a/tests/run/i8661/B.java b/tests/run/i8661/B.java new file mode 100644 index 000000000000..f83b1b0f67bb --- /dev/null +++ b/tests/run/i8661/B.java @@ -0,0 +1,4 @@ +package pk; +class B { + public void hi() { System.out.println("hi"); } +} diff --git a/tests/run/i8661/C.java b/tests/run/i8661/C.java new file mode 100644 index 000000000000..88d14102ce3f --- /dev/null +++ b/tests/run/i8661/C.java @@ -0,0 +1,4 @@ +package pk; +public class C extends B { + public C() {} +} diff --git a/tests/run/i8661/Test.scala b/tests/run/i8661/Test.scala new file mode 100644 index 000000000000..eaf9bc1e8aa7 --- /dev/null +++ b/tests/run/i8661/Test.scala @@ -0,0 +1,6 @@ +import pk.C +object Test { + def main(args: Array[String]): Unit = { + identity(new C).hi() + } +}