Details

      Description

      I thought this was already open somewhere but I can't find it.

      scala> case class Foo[T, U](f: T => U)
      defined class Foo
      
      // uh-oh, Any => Any should be Nothing => Any.
      scala> def f(x: Any) = x match { case Foo(bar) => bar }
      f: (x: Any)Any => Any
      
      scala> def f(x: Any) = x match { case Foo(bar) => bar(5) }
      f: (x: Any)Any
      
      scala> f(Foo((x: String) => x))
      java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
      	at $anonfun$1.apply(<console>:11)
      	at .f(<console>:9)
      	at .<init>(<console>:11)
      

        Issue Links

          Activity

          Hide
          Paul Phillips added a comment -

          Funny that I didn't immediately see the connection to SI-6925. (I discovered these through completely independent paths.) The funny thing is that in this one I'm pointing out that inferring Wrapped[T] is unsound and it has to infer Wrapped[_ <: T], and in SI-6925 we're calling the inference of Set[_ <: T] instead of Set[T] a regression. But maybe it can infer Set[T] on the invariant type? It's the covariant case for which it must only assume a bound, not a type.

          My email to scala-internals was: https://groups.google.com/d/topic/scala-internals/dGQCGQ8dYNs/discussion

          Here is a more direct demonstration.

          scala> abstract class Covariant[+A](x: A) { def apply(): A = x }
          defined class Covariant
          
          scala> case class Invariant[A](val xs: Array[A]) extends Covariant[A](xs.head)
          defined class Invariant
          
          scala> def f[A](value: Covariant[A]) = value match { case Invariant(xs) => xs }
          f: [A](value: Covariant[A])Array[A]
          
          scala> f(Invariant(Array[Int](1)): Covariant[Any])
          java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
          	at .<init>(<console>:12)
          	at .<clinit>(<console>)
          	at .<init>(<console>:11)
          	at .<clinit>(<console>)
          	at $print(<console>)
          
          Show
          Paul Phillips added a comment - Funny that I didn't immediately see the connection to SI-6925 . (I discovered these through completely independent paths.) The funny thing is that in this one I'm pointing out that inferring Wrapped [T] is unsound and it has to infer Wrapped [_ <: T] , and in SI-6925 we're calling the inference of Set [_ <: T] instead of Set [T] a regression. But maybe it can infer Set [T] on the invariant type? It's the covariant case for which it must only assume a bound, not a type. My email to scala-internals was: https://groups.google.com/d/topic/scala-internals/dGQCGQ8dYNs/discussion Here is a more direct demonstration. scala> abstract class Covariant[+A](x: A) { def apply(): A = x } defined class Covariant scala> case class Invariant[A](val xs: Array[A]) extends Covariant[A](xs.head) defined class Invariant scala> def f[A](value: Covariant[A]) = value match { case Invariant(xs) => xs } f: [A](value: Covariant[A])Array[A] scala> f(Invariant(Array[Int](1)): Covariant[Any]) java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object; at .<init>(<console>:12) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>)
          Hide
          Paul Phillips added a comment -

          And for backward compat to 2.7, here are 5 lines which incur an ArrayStoreException on any version of scala.

          trait Covariant[+A]
          val arr = Array("abc")
          case class Invariant[A](xs: Array[A]) extends Covariant[A]
          def f[A](v: Covariant[A]) = v match { case Invariant(xs) => xs }
          f(Invariant(arr): Covariant[Any])(0) = Nil
          
          Show
          Paul Phillips added a comment - And for backward compat to 2.7, here are 5 lines which incur an ArrayStoreException on any version of scala. trait Covariant[+A] val arr = Array("abc") case class Invariant[A](xs: Array[A]) extends Covariant[A] def f[A](v: Covariant[A]) = v match { case Invariant(xs) => xs } f(Invariant(arr): Covariant[Any])(0) = Nil
          Hide
          Paul Phillips added a comment -

          See further elaboration in SI-6944.

          Show
          Paul Phillips added a comment - See further elaboration in SI-6944 .
          Hide
          Adriaan Moors added a comment -

          ok, fixed it – there was some deskolemization logic that was only necessary to appease the old pattern matcher
          it's been moved to a less dangerous spot (/dev/null would be the most appropriate one in master)

          Show
          Adriaan Moors added a comment - ok, fixed it – there was some deskolemization logic that was only necessary to appease the old pattern matcher it's been moved to a less dangerous spot (/dev/null would be the most appropriate one in master)
          Show
          Adriaan Moors added a comment - https://github.com/scala/scala/pull/1876

            People

            • Assignee:
              Adriaan Moors
              Reporter:
              Paul Phillips
            • Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development