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

inferred type eludes variance check: unsoundness results

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0-M1
    • Component/s: None
    • Labels:
      None

      Description

      I thought I was going to fix this, but I started getting pretty far afield so I'll open it.

      class A[+T] {
        val foo0 = {
          class AsVariantAsIWantToBe { def contains(x: T) = () }
          new AsVariantAsIWantToBe
        }
        def foo1 = {
          class VarianceIsTheSpiceOfTypes { def contains(x: T) = () }
          new VarianceIsTheSpiceOfTypes
        }
      }
      
      object Test {
        def main(args: Array[String]): Unit = {
          val xs: A[String] = new A[String]
          println(xs.foo0 contains "abc")
          println((xs: A[Any]).foo0 contains 5)
          // java.lang.NoSuchMethodException: A$AsVariantAsIWantToBe$1.contains(java.lang.String)
        }
      }
      

      My understand is as follows: the variance validator takes the position that you can use type parameters however you like if your owner is a term, like val foo0. Though you are prohibited from declaring foo0 to have the unsound type ("Parameter type in structural refinement may not refer to an abstract type defined outside that refinement"), scala is still willing to infer it. In this case, that it infers within A[T] that foo0 has type

      ScalaObject

      {def contains(z0: T):Unit}

      Since A is covariant in T, it's bedlam in soundness town.

        Issue Links

          Activity

          Hide
          Commit Message Bot added a comment -

          (extempore in r25797) Closing soundness hole in variance checking.

          See SI-5060. Review by odersky.

          Show
          Commit Message Bot added a comment - (extempore in r25797 ) Closing soundness hole in variance checking. See SI-5060 . Review by odersky.
          Hide
          Simon Ochsenreither added a comment -

          This should be closed, at least the fix and the tests are committed.

          Show
          Simon Ochsenreither added a comment - This should be closed, at least the fix and the tests are committed.

            People

            • Assignee:
              Martin Odersky
              Reporter:
              Paul Phillips
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development