Skip to content

Commit b0e7e26

Browse files
authored
Merge pull request #8668 from dotty-staging/fix-#8661
Fix #8661: Avoid inaccessible cast targets in erasure
2 parents 895e361 + 82e5d96 commit b0e7e26

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -639,28 +639,21 @@ object Erasure {
639639

640640
val qual1 = typed(tree.qualifier, AnySelectionProto)
641641

642-
def mapOwner(sym: Symbol): Symbol = {
643-
// PolyFunction apply Selects will not have a symbol, so deduce the owner
644-
// from the typed qual.
645-
def polyOwner: Symbol =
646-
if (sym.exists || tree.name != nme.apply) NoSymbol
647-
else {
648-
val owner = qual1.tpe.widen.typeSymbol
649-
if (defn.isFunctionClass(owner)) owner else NoSymbol
650-
}
651-
652-
polyOwner orElse {
642+
def mapOwner(sym: Symbol): Symbol =
643+
if !sym.exists && tree.name == nme.apply then
644+
// PolyFunction apply Selects will not have a symbol, so deduce the owner
645+
// from the typed qual.
646+
val owner = qual1.tpe.widen.typeSymbol
647+
if defn.isFunctionClass(owner) then owner else NoSymbol
648+
else
653649
val owner = sym.maybeOwner
654-
if (defn.specialErasure.contains(owner)) {
650+
if defn.specialErasure.contains(owner) then
655651
assert(sym.isConstructor, s"${sym.showLocated}")
656652
defn.specialErasure(owner)
657-
}
658-
else if (defn.isSyntheticFunctionClass(owner))
653+
else if defn.isSyntheticFunctionClass(owner)
659654
defn.erasedFunctionClass(owner)
660655
else
661656
owner
662-
}
663-
}
664657

665658
val origSym = tree.symbol
666659

@@ -710,7 +703,11 @@ object Erasure {
710703
if (qual1.tpe.derivesFrom(sym.owner) || qual1.isInstanceOf[Super])
711704
select(qual1, sym)
712705
else
713-
recur(cast(qual1, sym.owner.typeRef))
706+
val castTarget = // Avoid inaccessible cast targets, see i8661
707+
if sym.owner.isAccessibleFrom(qual1.tpe)(using preErasureCtx)
708+
then sym.owner.typeRef
709+
else erasure(tree.qualifier.typeOpt.widen)
710+
recur(cast(qual1, castTarget))
714711
}
715712
}
716713

tests/run/i8661.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi

tests/run/i8661/B.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package pk;
2+
class B {
3+
public void hi() { System.out.println("hi"); }
4+
}

tests/run/i8661/C.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package pk;
2+
public class C extends B {
3+
public C() {}
4+
}

tests/run/i8661/Test.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pk.C
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
identity(new C).hi()
5+
}
6+
}

0 commit comments

Comments
 (0)