You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inferred types must uniformly encounter the same restrictions as declared types #6944
traitCovariant[+A]
valarr=Array("abc")
caseclassInvariant[A](xs: Array[A]) extendsCovariant[A]
deff[A](v: Covariant[A]) = v match { caseInvariant(xs) => xs }
f(Invariant(arr):Covariant[Any])(0) =Nil
That is a soundness hole in every version of scala. It is interesting to note however that through scala 2.9.2, you could write f in either of these ways, and it would still compile unsoundly:
deff[A](v: Covariant[A]) = v match { caseInvariant(xs) => (xs: Array[A]) }
deff[A](v: Covariant[A]):Array[A] = v match { caseInvariant(xs) => xs }
In scala 2.10 those versions no longer compile, BUT the unsound type could still be inferred! My certainty that this is not the only example of that motivates this separate ticket.
// scala 2.10
scala>deff[A](v: Covariant[A]):Array[A] = v match { caseInvariant(xs) => xs }
<console>:10:error: typemismatch;
found : Array[?A1] where type?A1<:A (this is a GADT skolem)
required: Array[A]
Note:?A1<:A, but classArray is invariant in typeT.
You may wish to investigate a wildcard typesuch as `_ <: A`. (SLS3.2.10)
deff[A](v: Covariant[A]):Array[A] = v match { caseInvariant(xs) => xs }
^
scala>deff[A](v: Covariant[A]) = v match { caseInvariant(xs) => xs }
f: [A](v: Covariant[A])Array[A]
@Blaisorblade said:
One does not need an invariant class to trigger this, nor arrays. Here's a reduced version, inspired to what happens in Lightweight Modular Staging:
objectMinimalUnsoundness {
traitExp[+T]
caseclassConst[+T](t: T) extendsExp[T] //Making Const invariant makes no difference.definterp[T](term: Exp[T]):T=
term match {
caseConst(t) => t //This should be rejected...
}
//Because classes like this can be declared:classMyConst(t: String) extendsConst[Any](t) withExp[Boolean]
}
//At the REPL:
scala>importMinimalUnsoundness._importMinimalUnsoundness._
scala>:typeinterp(newMyConst(""))
Boolean
scala> interp(newMyConst(""))
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
In the actual code, I want to prevent declaring classes like MyConst, so that interp is actually accepted, and making Const final is often not an option, but that's another story.
A more complete example - an interpreter for embedded simply-typed lambda-calculus with subtyping:
traitLambda {
traitExp[+T]
caseclassConst[T](t: T) extendsExp[T]
caseclassApp[S, T](fun: Exp[S=>T], arg: Exp[S]) extendsExp[T]
caseclassFun[S, T](body: Exp[S] =>Exp[T]) extendsExp[S=>T]
}
traitInterpextendsLambda {
definterp[T](term: Exp[T]):T=
term match {
caseConst(t) => t //Unsound!caseApp(fun, arg) =>
interp(fun) apply interp(arg) //Unsound!casef: Fun[s, t] =>//(x: s) => interp(f.body(Const(x)))
(((x: s) => interp(f.body(Const(x)))): (s => t)).asInstanceOf[T] //What I have to write//case Fun(body) =>//x => interp(body(Const(x)))
}
}
objectLanguageextendsLambdawithInterp {
//Demonstrate why all the above branches can trigger an error.classMyFun[T]() extendsFun[T, T](x => x) withExp[runtime.AbstractFunction1[T, T]]
classMyApp[T](t: T) extendsApp[Int, List[T]](Fun(x =>Const(List(t))), Const(1)) withExp[Nil.type]
classMyConst(t: String) extendsConst[Any](t) withExp[Boolean]
println(interp(newMyApp(1))) //This works thanks to heap pollution
println("""Executing 'interp(new MyApp(1))' in the REPL won't work - |you'll get a ClassCastException""".stripMargin)
println("""Same for 'interp(new MyConst(""))'""")
}
@Blaisorblade said:
Upon closer observation, the bug you describe is different from the one I mentioned. Moreover, the one you describe cannot be reproduced any more in 2.10.2. Now we get:
scala>deff[A](v: Covariant[A]) = v match { caseInvariant(xs) => xs }
f: [A](v: Covariant[A])Array[_ <:A]
hence the soundness hole you describe does not show up:
scala> f(Invariant(arr):Covariant[Any])(0) =Nil
<console>:13:error: typemismatch;
found : scala.collection.immutable.Nil.typerequired: ?A1 where type?A1
f(Invariant(arr):Covariant[Any])(0) =Nil^
But the type of f is still very bad, and I can use the technique of my example:
@Blaisorblade said:
Amazing. Unlike I said, using an invariant class (here, arrays, which forceInvariant to be invariant) makes a difference.
Usually, to fix the bug I mentioned, the compiler should deduce not ?A1 <: A (as in your error, which I repasted below) but ?A1 >: A. But because arrays are involved, it's not clear whether the compiler can deduce a constraint between ?A1 and A. On pen and paper, I could only deduce that if the most specific solution for v is Covariant[A2], then A2 <: ?A1 and A2 <: A. You can only set A2 = ?A1, giving ?A1 <: A, if you forbid the definition of Bad, as done by one sentence in the paper on case classes and extractors, "Matching objects with patterns", ECOOP 2007, by Burak Emir, Martin Odersky and John Williams. However, forbidding that definition will break valid code that doesn't care about pattern matching.
// scala 2.10
scala>deff[A](v: Covariant[A]):Array[A] = v match { caseInvariant(xs) => xs }
<console>:10:error: typemismatch;
found : Array[?A1] where type?A1<:A (this is a GADT skolem)
required: Array[A]
Note:?A1<:A, but classArray is invariant in typeT.
You may wish to investigate a wildcard typesuch as `_ <: A`. (SLS3.2.10)
deff[A](v: Covariant[A]):Array[A] = v match { caseInvariant(xs) => xs }
^
Welcome to Scala 2.13.5 (OpenJDK 64-Bit Server VM, Java 1.8.0_282).
Type in expressions for evaluation. Or try :help.
scala> trait Covariant[+A]
trait Covariant
scala> val arr = Array("abc")
val arr: Array[String] = Array(abc)
scala> case class Invariant[A](xs: Array[A]) extends Covariant[A]
class Invariant
scala> def f[A](v: Covariant[A]) = v match { case Invariant(xs) => xs }
def f[A](v: Covariant[A]): Array[_ <: A]
scala> f(Invariant(arr): Covariant[Any])(0) = Nil
^
error: type mismatch;
found : Nil.type
required: ?A1 where type ?A1
10 | class MyConst(t: String) extends Const[Any](t) with Exp[Boolean]
| ^
|illegal inheritance: class MyConst inherits conflicting instances of non-variant base trait Exp.
|
| Direct basetype: MinimalUnsoundness.Exp[Boolean]
| Basetype via case class Const: MinimalUnsoundness.Exp[Any]
and
1 |class Bad extends Invariant[Any](Array(1, 2, 3)) with Covariant[Boolean]
| ^
|illegal inheritance: class Bad inherits conflicting instances of non-variant base trait Covariant.
|
| Direct basetype: Covariant[Boolean]
| Basetype via case class Invariant: Covariant[Any]
Activity
scabug commentedon Jan 8, 2013
Imported From: https://issues.scala-lang.org/browse/SI-6944?orig=1
Reporter: @paulp
See #5585, #5189, #6925
scabug commentedon Jan 8, 2013
@paulp said:
Definitely see also #5585.
scabug commentedon Apr 19, 2013
@Blaisorblade said:
One does not need an invariant class to trigger this, nor arrays. Here's a reduced version, inspired to what happens in Lightweight Modular Staging:
In the actual code, I want to prevent declaring classes like
MyConst, so thatinterpis actually accepted, and makingConstfinal is often not an option, but that's another story.A more complete example - an interpreter for embedded simply-typed lambda-calculus with subtyping:
scabug commentedon Jun 11, 2013
@Blaisorblade said:
Upon closer observation, the bug you describe is different from the one I mentioned. Moreover, the one you describe cannot be reproduced any more in 2.10.2. Now we get:
hence the soundness hole you describe does not show up:
But the type of
fis still very bad, and I can use the technique of my example:Should I open another bug and close this one?
scabug commentedon Jun 11, 2013
@Blaisorblade said:
Amazing. Unlike I said, using an invariant class (here, arrays, which force
Invariantto be invariant) makes a difference.Usually, to fix the bug I mentioned, the compiler should deduce not
?A1 <: A(as in your error, which I repasted below) but?A1 >: A. But because arrays are involved, it's not clear whether the compiler can deduce a constraint between?A1andA. On pen and paper, I could only deduce that if the most specific solution forvisCovariant[A2], thenA2 <: ?A1andA2 <: A. You can only setA2 = ?A1, giving?A1 <: A, if you forbid the definition ofBad, as done by one sentence in the paper on case classes and extractors, "Matching objects with patterns", ECOOP 2007, by Burak Emir, Martin Odersky and John Williams. However, forbidding that definition will break valid code that doesn't care about pattern matching.dwijnand commentedon May 14, 2021
Seems like this one is fixed?
lrytz commentedon May 17, 2021
The minimization in #6944 (comment) still runs with a CCE
lrytz commentedon May 17, 2021
Also the one in #6944 (comment)
dwijnand commentedon May 17, 2021
In 3.0.0:
and