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 projection #3481
Comments
Imported From: https://issues.scala-lang.org/browse/SI-3481?orig=1 |
@harrah said: Note that the argument type of f is Any, so you can't do anything on 'a' that is unsafe. |
@jesnor said: scala> trait A { type T; def m(t : T) = t.toString }
defined trait A
scala> class B[T2] extends A { type T = T2 }
defined class B
scala> def f[T <: B[_]](a : T#T, b : T) = b.m(a)
f: [T <: B[_]](a: Any,b: T)java.lang.String
scala> f("Hello", new B[Int])
res0: java.lang.String = Hello Which obviously is unsound (calling (new B[Int]).m with a String). Note that changing f to: def f[T <: A](a : T#T, b : T) = b.m(a) gives a compiler error. So the problem is obviously related to existential types. |
@jesnor said: scala> class B[T] { type T2 = T; def m(t : T2) = t.toString }
defined class B
scala> val b : B[_] = new B[Int]
b: B[_] = B@110d68a
scala> b.m("Hello")
res7: java.lang.String = Hello |
@harrah said: scala> class B[T] { type T2 = T; def m(t : T2) = t.toString }
defined class B
scala> class C extends B[Int] { override def m(t: Int) = t + 2 }
defined class C
scala> val b : B[_] = new C
b: B[_] = C@488d12e4
scala> b.m("Hello")
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
at C.m(<console>:6)
at .<init>(<console>:9) |
@adriaanm said: abstract class B[T] {
type T2 = T
def m(t : T2): Any
}
object Test {
val b : B[_] = error("")
b.m("Hello") // should not typecheck, but b.T2 normalizes to Any!!
// thus, String <: b.T2 (because thirdTryRef in isSubtype normalizes)
// T as seen from Test.b is Any... should we have skolemized before getting here, or is asSeenFrom broken!? (oh my!)
} |
@SethTisue said: |
@adriaanm said: Martin, I'd be happy to implement a fix if you point me in the right direction (BTW, it's not a regression, it type checks in 2.7.3.final) |
@soc said (edited on Dec 1, 2011 7:58:42 PM UTC): Welcome to Scala version 2.10.0.r26093-b20111130020250 (OpenJDK 64-Bit Server VM, Java 1.7.0_147-icedtea). First example: <console>:10: error: type mismatch;
found : String("hello")
required: _$1 where type +_$1
f[A[Int]]("hello")
^ Second example: <console>:10: error: type mismatch;
found : String("Hello")
required: b.T2
(which expands to) _$1
b.m("Hello")
^ Is this fixed? |
@paulp said: |
@soc said: |
@paulp said: |
@soc said: |
@soc said: |
The following code works fine in latest nightly:
Instead of succeeding, it should give a type error on the last line.
The text was updated successfully, but these errors were encountered: