Scala Programming Language
  1. Scala Programming Language
  2. SI-750

Scala tries to pass primitive arrays to Java methods expecting polymorphic array types

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Misc Compiler
    • Labels:
      None
    • Environment:

      java, generics

      Description

      Spotted by Amit Dev

      Java:

      public class AO {
          public static <T> void f(T[] a) {
              System.out.println(ar0);
          }
      }
      

      Scala:

      object Main {
          def main(argv : Array[String]) : Unit = {
              val a = Array(1, 2, 3)
              AO.f(a)
          }
      }
      

      This tries to cast `a` to `Object[]`, which will always fail. It should give a compile-time error, since Java doesn't support polymorphic use of primitive arrays.

        Issue Links

          Activity

          Hide
          Martin Odersky added a comment -

          This will depend on the question whether or not to restrict Java generics to reference types. Postponed until we decide that question.

          Show
          Martin Odersky added a comment - This will depend on the question whether or not to restrict Java generics to reference types. Postponed until we decide that question.
          Hide
          Martin Odersky added a comment -

          Milestone postponed deleted

          Show
          Martin Odersky added a comment - Milestone postponed deleted
          Hide
          Paul Phillips added a comment -

          This is maybe better, maybe worse now: it compiles from all source, but not against the bytecode.

          [paulp@leaf 750]$$ scalac AO.java a.scala 
          [paulp@leaf 750]$$ scala Main
          java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
          	at Main$$.main(a.scala:4)
          	at Main.main(a.scala)
          

          But:

          [paulp@leaf 750]$$ javac AO.java 
          [paulp@leaf 750]$$ scalac a.scala 
          a.scala:4: error: type mismatch;
           found   : Array[Int]
           required: Array[? with java.lang.Object]
                  AO.f(a)
                       ^
          one error found
          
          Show
          Paul Phillips added a comment - This is maybe better, maybe worse now: it compiles from all source, but not against the bytecode. [paulp@leaf 750]$$ scalac AO.java a.scala [paulp@leaf 750]$$ scala Main java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object; at Main$$.main(a.scala:4) at Main.main(a.scala) But: [paulp@leaf 750]$$ javac AO.java [paulp@leaf 750]$$ scalac a.scala a.scala:4: error: type mismatch; found : Array[Int] required: Array[? with java.lang.Object] AO.f(a) ^ one error found
          Hide
          Paul Phillips added a comment -

          Consolidating with closely related SI-4216.

          Show
          Paul Phillips added a comment - Consolidating with closely related SI-4216 .
          Hide
          Jason Zaugg added a comment -

          It's working in both joint and separate compilation now. Test cases, and a few more comments: https://github.com/scala/scala/pull/479

          Show
          Jason Zaugg added a comment - It's working in both joint and separate compilation now. Test cases, and a few more comments: https://github.com/scala/scala/pull/479

            People

            • Assignee:
              Martin Odersky
              Reporter:
              Jamie Webb
              TracCC:
              Paul Phillips
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development