Skip to content

Commit 559398f

Browse files
committed
Fix some inconsistencies.
This makes `cs_cmp`, `cs_partial_cmp`, and `cs_op` (for `PartialEq`) more similar. It also fixes some out of date comments.
1 parent 65d0bfb commit 559398f

File tree

3 files changed

+31
-58
lines changed

3 files changed

+31
-58
lines changed

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

+13-29
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
44

5-
use rustc_ast::ptr::P;
6-
use rustc_ast::{self as ast, MetaItem};
5+
use rustc_ast::MetaItem;
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::{sym, Ident};
98
use rustc_span::Span;
@@ -40,43 +39,25 @@ pub fn expand_deriving_ord(
4039
trait_def.expand(cx, mitem, item, push)
4140
}
4241

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-
5442
pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
5543
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]));
5845
let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
5946

6047
// Builds:
6148
//
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-
// ...
67-
// }
68-
// cmp => cmp
69-
// },
70-
// cmp => cmp
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,
7153
// }
72-
//
7354
let expr = cs_fold(
7455
// foldr nests the if-elses correctly, leaving the first field
7556
// as the outermost one, and the last as the innermost.
7657
false,
7758
|cx, span, old, self_expr, other_selflike_exprs| {
7859
// match new {
79-
// ::std::cmp::Ordering::Equal => old,
60+
// ::core::cmp::Ordering::Equal => old,
8061
// cmp => cmp
8162
// }
8263
let new = {
@@ -90,7 +71,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
9071
cx.expr_call_global(span, cmp_path.clone(), args)
9172
};
9273

93-
let eq_arm = cx.arm(span, cx.pat_path(span, equals_path.clone()), old);
74+
let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), old);
9475
let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
9576

9677
cx.expr_match(span, new, vec![eq_arm, neq_arm])
@@ -110,13 +91,16 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
11091

11192
new
11293
}
113-
None => cx.expr_path(equals_path.clone()),
94+
None => cx.expr_path(equal_path.clone()),
11495
},
11596
Box::new(|cx, span, tag_tuple| {
11697
if tag_tuple.len() != 2 {
11798
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
11899
} else {
119-
ordering_collapsed(cx, span, tag_tuple)
100+
let lft = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[0]));
101+
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[1]));
102+
let fn_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
103+
cx.expr_call_global(span, fn_cmp_path, vec![lft, rgt])
120104
}
121105
}),
122106
cx,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ pub fn expand_deriving_partial_eq(
3636

3737
let expr = cs_fold(
3838
true, // use foldl
39-
|cx, span, subexpr, self_expr, other_selflike_exprs| {
39+
|cx, span, old, self_expr, other_selflike_exprs| {
4040
let eq = op(cx, span, self_expr, other_selflike_exprs);
41-
cx.expr_binary(span, combiner, subexpr, eq)
41+
cx.expr_binary(span, combiner, old, eq)
4242
},
43-
|cx, args| {
44-
match args {
45-
Some((span, self_expr, other_selflike_exprs)) => {
46-
// Special-case the base case to generate cleaner code.
47-
op(cx, span, self_expr, other_selflike_exprs)
48-
}
49-
None => cx.expr_bool(span, base),
43+
|cx, args| match args {
44+
Some((span, self_expr, other_selflike_exprs)) => {
45+
// Special-case the base case to generate cleaner code.
46+
op(cx, span, self_expr, other_selflike_exprs)
5047
}
48+
None => cx.expr_bool(span, base),
5149
},
5250
Box::new(|cx, span, _| cx.expr_bool(span, !base)),
5351
cx,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::{path_std, pathvec_std};
44

5-
use rustc_ast::ptr::P;
6-
use rustc_ast::{Expr, MetaItem};
5+
use rustc_ast::MetaItem;
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::{sym, Ident};
98
use rustc_span::Span;
@@ -50,34 +49,25 @@ pub fn expand_deriving_partial_ord(
5049

5150
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
5251
let test_id = Ident::new(sym::cmp, span);
53-
let ordering = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
54-
let ordering_expr = cx.expr_path(ordering.clone());
55-
52+
let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
5653
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
5754

5855
// Builds:
5956
//
60-
// match ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1) {
61-
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) =>
62-
// match ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2) {
63-
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => {
64-
// ...
65-
// }
66-
// cmp => cmp
67-
// },
68-
// cmp => cmp
57+
// match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
58+
// ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
59+
// ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
60+
// cmp => cmp,
6961
// }
70-
//
7162
let expr = cs_fold(
7263
// foldr nests the if-elses correctly, leaving the first field
7364
// as the outermost one, and the last as the innermost.
7465
false,
7566
|cx, span, old, self_expr, other_selflike_exprs| {
7667
// match new {
77-
// Some(::std::cmp::Ordering::Equal) => old,
68+
// Some(::core::cmp::Ordering::Equal) => old,
7869
// cmp => cmp
7970
// }
80-
8171
let new = {
8272
let [other_expr] = other_selflike_exprs else {
8373
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`");
@@ -91,12 +81,13 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
9181
cx.expr_call_global(span, partial_cmp_path.clone(), args)
9282
};
9383

94-
let eq_arm = cx.arm(span, cx.pat_some(span, cx.pat_path(span, ordering.clone())), old);
84+
let eq_arm =
85+
cx.arm(span, cx.pat_some(span, cx.pat_path(span, equal_path.clone())), old);
9586
let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
9687

9788
cx.expr_match(span, new, vec![eq_arm, neq_arm])
9889
},
99-
|cx: &mut ExtCtxt<'_>, args: Option<(Span, P<Expr>, &[P<Expr>])>| match args {
90+
|cx, args| match args {
10091
Some((span, self_expr, other_selflike_exprs)) => {
10192
let new = {
10293
let [other_expr] = other_selflike_exprs else {
@@ -111,7 +102,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
111102

112103
new
113104
}
114-
None => cx.expr_some(span, ordering_expr.clone()),
105+
None => cx.expr_some(span, cx.expr_path(equal_path.clone())),
115106
},
116107
Box::new(|cx, span, tag_tuple| {
117108
if tag_tuple.len() != 2 {

0 commit comments

Comments
 (0)