Skip to content

MatchError when calling a public method inherited from a default class #17391

Closed
@theosotr

Description

@theosotr

Compiler version

3.2.2

Minimized code

I have the following Java code

package mypkg;

class Base<E> {
    public void retainAll(String x) {}
}

public class Bar<E> extends Base<E> {}

which is called from the following Scala code:

def test(): Unit =
{
  val x: Foo[mypkg.Bar[String]] = ???
  val y: mypkg.Bar[String] = ???

  y.retainAll("fd") // works
  x.f.retainAll("fd"); // crashes

}

class Foo[T](val f: T)

Output

-- Error: program.scala:6:2 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
6 |  x.f.retainAll("fd"); // crashes
  |  ^^^
  |  Unable to emit reference to method retainAll in class Base, class Base is not accessible in package object program$package
exception while typing x.f.retainAll("fd") of class class dotty.tools.dotc.ast.Trees$Apply # -1
exception while typing {
  val x: Foo[mypkg.Bar[String]] = ???
  val y: mypkg.Bar[String] = ???
  y.retainAll("fd")
  x.f.retainAll("fd")
} of class class dotty.tools.dotc.ast.Trees$Block # -1
exception while typing def test(): Unit =
  {
    val x: Foo[mypkg.Bar[String]] = ???
    val y: mypkg.Bar[String] = ???
    y.retainAll("fd")
    x.f.retainAll("fd")
  } of class class dotty.tools.dotc.ast.Trees$DefDef # -1
exception while typing @SourceFile("program.scala") final module class program$package() extends Object() {
  private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[program$package.type])
  def test(): Unit =
    {
      val x: Foo[mypkg.Bar[String]] = ???
      val y: mypkg.Bar[String] = ???
      y.retainAll("fd")
      x.f.retainAll("fd")
    }
} of class class dotty.tools.dotc.ast.Trees$TypeDef # -1
exception while typing package <empty> {
  @SourceFile("program.scala") class Foo[T](f: T) extends Object() {
    private type T
    def f: T
  }
  final lazy module val program$package: program$package = new program$package()
  @SourceFile("program.scala") final module class program$package() extends Object() {
    private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[program$package.type])
    def test(): Unit =
      {
        val x: Foo[mypkg.Bar[String]] = ???
        val y: mypkg.Bar[String] = ???
        y.retainAll("fd")
        x.f.retainAll("fd")
      }
  }
} of class class dotty.tools.dotc.ast.Trees$PackageDef # -1
scala.MatchError: tree x.f.retainAll("fd") has unexpected type of function x.f.retainAll/x.f(): <error Unable to emit reference to method retainAll in class Base, class Base is not accessible in package object program$package>, was (x$0: String): Unit, args = ["fd"] (of class java.lang.String) while running erasure on program.scala
exception occurred while compiling program.scala
scala.MatchError: tree x.f.retainAll("fd") has unexpected type of function x.f.retainAll/x.f(): <error Unable to emit reference to method retainAll in class Base, class Base is not accessible in package object program$package>, was (x$0: String): Unit, args = ["fd"] (of class java.lang.String) while compiling program.scala
Exception in thread "main" scala.MatchError: tree x.f.retainAll("fd") has unexpected type of function x.f.retainAll/x.f(): <error Unable to emit reference to method retainAll in class Base, class Base is not accessible in package object program$package>, was (x$0: String): Unit, args = ["fd"] (of class java.lang.String)
        at dotty.tools.dotc.transform.Erasure$Typer.typedApply(Erasure.scala:862)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2895)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2958)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:126)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3025)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3029)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3145)
        at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1069)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2903)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2958)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:126)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3025)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3029)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3145)
        at dotty.tools.dotc.typer.Typer.$anonfun$51(Typer.scala:2356)
        at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:249)
        at dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2356)
        at dotty.tools.dotc.transform.Erasure$Typer.typedDefDef(Erasure.scala:944)
        at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2871)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2957)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:126)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3025)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3029)
        at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3051)
        at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3101)
        at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1043)
        at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2532)
        at dotty.tools.dotc.transform.Erasure$Typer.typedClassDef(Erasure.scala:1032)
        at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:2883)
        at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2887)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2957)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:126)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3025)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3029)
        at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3051)
        at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3101)
        at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:1043)
        at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2659)
        at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2928)
        at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2958)
        at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:126)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3025)
        at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3029)
        at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3145)
        at dotty.tools.dotc.transform.Erasure.run(Erasure.scala:144)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:316)
        at scala.collection.immutable.List.map(List.scala:246)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:320)
        at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:238)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
        at dotty.tools.dotc.Run.runPhases$1(Run.scala:249)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:257)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:266)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:68)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:266)
        at dotty.tools.dotc.Run.compileSources(Run.scala:190)
        at dotty.tools.dotc.Run.compile(Run.scala:174)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:35)
        at dotty.tools.dotc.Driver.process(Driver.scala:195)
        at dotty.tools.dotc.Driver.process(Driver.scala:163)
        at dotty.tools.dotc.Driver.process(Driver.scala:175)
        at dotty.tools.dotc.Driver.main(Driver.scala:205)
        at dotty.tools.MainGenericCompiler$.run$1(MainGenericCompiler.scala:162)
        at dotty.tools.MainGenericCompiler$.main(MainGenericCompiler.scala:186)
        at dotty.tools.MainGenericCompiler.main(MainGenericCompiler.scala)

Expectation

Compile successfully

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions