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

scala.util.Try.apply produces java.lang.ClassCastException

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0-M7
    • Fix Version/s: Scala 2.10.0-M7, Scala 2.10.0
    • Component/s: Misc Library
    • Labels:
      None
    • Environment:

      Ubuntu 11.10 / Windows 7
      OpenJDK Runtime Environment (IcedTea6 1.11.3) (6b24-1.11.3-2)
      scala 2.10.0-M7

      Description

      A weird runtime exception by Try.apply

      $ scala-2.10.0-M7/bin/scala -e 'scala.util.Try(1.toString)'
      java.lang.ClassCastException: java.lang.String cannot be cast to scala.Function0
      at Main$$anon$1.<init>(scalacmd5144416276510110047.scala:1)
      at Main$.main(scalacmd5144416276510110047.scala:1)
      at Main.main(scalacmd5144416276510110047.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:616)
      at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71)
      ....

      Same error can be reproduced in repl and in real compiled application using Test.scala:

      object Test {
      def main(args: Array[String])

      { scala.util.Try(1.toString) }

      }

      $ scala-2.10.0-M7/bin/scalac Test.scala
      $ scala-2.10.0-M7/bin/scala Test

        Activity

        Hide
        Jason Zaugg added a comment -

        Bonus points, please:

        scala210 -nocompdaemon -e "def foo[A](a: => A) = a; foo(1.getClass())"
        java.lang.ClassCastException: java.lang.Class cannot be cast to scala.Function0
        	at Main$$anon$1.<init>(scalacmd4632653116387645739.scala:1)
        
        Show
        Jason Zaugg added a comment - Bonus points, please: scala210 -nocompdaemon -e "def foo[A](a: => A) = a; foo(1.getClass())" java.lang.ClassCastException: java.lang.Class cannot be cast to scala.Function0 at Main$$anon$1.<init>(scalacmd4632653116387645739.scala:1)
        Hide
        A. P. Marki added a comment - - edited

        Community spirit badge if the PR fixes the comment Note that this is not called "isExprSafeToInline" to Note that this is not called "isPureExpr".

        Kind of a "ceci n'est pas une pipe" moment. I read that in the middle of the night and stopped blinking for an indeterminate length of time.

        https://github.com/scala/scala/blob/v2.10.0-M7/src/reflect/scala/reflect/internal/TreeInfo.scala#L70

        >I think, that's what Paolo meant with "lose their func wrappers".
        It was. It's a little known and infrequently mulled fact that my middle name is Paul. And full disclosure, in that comment, I almost wrote, "funky wrappers."

        Show
        A. P. Marki added a comment - - edited Community spirit badge if the PR fixes the comment Note that this is not called "isExprSafeToInline" to Note that this is not called "isPureExpr" . Kind of a "ceci n'est pas une pipe" moment. I read that in the middle of the night and stopped blinking for an indeterminate length of time. https://github.com/scala/scala/blob/v2.10.0-M7/src/reflect/scala/reflect/internal/TreeInfo.scala#L70 >I think, that's what Paolo meant with "lose their func wrappers". It was. It's a little known and infrequently mulled fact that my middle name is Paul. And full disclosure, in that comment, I almost wrote, "funky wrappers."
        Hide
        Paolo G. Giarrusso added a comment -

        I retract my comment above, I debugged the wrong thing.
        @Jason: deEta, the code you mention, seems to change the type of the result. A comment should explain when it's supposed to work. If it never does, it should be removed, but assuming that it works correctly in some cases, a very minimal attempt for the fix can be found here:
        https://github.com/Blaisorblade/scala/tree/issue/6306

        It does work on the testcase, but it seems overkill; I add a set named inByNameCallSite analogously to byNameArg, but I'm not sure if I could just use byNameArg because I can't fully read the code; I currently suspect that I couldn't.

        If this becomes a pull request, I'll add proper acknowledgments to everybody involved.

        Show
        Paolo G. Giarrusso added a comment - I retract my comment above, I debugged the wrong thing. @Jason: deEta, the code you mention, seems to change the type of the result. A comment should explain when it's supposed to work. If it never does, it should be removed, but assuming that it works correctly in some cases, a very minimal attempt for the fix can be found here: https://github.com/Blaisorblade/scala/tree/issue/6306 It does work on the testcase, but it seems overkill; I add a set named inByNameCallSite analogously to byNameArg , but I'm not sure if I could just use byNameArg because I can't fully read the code; I currently suspect that I couldn't. If this becomes a pull request, I'll add proper acknowledgments to everybody involved.
        Show
        Paolo G. Giarrusso added a comment - https://github.com/scala/scala/pull/1255
        Hide
        Paolo G. Giarrusso added a comment - - edited

        Pull request 1255 was finally merged, so I'm closing this bug.

        Show
        Paolo G. Giarrusso added a comment - - edited Pull request 1255 was finally merged, so I'm closing this bug.

          People

          • Assignee:
            Josh Suereth
            Reporter:
            Mikael Taina
          • Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development