@@ -34,14 +34,14 @@ enum Issue {
3434 Permutation ( Vec < Option < usize > > ) ,
3535}
3636
37- #[ derive( Clone , Debug ) ]
37+ #[ derive( Clone , Debug , Eq , PartialEq ) ]
3838pub ( crate ) enum Compatibility < ' tcx > {
3939 Compatible ,
4040 Incompatible ( Option < TypeError < ' tcx > > ) ,
4141}
4242
4343/// Similar to `Issue`, but contains some extra information
44- #[ derive( Debug ) ]
44+ #[ derive( Debug , PartialEq , Eq ) ]
4545pub ( crate ) enum Error < ' tcx > {
4646 /// The provided argument is the invalid type for the expected input
4747 Invalid ( ProvidedIdx , ExpectedIdx , Compatibility < ' tcx > ) ,
@@ -55,6 +55,34 @@ pub(crate) enum Error<'tcx> {
5555 Permutation ( Vec < ( ExpectedIdx , ProvidedIdx ) > ) ,
5656}
5757
58+ impl Ord for Error < ' _ > {
59+ fn cmp ( & self , other : & Self ) -> Ordering {
60+ let key = |error : & Error < ' _ > | -> usize {
61+ match error {
62+ Error :: Invalid ( ..) => 0 ,
63+ Error :: Extra ( _) => 1 ,
64+ Error :: Missing ( _) => 2 ,
65+ Error :: Swap ( ..) => 3 ,
66+ Error :: Permutation ( ..) => 4 ,
67+ }
68+ } ;
69+ match ( self , other) {
70+ ( Error :: Invalid ( a, _, _) , Error :: Invalid ( b, _, _) ) => a. cmp ( b) ,
71+ ( Error :: Extra ( a) , Error :: Extra ( b) ) => a. cmp ( b) ,
72+ ( Error :: Missing ( a) , Error :: Missing ( b) ) => a. cmp ( b) ,
73+ ( Error :: Swap ( a, b, ..) , Error :: Swap ( c, d, ..) ) => a. cmp ( c) . then ( b. cmp ( d) ) ,
74+ ( Error :: Permutation ( a) , Error :: Permutation ( b) ) => a. cmp ( b) ,
75+ _ => key ( self ) . cmp ( & key ( other) ) ,
76+ }
77+ }
78+ }
79+
80+ impl PartialOrd for Error < ' _ > {
81+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
82+ Some ( self . cmp ( other) )
83+ }
84+ }
85+
5886pub ( crate ) struct ArgMatrix < ' tcx > {
5987 /// Maps the indices in the `compatibility_matrix` rows to the indices of
6088 /// the *user provided* inputs
@@ -378,11 +406,7 @@ impl<'tcx> ArgMatrix<'tcx> {
378406
379407 // sort errors with same type by the order they appear in the source
380408 // so that suggestion will be handled properly, see #112507
381- errors. sort_by ( |a, b| match ( a, b) {
382- ( Error :: Missing ( i) , Error :: Missing ( j) ) => i. cmp ( j) ,
383- ( Error :: Extra ( i) , Error :: Extra ( j) ) => i. cmp ( j) ,
384- _ => Ordering :: Equal ,
385- } ) ;
409+ errors. sort ( ) ;
386410 return ( errors, matched_inputs) ;
387411 }
388412}
0 commit comments