Skip to content

Quickly fix HKT GADT unsoundness issue #9138

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

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 17 additions & 14 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3398,7 +3398,7 @@ class Typer extends Namer

// try an implicit conversion
val prevConstraint = ctx.typerState.constraint
def recover(failure: SearchFailureType) = {
def recover(failure: SearchFailureType): Tree = {
def canTryGADTHealing: Boolean = {
val isDummy = tree.hasAttachment(dummyTreeOfType.IsDummyTree)
tryGadtHealing // allow GADT healing only once to avoid a loop
Expand All @@ -3411,20 +3411,23 @@ class Typer extends Namer
else if (canTryGADTHealing) {
// try recovering with a GADT approximation
val nestedCtx = ctx.fresh.setNewTyperState()
val res =
readapt(
tree = tpd.Typed(tree, TypeTree(Inferencing.approximateGADT(wtp))),
shouldTryGadtHealing = false,
)(using nestedCtx)
if (!nestedCtx.reporter.hasErrors) {
// GADT recovery successful
nestedCtx.typerState.commit()
res
} else {
// otherwise fail with the error that would have been reported without the GADT recovery
err.typeMismatch(tree, pt, failure)
val approximated = Inferencing.approximateGADT(wtp)(using nestedCtx)
if (tree.tpe <:< approximated) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't that check be done in the nestedCtx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's sadly not the part that's making it explode. Not even making the check frozen and doing it in nestedCtx helps. I have no idea why, seems that we will need to take a more scorched-earth type of approach.

val res =
readapt(
tree = tpd.Typed(tree, TypeTree(approximated)),
shouldTryGadtHealing = false,
)(using nestedCtx)

if (!nestedCtx.reporter.hasErrors) {
// GADT recovery successful
nestedCtx.typerState.commit()
return res
}
}
} else err.typeMismatch(tree, pt, failure)
}

err.typeMismatch(tree, pt, failure)
}
if ctx.mode.is(Mode.ImplicitsEnabled) && tree.typeOpt.isValueType then
if pt.isRef(defn.AnyValClass) || pt.isRef(defn.ObjectClass) then
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i9044.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sealed trait Test[+F[_], +A] extends Product with Serializable

object Test {

implicit class Syntax[F[_], A](val self: Test[F, A]) extends AnyVal {

def fold[B](completed: F[A] => B): B = self match {
case Completed(fa) => completed(fa) // error
}
}


final case class Completed[F[_], A](fa: F[A]) extends Test[F, A]
}