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
Incorrect inferred type in contravariant position #4881
Comments
Imported From: https://issues.scala-lang.org/browse/SI-4881?orig=1 |
@retronym said: scala> contraLBUB
res9: Contra[A] = null
scala> contraLB
res10: Contra[Any] = null So the question is, does "Case 3: Methods" explain these? scala> contraLBUB()
res11: Contra[C] = null
scala> contraLB()
res12: Contra[C] = null |
@Alefas said: foo
foo() |
@paulp said:
|
@retronym said: diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.s
index 688dcd9..a34b93d 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala
@@ -575,8 +575,9 @@ trait Infer {
"argument expression's type is not compatible with formal parameter type" + foundReqMsg(tp1, pt1))
}
}
val targs = solvedTypes(
- tvars, tparams, tparams map varianceInTypes(formals),
+ tvars, tparams, tparams map varianceInTypes(restpe +: formals),
false, lubDepth(formals) max lubDepth(argtpes)
)
adjustTypeArgs(tparams, tvars, targs, restpe) Compare with: |
@adriaanm said (edited on Jun 21, 2012 8:09:59 AM UTC): |
@adriaanm said: |
@Blaisorblade said: def f[T, Coll <: Traversable[T]](p0: String)(p1: Traversable[T]): (Coll => Traversable[T]) = ??? //Here Coll would be maximized.
def f[T, Coll <: Traversable[T]](p0: String)(p1: Traversable[T])(p2: Coll): Traversable[T] = ??? //Here Coll would be minimized. This is if I get the new expected behavior right, which says that (ignoring mixed variance) a type argument must be minimized (made most specific) if it appears either: |
According to Language specification (6.26.4 Case 2):
If several substitutions exist,
local-type inference will choose for each type variable ai a minimal or maximal type
Ti of the solution space. A maximal type Ti will be chosen if the type parameter ai
appears contravariantly (§4.5) in the type T of the expression. A minimal type Ti
will be chosen in all other situations, i.e. if the variable appears covariantly, nonvariantly
or not at all in the type T.
What we can see here. Value has Type Contra[a]. So position of a is contravariant. There is no covariant or invariant positions of a => maximal solution should be chosen.
The text was updated successfully, but these errors were encountered: