Skip to content

Fix #1891: Don't add redundant constraint #1893

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
Jan 11, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ trait ConstraintHandling {
b match {
case b: AndOrType => occursIn(b.tp1) || occursIn(b.tp2)
case b: TypeVar => occursIn(b.origin)
case b: TermRef => occursIn(b.underlying)
case _ => false
}
}
Expand Down
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,22 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
thirdTryNamed(tp1, tp2)
case tp2: PolyParam =>
def comparePolyParam =
(ctx.mode is Mode.TypevarsMissContext) ||
isSubTypeWhenFrozen(tp1, bounds(tp2).lo) || {
(ctx.mode is Mode.TypevarsMissContext) || {
val alwaysTrue =
// The following condition is carefully formulated to catch all cases
// where the subtype relation is true without needing to add a constraint
// It's tricky because we might need to either appriximate tp2 by its
// lower bound or else widen tp1 and check that the result is a subtype of tp2.
Copy link
Member

Choose a reason for hiding this comment

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

I don't see a widening of tp1 in the code below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The widening is done by all the other parts of subtyping. The important part is not to narrow the lower bound of tp2 before we had a chance to widen tp1.

// So if the constraint is not yet frozen, we do the same comparison again
// with a frozen constraint, which means that we get a chance to do the
// widening in `fourthTry` before adding to the constraint.
if (frozenConstraint || alwaysFluid) isSubType(tp1, bounds(tp2).lo)
else isSubTypeWhenFrozen(tp1, tp2)
alwaysTrue || {
if (canConstrain(tp2)) addConstraint(tp2, tp1.widenExpr, fromBelow = true)
else fourthTry(tp1, tp2)
}
}
comparePolyParam
case tp2: RefinedType =>
def compareRefinedSlow: Boolean = {
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i1891.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Test {
class CC2[A, B](a: A, b: B)

type T2[A, B] = CC2[A, B]

class ArrowAssoc[A](val self: A) {
@inline def f[B](y: B): CC2[A, B] = new CC2(self, y)
}

def foo = (new ArrowAssoc(1)).f(2)
}