Skip to content
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

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

Closed
theosotr opened this issue May 2, 2023 · 0 comments · Fixed by #18056
Closed

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

theosotr opened this issue May 2, 2023 · 0 comments · Fixed by #18056

Comments

@theosotr
Copy link

theosotr commented May 2, 2023

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

@theosotr theosotr added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels May 2, 2023
@mbovel mbovel added compat:java area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 4, 2023
@odersky odersky self-assigned this May 5, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 25, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 26, 2023
odersky added a commit to dotty-staging/dotty that referenced this issue Jun 26, 2023
The original qualifier type was computed in the wrong context. For the logic
to make sense, it needs to compute the type before erasure and then erase that
type. That fixes scala#17391 by now compiling it correctly, the same way Scala 2 does.
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Kordyjan pushed a commit that referenced this issue Nov 23, 2023
Kordyjan pushed a commit that referenced this issue Nov 23, 2023
The original qualifier type was computed in the wrong context. For the logic
to make sense, it needs to compute the type before erasure and then erase that
type. That fixes #17391 by now compiling it correctly, the same way Scala 2 does.

[Cherry-picked 996bb15]
Kordyjan pushed a commit that referenced this issue Nov 29, 2023
Kordyjan pushed a commit that referenced this issue Nov 29, 2023
The original qualifier type was computed in the wrong context. For the logic
to make sense, it needs to compute the type before erasure and then erase that
type. That fixes #17391 by now compiling it correctly, the same way Scala 2 does.

[Cherry-picked 996bb15]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants