Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inferred type eludes variance check: unsoundness results #5060

Closed
scabug opened this issue Oct 6, 2011 · 3 comments
Closed

inferred type eludes variance check: unsoundness results #5060

scabug opened this issue Oct 6, 2011 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Oct 6, 2011

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.

@scabug
Copy link
Author

scabug commented Oct 6, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5060?orig=1
Reporter: @paulp
See #5517

@scabug
Copy link
Author

scabug commented Oct 6, 2011

Commit Message Bot (anonymous) said:
(extempore in r25797) Closing soundness hole in variance checking.

See #5060. Review by odersky.

@scabug
Copy link
Author

scabug commented Feb 23, 2012

@soc said:
This should be closed, at least the fix and the tests are committed.

@scabug scabug closed this as completed Feb 23, 2012
@scabug scabug added this to the 2.10.0-M1 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants