Skip to content

Java interop: Type Mismatch for vararg ...T because "Required: T & Object" #9439

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

Closed
sjrd opened this issue Jul 27, 2020 · 0 comments
Closed

Comments

@sjrd
Copy link
Member

sjrd commented Jul 27, 2020

Minimized code

object Test {
  // First example with a concrete type <: AnyVal
  def main(args: Array[String]): Unit = {
    val coll = new java.util.ArrayList[Int]()
    java.util.Collections.addAll(coll, 5, 6)
    println(coll.size())
  }

  // Second example with an abstract type not known to be <: AnyRef
  def foo[A](a1: A, a2: A): Unit = {
    val coll = new java.util.ArrayList[A]()
    java.util.Collections.addAll(coll, a1, a2)
    println(coll.size())
  }
}

Output

-- [E007] Type Mismatch Error: tests\run\hello.scala:5:39 ----------------------
5 |    java.util.Collections.addAll(coll, 5, 6)
  |                                       ^
  |                                       Found:    (5 : Int)
  |                                       Required: Int & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:5:42 ----------------------
5 |    java.util.Collections.addAll(coll, 5, 6)
  |                                          ^
  |                                          Found:    (6 : Int)
  |                                          Required: Int & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:12:39 ---------------------
12 |    java.util.Collections.addAll(coll, a1, a2)
   |                                       ^^
   |                                       Found:    (a1 : A)
   |                                       Required: A & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:12:43 ---------------------
12 |    java.util.Collections.addAll(coll, a1, a2)
   |                                           ^^
   |                                           Found:    (a2 : A)
   |                                           Required: A & Object
4 errors found

Expectation

I expect that both examples compile.

Notes

JavaDoc of java.util.Collections.addAll

public static <T> boolean addAll(Collection<? super T> c, T... elements)

This prevents the Scala.js test CollectionsOnCollectionsTest from compiling, at the following line:
https://github.com/scala-js/scala-js/blob/6b9f04b0c0531d889235726d8956cb01a4c7476d/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/CollectionsOnCollectionsTest.scala#L146

@sjrd sjrd added the itype:bug label Jul 27, 2020
@smarter smarter self-assigned this Jul 27, 2020
smarter added a commit to dotty-staging/dotty that referenced this issue Jul 27, 2020
This is much more convenient for users but is more complicated for the
compiler since we still need to translate the varargs into an `Object[]`
in bytecode. Since ElimRepeated was already responsible for doing some
adaptation of repeated arguments, it now also takes care of this (this
differs from Scala 2 which handles this at Erasure).

Fixes scala#9439.
smarter added a commit to dotty-staging/dotty that referenced this issue Jul 27, 2020
This is much more convenient for users but is more complicated for the
compiler since we still need to translate the varargs into an `Object[]`
in bytecode. Since ElimRepeated was already responsible for doing some
adaptation of repeated arguments, it now also takes care of this (this
differs from Scala 2 which handles this at Erasure).

Fixes scala#9439.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants