@@ -3,7 +3,7 @@ use crate::deriving::generic::*;
33use crate :: deriving:: path_std;
44
55use rustc_ast:: ptr:: P ;
6- use rustc_ast:: { self as ast, Expr , MetaItem } ;
6+ use rustc_ast:: { self as ast, MetaItem } ;
77use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
88use rustc_span:: symbol:: { sym, Ident } ;
99use rustc_span:: Span ;
@@ -51,7 +51,7 @@ pub fn ordering_collapsed(
5151 cx. expr_call_global ( span, fn_cmp_path, vec ! [ lft, rgt] )
5252}
5353
54- pub fn cs_cmp ( cx : & mut ExtCtxt < ' _ > , span : Span , substr : & Substructure < ' _ > ) -> P < Expr > {
54+ pub fn cs_cmp ( cx : & mut ExtCtxt < ' _ > , span : Span , substr : & Substructure < ' _ > ) -> BlockOrExpr {
5555 let test_id = Ident :: new ( sym:: cmp, span) ;
5656 let equals_path = cx. path_global ( span, cx. std_path ( & [ sym:: cmp, sym:: Ordering , sym:: Equal ] ) ) ;
5757
@@ -70,7 +70,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
7070 // cmp => cmp
7171 // }
7272 //
73- cs_fold (
73+ let expr = cs_fold (
7474 // foldr nests the if-elses correctly, leaving the first field
7575 // as the outermost one, and the last as the innermost.
7676 false ,
@@ -79,15 +79,12 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
7979 // ::std::cmp::Ordering::Equal => old,
8080 // cmp => cmp
8181 // }
82-
8382 let new = {
8483 let [ other_f] = other_fs else {
8584 cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" ) ;
8685 } ;
87-
8886 let args =
8987 vec ! [ cx. expr_addr_of( span, self_f) , cx. expr_addr_of( span, other_f. clone( ) ) ] ;
90-
9188 cx. expr_call_global ( span, cmp_path. clone ( ) , args)
9289 } ;
9390
@@ -96,7 +93,21 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
9693
9794 cx. expr_match ( span, new, vec ! [ eq_arm, neq_arm] )
9895 } ,
99- cx. expr_path ( equals_path. clone ( ) ) ,
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+ } ,
100111 Box :: new ( |cx, span, tag_tuple| {
101112 if tag_tuple. len ( ) != 2 {
102113 cx. span_bug ( span, "not exactly 2 arguments in `derive(Ord)`" )
@@ -107,5 +118,6 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
107118 cx,
108119 span,
109120 substr,
110- )
121+ ) ;
122+ BlockOrExpr :: new_expr ( expr)
111123}
0 commit comments