Skip to content

Fix #1590: Eliminate wildcards when approximating a type #1592

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 2 commits into from
Oct 16, 2016
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
24 changes: 23 additions & 1 deletion src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ trait ConstraintHandling {
/** Solve constraint set for given type parameter `param`.
* If `fromBelow` is true the parameter is approximated by its lower bound,
* otherwise it is approximated by its upper bound. However, any occurrences
* of the parameter in a refinement somewhere in the bound are removed.
* of the parameter in a refinement somewhere in the bound are removed. Also
* wildcard types in bounds are approximated by their upper or lower bounds.
* (Such occurrences can arise for F-bounded types).
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved before the sentence you added

* The constraint is left unchanged.
* @return the instantiating type
Expand All @@ -174,6 +175,27 @@ trait ConstraintHandling {
def apply(tp: Type) = mapOver {
tp match {
case tp: RefinedType if param occursIn tp.refinedInfo => tp.parent
case tp: WildcardType =>
val bounds = tp.optBounds.orElse(TypeBounds.empty).bounds
// Try to instantiate the wildcard to a type that is known to conform to it.
// This means:
// If fromBelow is true, we minimize the type overall
// Hence, if variance < 0, pick the maximal safe type: bounds.lo
// (i.e. the whole bounds range is over the type)
// if variance > 0, pick the minimal safe type: bounds.hi
// (i.e. the whole bounds range is under the type)
// if variance == 0, pick bounds.lo anyway (this is arbitrary but in line with
Copy link
Member

Choose a reason for hiding this comment

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

I think you mean bounds.hi here, or the code is wrong, in any case I'd love to see some tests for all these cases :).

Copy link
Member

Choose a reason for hiding this comment

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

Ah nevermind, I misread the condition.

// the principle that we pick the smaller type when in doubt).
// If fromBelow is false, we maximize the type overall and reverse the bounds
// if variance != 0. For variance == 0, we still minimize.
// In summary we pick the bound given by this table:
//
// variance | -1 0 1
// ------------------------
// from below | lo lo hi
// from above | hi lo lo
//
if (variance == 0 || fromBelow == (variance < 0)) bounds.lo else bounds.hi
case _ => tp
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i1590.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
case class W[T](seq: Option[Option[T]] = Option.empty)
object W {
def apply[T] = new W[T]()
}

case class V[T](vv: W[W[T]] = W.apply)
object Test {
W[Int]()
// V[Int]() fails in scalac and dotty: both instantiate the vv-default to W[Nothing]
}