diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-23 08:50:13 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-23 08:50:13 -0700 |
commit | 3bcd6f8cb5ad63dff3514fd2d74fdcc23959bec2 (patch) | |
tree | be08a99fb120ab98e09f090ba4d3ec48e6049e8a /src | |
parent | 7b62eeea86c62f5e068c366065f8f4c2e6624eb7 (diff) | |
parent | 5c5e8d4dcd151a6e2bf9e7c259c618b9b4eff00f (diff) | |
download | scala-3bcd6f8cb5ad63dff3514fd2d74fdcc23959bec2.tar.gz scala-3bcd6f8cb5ad63dff3514fd2d74fdcc23959bec2.tar.bz2 scala-3bcd6f8cb5ad63dff3514fd2d74fdcc23959bec2.zip |
Merge pull request #975 from adriaanm/ticket-4881b
SI-4881 infer variance from formals, then result
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Infer.scala | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index 960c210649..47cf80807a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -445,7 +445,7 @@ trait Infer { val tvars = tparams map freshVar if (isConservativelyCompatible(restpe.instantiateTypeParams(tparams, tvars), pt)) map2(tparams, tvars)((tparam, tvar) => - instantiateToBound(tvar, varianceInTypes(formals)(tparam))) + instantiateToBound(tvar, inferVariance(formals, restpe)(tparam))) else tvars map (tvar => WildcardType) } @@ -575,13 +575,24 @@ 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 inferVariance(formals, restpe), false, lubDepth(formals) max lubDepth(argtpes) ) adjustTypeArgs(tparams, tvars, targs, restpe) } + /** Determine which variance to assume for the type paraneter. We first chose the variance + * that minimizes any formal parameters. If that is still undetermined, because the type parameter + * does not appear as a formal parameter type, then we pick the variance so that it minimizes the + * method's result type instead. + */ + private def inferVariance(formals: List[Type], restpe: Type)(tparam: Symbol): Int = { + val v = varianceInTypes(formals)(tparam) + if (v != VarianceFlags) v else varianceInType(restpe)(tparam) + } + private[typechecker] def followApply(tp: Type): Type = tp match { case NullaryMethodType(restp) => val restp1 = followApply(restp) |