Aladdin - Scala Bugtracking
[#781] project: compiler priority: low category: bug
submitter assigned to status date submitted
Nikolay Burak fixed 2006-10-19 12:27:31.0
subject [contrib #229] pattern-matching null gives inconsistent results
code
scala> null.asInstanceOf[String] match { case x : String => true }
line0: scala.Boolean = true

scala> null.asInstanceOf[AnyRef] match { case x : String => true }
java.lang.NullPointerException
        at scala.MatchError.<init>(MatchError.scala:45)
        at line1$object$.<init>(<console>:4)
        at line1$object$.<clinit>(<console>)
        at RequestResult$line1$object$.<init>(<console>:3)
        at RequestResult$line1$object$.<clinit>(<console>)
        at RequestResult$line1$object.result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:517)        at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:246)
        at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:168)
        at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:81)
        at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:187)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:82)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

scala> null.asInstanceOf[String] match { case x : String => true; case _ => false }
<console>:4 error: unreachable code
  val line2 = null.asInstanceOf[String] match { case x : String => true; case _ => false }
    ^

scala> null.asInstanceOf[String] match { case x : String => true; case null => false }
line3: scala.Boolean = true

scala> null.asInstanceOf[AnyRef] match { case x : String => true; case null => false }
line4: scala.Boolean = false
what happened
see above
what expected The line0 and line1 examples should give the same results (either both should return true, or both should fail). The line2 and line3 examples should give the same results (either both should error out as having unreachable code, or both should return false). Note that the line3 example does have unreachable code (it's evidently impossible to reach the second branch), and the compiler should be able to know this. The line3 and line4 examples should give the same results (either both should error out as having unreachable code, or both should return false). I realize that making the implementation consistent in this way may reduce performance; if you choose not to fix it, these odd behaviors should at least be documented. (Although I think that anybody who writes the code in the line3 example would really want it to return false, even if that slows things down a little!)
[back to overview]
Changes of this bug report
Nikolay  edited on  2006-10-19 12:28:15.0
Transferred from the contributions and assigned to Burak
Burak  edited on  2006-10-19 14:31:01.0
Due to an incorrect optimization of typed patterns. There is no unreachable code. The behavior is now
scala> null.asInstanceOf[String] match { case x : String => true }
scala.MatchError
 
scala> null.asInstanceOf[AnyRef] match { case x : String => true }
scala.MatchError: null
scala> null.asInstanceOf[String] match { case x : String => true; case _ => false }
false
scala> null.asInstanceOf[String] match { case x : String => true; case null => false }
false
scala> null.asInstanceOf[AnyRef] match { case x : String => true; case null => false }
false
Burak  edited on  2006-10-19 14:32:39.0
The spec on pattern matching really means non-null values (or equivalently, instance in terms of "instanceOf") when it says a typed pattern matches any value that is instance of the type.
Burak  edited on  2006-10-19 16:14:36.0
(set to fix again just to test contrib)
Burak  edited on  2006-10-19 17:44:47.0
(test)
Burak  edited on  2006-10-19 17:45:13.0
Burak  edited on  2006-10-19 17:52:14.0
Burak  edited on  2006-10-19 17:55:21.0
Burak  edited on  2006-10-19 17:55:40.0