This commit is contained in:
yukang 2023-06-19 11:03:48 +08:00
parent 0b20096eff
commit aba1cf159f

View File

@ -34,14 +34,14 @@ enum Issue {
Permutation(Vec<Option<usize>>),
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum Compatibility<'tcx> {
Compatible,
Incompatible(Option<TypeError<'tcx>>),
}
/// Similar to `Issue`, but contains some extra information
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Error<'tcx> {
/// The provided argument is the invalid type for the expected input
Invalid(ProvidedIdx, ExpectedIdx, Compatibility<'tcx>),
@ -55,6 +55,34 @@ pub(crate) enum Error<'tcx> {
Permutation(Vec<(ExpectedIdx, ProvidedIdx)>),
}
impl Ord for Error<'_> {
fn cmp(&self, other: &Self) -> Ordering {
let key = |error: &Error<'_>| -> usize {
match error {
Error::Invalid(..) => 0,
Error::Extra(_) => 1,
Error::Missing(_) => 2,
Error::Swap(..) => 3,
Error::Permutation(..) => 4,
}
};
match (self, other) {
(Error::Invalid(a, _, _), Error::Invalid(b, _, _)) => a.cmp(b),
(Error::Extra(a), Error::Extra(b)) => a.cmp(b),
(Error::Missing(a), Error::Missing(b)) => a.cmp(b),
(Error::Swap(a, b, ..), Error::Swap(c, d, ..)) => a.cmp(c).then(b.cmp(d)),
(Error::Permutation(a), Error::Permutation(b)) => a.cmp(b),
_ => key(self).cmp(&key(other)),
}
}
}
impl PartialOrd for Error<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
pub(crate) struct ArgMatrix<'tcx> {
/// Maps the indices in the `compatibility_matrix` rows to the indices of
/// the *user provided* inputs
@ -378,11 +406,7 @@ pub(crate) fn find_errors(
// sort errors with same type by the order they appear in the source
// so that suggestion will be handled properly, see #112507
errors.sort_by(|a, b| match (a, b) {
(Error::Missing(i), Error::Missing(j)) => i.cmp(j),
(Error::Extra(i), Error::Extra(j)) => i.cmp(j),
_ => Ordering::Equal,
});
errors.sort();
return (errors, matched_inputs);
}
}