Skip to content

Fix #4032: Fix direction of instantiation. #4059

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

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Constraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ abstract class Constraint extends Showable {
/** Check whether predicate holds for all parameters in constraint */
def forallParams(p: TypeParamRef => Boolean): Boolean

/** Perform operation `op` on all typevars, or only on uninstantiated
* typevars, depending on whether `uninstOnly` is set or not.
*/
/** Perform operation `op` on all typevars that do not have their `inst` field set. */
def foreachTypeVar(op: TypeVar => Unit): Unit

/** The uninstantiated typevars of this constraint */
/** The uninstantiated typevars of this constraint, which still have a bounds constraint
*/
def uninstVars: collection.Seq[TypeVar]

/** The weakest constraint that subsumes both this constraint and `other` */
Expand Down
26 changes: 25 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ object Inferencing {

propagate(accu(SimpleIdentityMap.Empty, tp))
}

private def varianceInContext(tvar: TypeVar)(implicit ctx: Context): FlagSet = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documentation for this method

object accu extends TypeAccumulator[FlagSet] {
def apply(fs: FlagSet, t: Type): FlagSet =
if (fs == EmptyFlags) fs
else if (t eq tvar)
if (variance > 0) fs &~ Contravariant
else if (variance < 0) fs &~ Covariant
else EmptyFlags
else foldOver(fs, t)
}
val constraint = ctx.typerState.constraint
val tparam = tvar.origin
(VarianceFlags /: constraint.uninstVars) { (fs, tv) =>
if ((tv `eq` tvar) || (fs == EmptyFlags)) fs
else {
val otherParam = tv.origin
val fs1 = if (constraint.isLess(tparam, otherParam)) fs &~ Covariant else fs
val fs2 = if (constraint.isLess(otherParam, tparam)) fs1 &~ Contravariant else fs1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odersky We were wondering about

We take this into account to instantiate the variable so that it narrows the overall constraint the least, defaulting again to lower bound if there is not best direction.

Are you trying to give the biggest type to the typing context? If so, do you know the variance of tv's occurrences in the typing context? It could occur in either covariant or contravariant positions, and that affects the polarity of occurrences of tvar in tv's bounds, but it doesn't look like tvar's polarity is accounted for...

accu(fs2, constraint.entry(otherParam))
}
}
}
}

trait Inferencing { this: Typer =>
Expand Down Expand Up @@ -389,7 +412,8 @@ trait Inferencing { this: Typer =>
if (!(vs contains tvar) && qualifies(tvar)) {
typr.println(s"instantiating non-occurring ${tvar.show} in ${tp.show} / $tp")
ensureConstrained()
tvar.instantiate(fromBelow = tvar.hasLowerBound)
tvar.instantiate(
fromBelow = tvar.hasLowerBound || !varianceInContext(tvar).is(Covariant))
}
}
if (constraint.uninstVars exists qualifies) interpolate()
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i4032.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import scala.concurrent.Future

class Gen[+T] {
def map[U](f: T => U): Gen[U] = ???
}

object Gen {
def oneOf[T](t0: T, t1: T): Gen[T] = ??? // Compile with this line commented
def oneOf[T](g0: Gen[T], g1: Gen[T]): Gen[T] = ???
}

class Arbitrary[T]

object Arbitrary {
def arbitrary[T]: Gen[T] = ???

def arbFuture[X]: Gen[Future[X]] =
Gen.oneOf(arbitrary[Future[X]], arbitrary[Throwable].map(Future.failed))
}