Skip to content

Fix #8661: Avoid inaccessible cast targets in erasure #8668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/run/i8661.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
4 changes: 4 additions & 0 deletions tests/run/i8661/B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pk;
class B {
public void hi() { System.out.println("hi"); }
}
4 changes: 4 additions & 0 deletions tests/run/i8661/C.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pk;
public class C extends B {
public C() {}
}
6 changes: 6 additions & 0 deletions tests/run/i8661/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pk.C
object Test {
def main(args: Array[String]): Unit = {
identity(new C).hi()
}
}