Skip to content

WIP: GADT-aware patmat exhaustivity checking #3454

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 6 commits 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dotty.tools.dotc.transform.patmat

import dotty.tools.dotc.core.Contexts.Context

/** Checks satisfiability of constraints in an extremely naive way */
object NaiveConstraintChecker {
def hasUnsatisfiableConstraints(s: ConstrainedSpace)(implicit ctx: Context): Boolean =
!termsPotentiallySatisfiable(s.termConstraints) ||
!typesPotentiallySatisfiable(s.typeConstraints)

def termsPotentiallySatisfiable(constraints: List[TermConstraint]): Boolean =
constraints.forall {
case Dummy | Neg(Dummy) => true
case AlwaysSatisfied => true
case Neg(AlwaysSatisfied) => false
}

@annotation.tailrec
def typesPotentiallySatisfiable(constraints: List[TypeConstraint])(implicit ctx: Context): Boolean = {
def comparePair(tpeq1: TypeEquality, tpeq2: TypeEquality) =
if (tpeq1.tp1 =:= tpeq2.tp1) tpeq1.tp2 =:= tpeq2.tp2 else true

@annotation.tailrec
def compareOneVsList(tpeq: TypeEquality, constraints: List[TypeConstraint]): Boolean =
constraints match {
case Nil => true
case (tpeq2: TypeEquality) :: rest =>
comparePair(tpeq, tpeq2) && compareOneVsList(tpeq, rest)
}

constraints match {
case Nil => true
case (tpeq: TypeEquality) :: tail =>
compareOneVsList(tpeq, tail) && typesPotentiallySatisfiable(tail)
}
}
}
Loading