Skip to content

Type inference does not account for bounded type parameters from java #23723

@jchyb

Description

@jchyb

Compiler version

3.7.2 (any, even scala 2)

Minimized code

import java.io.InputStream;

class ArchiveInputStream<I> {
  I i;
  public ArchiveInputStream(I i) {
    this.i = i;
  }
  public static <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInputStream() {
    return (I)(new ArchiveInputStream<ArchiveEntry>(new ArchiveEntry()));
  }

  public I getI() {
    return i;
  }
}
class ArchiveEntry{
  public void test() {
    System.out.println("ok");
  }
}
import scala.util.Try
import java.io._

object Test {
   def createArchiveStream(): Try[ArchiveInputStream[ArchiveEntry]] = {
      Try {
         ArchiveInputStream.createArchiveInputStream()
      }
   }

   def main(args: Array[String]) =
      createArchiveStream().get.getI.test()
}

Output

Runtime crash:

Exception in thread "main" java.lang.ClassCastException: class ArchiveInputStream cannot be cast to class scala.runtime.Nothing$ (ArchiveInputStream and scala.runtime.Nothing$ are in unnamed module of loader 'app')
        at Test$.$anonfun$createArchiveStream$1(Repr.scala:7)
        at scala.util.Try$.apply(Try.scala:213)
        at Test$.createArchiveStream(Repr.scala:7)
        at Test$.main(Repr.scala:12)
        at Test.main(Repr.scala)```

Expectation

I think ArchiveInputStream.createArchiveInputStream() should have the extends ArchiveInputStream<? extends ArchiveEntry> constraint taken into account when inferring so that it does not get inferred into Nothing. In that case, we would have the "ok" printed out.

I am a little surprised this doesn't even work in scala 2, which makes me feel like I am missing something (perhaps it's a known limitation?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    itype:bugstat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions