Skip to content

Simplify merging in lub/glb, avoid unnecessary constraints #9053

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
Jun 1, 2020
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
6 changes: 2 additions & 4 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1983,8 +1983,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
/** Merge `t1` into `tp2` if t1 is a subtype of some &-summand of tp2.
*/
private def mergeIfSub(tp1: Type, tp2: Type): Type =
if (isSubTypeWhenFrozen(tp1, tp2))
if (isSubTypeWhenFrozen(tp2, tp1)) tp2 else tp1 // keep existing type if possible
if (isSubTypeWhenFrozen(tp1, tp2)) tp1
else tp2 match {
case tp2 @ AndType(tp21, tp22) =>
val lower1 = mergeIfSub(tp1, tp21)
Expand All @@ -2004,8 +2003,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
* @param canConstrain If true, new constraints might be added to make the merge possible.
*/
private def mergeIfSuper(tp1: Type, tp2: Type, canConstrain: Boolean): Type =
if (isSubType(tp2, tp1, whenFrozen = !canConstrain))
if (isSubType(tp1, tp2, whenFrozen = !canConstrain)) tp2 else tp1 // keep existing type if possible
if (isSubType(tp2, tp1, whenFrozen = !canConstrain)) tp1
else tp2 match {
case tp2 @ OrType(tp21, tp22) =>
val higher1 = mergeIfSuper(tp1, tp21, canConstrain)
Expand Down
3 changes: 1 addition & 2 deletions tests/neg/i4564.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ class BaseCNSP[T] {
}

object ClashNoSigPoly extends BaseCNSP[Int]
// TODO: improve error message
case class ClashNoSigPoly private(x: Int) // error: found: ClashNoSigPoly required: Nothing
case class ClashNoSigPoly private(x: Int)
11 changes: 11 additions & 0 deletions tests/pos/merge-constraint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Hi
class Lo extends Hi

object Test {
def foo[T, U <: T](t: T, f: T => U): U = ???

def test(hi: Hi, lo: Lo): Unit = {
val ret = foo(hi, x => lo) // This used to infer U := Hi
val y: Lo = ret
}
}