Skip to content

safe_&: handle higher-kinded arguments like regular & #9470

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
Aug 10, 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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ class TypeComparer(using val comparerCtx: Context) extends ConstraintHandling wi
*
* [X1, ..., Xn] -> op(tp1[X1, ..., Xn], tp2[X1, ..., Xn])
*/
private def liftIfHK(tp1: Type, tp2: Type,
def liftIfHK(tp1: Type, tp2: Type,
op: (Type, Type) => Type, original: (Type, Type) => Type, combineVariance: (Variance, Variance) => Variance) = {
val tparams1 = tp1.typeParams
val tparams2 = tp2.typeParams
Expand Down
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,11 @@ object Types {
*/
def safe_& (that: Type)(using Context): Type = (this, that) match {
case (TypeBounds(lo1, hi1), TypeBounds(lo2, hi2)) =>
TypeBounds(OrType(lo1.stripLazyRef, lo2.stripLazyRef), AndType(hi1.stripLazyRef, hi2.stripLazyRef))
case _ => this & that
TypeBounds(
OrType.makeHk(lo1.stripLazyRef, lo2.stripLazyRef),
AndType.makeHk(hi1.stripLazyRef, hi2.stripLazyRef))
case _ =>
this & that
}

/** `this & that`, but handle CyclicReferences by falling back to `safe_&`.
Expand Down Expand Up @@ -2934,6 +2937,10 @@ object Types {
tp2
else
if (checkValid) apply(tp1, tp2) else unchecked(tp1, tp2)

/** Like `make`, but also supports higher-kinded types as argument */
def makeHk(tp1: Type, tp2: Type)(using Context): Type =
ctx.typeComparer.liftIfHK(tp1, tp2, AndType.make(_, _, checkValid = false), makeHk, _ | _)
}

abstract case class OrType(tp1: Type, tp2: Type) extends AndOrType {
Expand Down Expand Up @@ -3020,6 +3027,10 @@ object Types {
def make(tp1: Type, tp2: Type)(using Context): Type =
if (tp1 eq tp2) tp1
else apply(tp1, tp2)

/** Like `make`, but also supports higher-kinded types as argument */
def makeHk(tp1: Type, tp2: Type)(using Context): Type =
ctx.typeComparer.liftIfHK(tp1, tp2, OrType(_, _), makeHk, _ & _)
}

/** An extractor object to pattern match against a nullable union.
Expand Down
9 changes: 9 additions & 0 deletions tests/pending/pos/i9346.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Foo[+A] {
type Repr[+O] <: Foo[O] {
type Repr[+OO] = Foo.this.Repr[OO]
}

def foo: Repr[A]

def bar: Repr[A] = this.foo.foo
}