Skip to content

Fix existing GADT constraints with introduced pattern-bound symbols #22928

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
Apr 9, 2025
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
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/dotc/core/GadtConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class GadtConstraint private (
reverseMapping = reverseMapping.updated(tv.origin, sym),
)

def replace(param: TypeParamRef, tp: Type)(using Context) =
var constr = constraint
for
poly <- constraint.domainLambdas
paramRef <- poly.paramRefs
do
val entry0 = constr.entry(paramRef)
val entry1 = entry0.substParam(param, tp)
if entry1 ne entry0 then
constr = constr.updateEntry(paramRef, entry1)
withConstraint(constr)

/** Is `sym1` ordered to be less than `sym2`? */
def isLess(sym1: Symbol, sym2: Symbol)(using Context): Boolean =
constraint.isLess(tvarOrError(sym1).origin, tvarOrError(sym2).origin)
Expand Down Expand Up @@ -245,6 +257,9 @@ sealed trait GadtState {
result
}

def replace(param: TypeParamRef, tp: Type)(using Context) =
gadt = gadt.replace(param, tp)

/** See [[ConstraintHandling.approximation]] */
def approximation(sym: Symbol, fromBelow: Boolean, maxLevel: Int = Int.MaxValue)(using Context): Type = {
approximation(gadt.tvarOrError(sym).origin, fromBelow, maxLevel).match
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,15 @@ object Inferencing {
}
}
}
val res = patternBindings.toList.map { (boundSym, _) =>
val res = patternBindings.toList.map { (boundSym, origin) =>
// substitute bounds of pattern bound variables to deal with possible F-bounds
for (wildCard, param) <- patternBindings do
boundSym.info = boundSym.info.substParam(param, wildCard.typeRef)

// also substitute in any GADT bounds
// e.g. in i22879, replace the `T` in `X <: Iterable[T]` with the pattern bound `T$1`
ctx.gadtState.replace(origin, boundSym.typeRef)

boundSym
}

Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i22879.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sealed trait E[S]
final case class I[T, U <: Iterable[T]]() extends E[U]
class Test {
def test[X](a: E[X]): Unit = {
a match {
case I() => ???
}
}
}
Loading