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
unsound type parameter inference #5189
Comments
Imported From: https://issues.scala-lang.org/browse/SI-5189?orig=1 |
@paulp said: scala> def f(x: Any) = x match { case Foo(bar) => bar }
<console>:9: warning: non variable type-argument Any in type Foo[Any,Any] is unchecked since it is eliminated by erasure
def f(x: Any) = x match { case Foo(bar) => bar }
^
f: (x: Any)Any => Any |
@paulp said: |
@adriaanm said: |
@adriaanm said (edited on Jul 28, 2012 8:33:44 AM UTC): |
@adriaanm said: |
@paulp said:
def unwrap[T](x: AbsWrapperCov[T])/*: Wrapped[T]*/ = x match {
case Wrapper/*[_ <: T ]*/(wrapped) => wrapped
} Then it compiles - and infers Wrapped[T], unsoundly. I assume this is pt=WildcardType never coming to grips with the situation. I believe the soundness issue requires an invariant something like: if the type of a GADT method (inferred or otherwise) uses the method's type parameters as type arguments, then either the corresponding class type parameters must be covariant, or the usages must be upper-bounded by the type parameter rather than the type parameter itself. Similar for contravariant/lower. That doesn't address the type inference question - it has to infer Wrapped[_ <: T] rather than Wrapped[T] in the above example, and I'm not sure what that will take. I also have serious doubts about the mitigation strategy taken with the original patch, i.e. adding a bunch of casts. Those casts look pretty unsafe, is there reason to believe otherwise? |
@paulp said: 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>) |
@paulp said: 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 |
@adriaanm said: |
@adriaanm said: |
I thought this was already open somewhere but I can't find it.
The text was updated successfully, but these errors were encountered: