about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2023-06-19 11:03:48 +0800
committeryukang <moorekang@gmail.com>2023-06-19 11:03:48 +0800
commitaba1cf159ffc53c72149703bf4f7cf072c3dd65f (patch)
tree5e09624b2a8007dcf7cfd731597d0e4e86af8556
parent0b20096eff0e12690e63be97647b50383402e57c (diff)
downloadrust-aba1cf159ffc53c72149703bf4f7cf072c3dd65f.tar.gz
rust-aba1cf159ffc53c72149703bf4f7cf072c3dd65f.zip
fix sort
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs38
1 files changed, 31 insertions, 7 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
index e535d244c89..7aadb95d939 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
@@ -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 @@ impl<'tcx> ArgMatrix<'tcx> {
 
         // 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);
     }
 }