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

unchecked variance declarations in type lambdas as method type parameters

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.3-RC3, Scala 2.11.0-M5
    • Fix Version/s: Scala 2.11.0-M8
    • Component/s: Misc Compiler
    • Environment:

      Oracle JDK 1.7.0_25-b17, Windows Server 2008 R2

      Description

      Seems vaguely related to SI-1364.

      oops1 & oops2 below should not compile; they throw as commented at runtime.

      This seems specific to the extra structural type & projection; see how up[Stringer] fails to compile, but a trivial wrapper around Stringer causes it to pass.

      coinv.scala
      object coinv {
        def up[F[+_]](fa: F[String]): F[Object] = fa
        def down[F[-_]](fa: F[Object]): F[String] = fa
      
        up(List("hi"))
        // [error] type A is covariant, but type _ is declared contravariant
        // down(List('whatever: Object))
      
        // should not compile; `l' is unsound
        def oops1 = down[({type l[-a] = List[a]})#l](List('whatever: Object)).head + "oops"
        // scala> oops1
        // java.lang.ClassCastException: scala.Symbol cannot be cast to java.lang.String
        //         at com.nocandysw.coinv$.oops1(coinv.scala:12)
      
        type Stringer[-A] = A => String
        down[Stringer](_.toString)
        // [error] type A is contravariant, but type _ is declared covariant
        // up[Stringer]("printed: " + _)
      
        // should not compile; `l' is unsound
        def oops2 = up[({type l[+a] = Stringer[a]})#l]("printed: " + _)
        // scala> oops2(Some(33))
        // java.lang.ClassCastException: scala.Some cannot be cast to java.lang.String
        //         at com.nocandysw.coinv$$anonfun$oops2$1.apply(coinv.scala:20)
      }
      

        Issue Links

          Activity

          Hide
          Stephen Compall added a comment -

          Similarly for invariant positions:

          def oops3 = up[({type l[+a] = a => a})#l](_ + "hi")('fortytwo)
          // java.lang.ClassCastException: scala.Symbol cannot be cast to java.lang.String
          
          Show
          Stephen Compall added a comment - Similarly for invariant positions: def oops3 = up[({type l[+a] = a => a})#l](_ + "hi")('fortytwo) // java.lang.ClassCastException: scala.Symbol cannot be cast to java.lang.String
          Show
          Jason Zaugg added a comment - https://github.com/scala/scala/pull/3178

            People

            • Assignee:
              Jason Zaugg
              Reporter:
              Stephen Compall
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development