@@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
2
2
use crate :: deriving:: generic:: * ;
3
3
use crate :: deriving:: path_std;
4
4
5
- use rustc_ast:: ptr:: P ;
6
- use rustc_ast:: { self as ast, MetaItem } ;
5
+ use rustc_ast:: MetaItem ;
7
6
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
8
7
use rustc_span:: symbol:: { sym, Ident } ;
9
8
use rustc_span:: Span ;
@@ -28,7 +27,7 @@ pub fn expand_deriving_ord(
28
27
name: sym:: cmp,
29
28
generics: Bounds :: empty( ) ,
30
29
explicit_self: true ,
31
- args : vec![ ( self_ref( ) , sym:: other) ] ,
30
+ nonself_args : vec![ ( self_ref( ) , sym:: other) ] ,
32
31
ret_ty: Path ( path_std!( cmp:: Ordering ) ) ,
33
32
attributes: attrs,
34
33
unify_fieldless_variants: true ,
@@ -40,84 +39,54 @@ pub fn expand_deriving_ord(
40
39
trait_def. expand ( cx, mitem, item, push)
41
40
}
42
41
43
- pub fn ordering_collapsed (
44
- cx : & mut ExtCtxt < ' _ > ,
45
- span : Span ,
46
- self_arg_tags : & [ Ident ] ,
47
- ) -> P < ast:: Expr > {
48
- let lft = cx. expr_addr_of ( span, cx. expr_ident ( span, self_arg_tags[ 0 ] ) ) ;
49
- let rgt = cx. expr_addr_of ( span, cx. expr_ident ( span, self_arg_tags[ 1 ] ) ) ;
50
- let fn_cmp_path = cx. std_path ( & [ sym:: cmp, sym:: Ord , sym:: cmp] ) ;
51
- cx. expr_call_global ( span, fn_cmp_path, vec ! [ lft, rgt] )
52
- }
53
-
54
42
pub fn cs_cmp ( cx : & mut ExtCtxt < ' _ > , span : Span , substr : & Substructure < ' _ > ) -> BlockOrExpr {
55
43
let test_id = Ident :: new ( sym:: cmp, span) ;
56
- let equals_path = cx. path_global ( span, cx. std_path ( & [ sym:: cmp, sym:: Ordering , sym:: Equal ] ) ) ;
57
-
44
+ let equal_path = cx. path_global ( span, cx. std_path ( & [ sym:: cmp, sym:: Ordering , sym:: Equal ] ) ) ;
58
45
let cmp_path = cx. std_path ( & [ sym:: cmp, sym:: Ord , sym:: cmp] ) ;
59
46
60
47
// Builds:
61
48
//
62
- // match ::std::cmp::Ord::cmp(&self_field1, &other_field1) {
63
- // ::std::cmp::Ordering::Equal =>
64
- // match ::std::cmp::Ord::cmp(&self_field2, &other_field2) {
65
- // ::std::cmp::Ordering::Equal => {
66
- // ...
49
+ // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
50
+ // ::std::cmp::Ordering::Equal =>
51
+ // ::core::cmp::Ord::cmp(&self.y, &other.y),
52
+ // cmp => cmp,
67
53
// }
68
- // cmp => cmp
69
- // },
70
- // cmp => cmp
71
- // }
72
- //
73
54
let expr = cs_fold (
74
55
// foldr nests the if-elses correctly, leaving the first field
75
56
// as the outermost one, and the last as the innermost.
76
57
false ,
77
- |cx, span, old, self_f, other_fs| {
78
- // match new {
79
- // ::std::cmp::Ordering::Equal => old,
80
- // cmp => cmp
81
- // }
82
- let new = {
83
- let [ other_f] = other_fs else {
84
- cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" ) ;
85
- } ;
86
- let args =
87
- vec ! [ cx. expr_addr_of( span, self_f) , cx. expr_addr_of( span, other_f. clone( ) ) ] ;
88
- cx. expr_call_global ( span, cmp_path. clone ( ) , args)
89
- } ;
90
-
91
- let eq_arm = cx. arm ( span, cx. pat_path ( span, equals_path. clone ( ) ) , old) ;
92
- let neq_arm = cx. arm ( span, cx. pat_ident ( span, test_id) , cx. expr_ident ( span, test_id) ) ;
93
-
94
- cx. expr_match ( span, new, vec ! [ eq_arm, neq_arm] )
95
- } ,
96
- |cx, args| match args {
97
- Some ( ( span, self_f, other_fs) ) => {
98
- let new = {
99
- let [ other_f] = other_fs else {
100
- cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" ) ;
101
- } ;
102
- let args =
103
- vec ! [ cx. expr_addr_of( span, self_f) , cx. expr_addr_of( span, other_f. clone( ) ) ] ;
104
- cx. expr_call_global ( span, cmp_path. clone ( ) , args)
105
- } ;
106
-
107
- new
108
- }
109
- None => cx. expr_path ( equals_path. clone ( ) ) ,
110
- } ,
111
- Box :: new ( |cx, span, tag_tuple| {
112
- if tag_tuple. len ( ) != 2 {
113
- cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" )
114
- } else {
115
- ordering_collapsed ( cx, span, tag_tuple)
116
- }
117
- } ) ,
118
58
cx,
119
59
span,
120
60
substr,
61
+ |cx, fold| match fold {
62
+ CsFold :: Single ( field) => {
63
+ let [ other_expr] = & field. other_selflike_exprs [ ..] else {
64
+ cx. span_bug ( field. span , "not exactly 2 arguments in `derive(Ord)`" ) ;
65
+ } ;
66
+ let args = vec ! [
67
+ cx. expr_addr_of( field. span, field. self_expr. clone( ) ) ,
68
+ cx. expr_addr_of( field. span, other_expr. clone( ) ) ,
69
+ ] ;
70
+ cx. expr_call_global ( field. span , cmp_path. clone ( ) , args)
71
+ }
72
+ CsFold :: Combine ( span, expr1, expr2) => {
73
+ let eq_arm = cx. arm ( span, cx. pat_path ( span, equal_path. clone ( ) ) , expr1) ;
74
+ let neq_arm =
75
+ cx. arm ( span, cx. pat_ident ( span, test_id) , cx. expr_ident ( span, test_id) ) ;
76
+ cx. expr_match ( span, expr2, vec ! [ eq_arm, neq_arm] )
77
+ }
78
+ CsFold :: Fieldless => cx. expr_path ( equal_path. clone ( ) ) ,
79
+ CsFold :: EnumNonMatching ( span, tag_tuple) => {
80
+ if tag_tuple. len ( ) != 2 {
81
+ cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" )
82
+ } else {
83
+ let lft = cx. expr_addr_of ( span, cx. expr_ident ( span, tag_tuple[ 0 ] ) ) ;
84
+ let rgt = cx. expr_addr_of ( span, cx. expr_ident ( span, tag_tuple[ 1 ] ) ) ;
85
+ let fn_cmp_path = cx. std_path ( & [ sym:: cmp, sym:: Ord , sym:: cmp] ) ;
86
+ cx. expr_call_global ( span, fn_cmp_path, vec ! [ lft, rgt] )
87
+ }
88
+ }
89
+ } ,
121
90
) ;
122
91
BlockOrExpr :: new_expr ( expr)
123
92
}
0 commit comments