Skip to content

Fix #11234: Avoid cycles in unifying F-bounded type parameters #11237

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 4, 2021
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
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
order(this, param1, param2).checkNonCyclic()

def unify(p1: TypeParamRef, p2: TypeParamRef)(using Context): This =
val p1Bounds = (nonParamBounds(p1) & nonParamBounds(p2)).substParam(p2, p1)
val bound1 = nonParamBounds(p1).substParam(p2, p1)
val bound2 = nonParamBounds(p2).substParam(p2, p1)
val p1Bounds = bound1 & bound2
updateEntry(p1, p1Bounds).replace(p2, p1)

// ---------- Replacements and Removals -------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
}
def compareTypeParamRef =
assumedTrue(tp1) ||
tp2.match {
case tp2: TypeParamRef => constraint.isLess(tp1, tp2)
case _ => false
} ||
isSubTypeWhenFrozen(bounds(tp1).hi, tp2) || {
if (canConstrain(tp1) && !approx.high)
addConstraint(tp1, tp2, fromBelow = false) && flagNothingBound
Expand Down Expand Up @@ -540,11 +544,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// widening in `fourthTry` before adding to the constraint.
if (frozenConstraint) recur(tp1, bounds(tp2).lo)
else isSubTypeWhenFrozen(tp1, tp2)
alwaysTrue ||
frozenConstraint && (tp1 match {
case tp1: TypeParamRef => constraint.isLess(tp1, tp2)
case _ => false
}) || {
alwaysTrue || {
if (canConstrain(tp2) && !approx.low)
addConstraint(tp2, tp1.widenExpr, fromBelow = true)
else fourthTry
Expand Down Expand Up @@ -2122,7 +2122,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
}

private def andTypeGen(tp1: Type, tp2: Type, op: (Type, Type) => Type,
original: (Type, Type) => Type = _ & _, isErased: Boolean = ctx.erasedTypes): Type = trace(s"glb(${tp1.show}, ${tp2.show})", subtyping, show = true) {
original: (Type, Type) => Type = _ & _, isErased: Boolean = ctx.erasedTypes): Type = trace(s"andTypeGen(${tp1.show}, ${tp2.show})", subtyping, show = true) {
val t1 = distributeAnd(tp1, tp2)
if (t1.exists) t1
else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotc
package core

import Contexts._, Types._, Symbols._, Names._, Flags._
import Denotations._, SymDenotations._
import SymDenotations._
import util.Spans._
import util.Stats
import NameKinds.DepParamName
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i11234.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
trait Foo[A <: Foo[A]]
trait FooCreator[A <: Foo[A]] {
def createFoo(): A
}

trait FooWrapper {
type A <: Foo[A]
def foo: A
}

object FooWrapper {
def apply[A0 <: Foo[A0]](toWrap: A0): FooWrapper { type A = A0 } = new FooWrapper {
type A = A0
def foo: A0 = toWrap
}
}

def error(fooWrapper: FooWrapper, processor: [A <: Foo[A]] => A => A): FooWrapper =
FooWrapper(processor(fooWrapper.foo))