Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-7540

optimizer changes behavior when pattern matching Some(X) scrutinee when X's type assumed wrong

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: Optimizer
    • Labels:

      Description

      The following program produces different results depending on whether it's compiled with -optimise or not.

      object Test {
       
        def main(args: Array[String]) {
          val s = Some(args).asInstanceOf[Option[String]]
          m(s)
        }
       
        def m(a: Option[String]) {
          a match {
            case null => println("null")
            case None => println("None")
            case Some(x) if x == null => println("Some(null)")
            case Some(x)              => println(s"Some($x)")
          }
        }
       
      }
      

      Console output unoptimized:

      java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
              at Test$.m(bt4.scala:12)
              at Test$.main(bt4.scala:5)
              at Test.main(bt4.scala)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:601)
              . . .
      
      

      Console output under -optimise :

      Some([Ljava.lang.String;@4cd96e86)
      

      One could argue whether:

      • it's the asInstanceOf that goes over the edge, or
      • the removal of the checkcast bytecode that went too far.

      In any case, it's a behavioral difference wrt the unoptimized run (and that must be the correct one).

        Attachments

          Activity

            People

            • Assignee:
              jamesiry James Iry
              Reporter:
              magarcia Miguel Garcia
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: