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.
This commit is contained in:
parent
65d0bfbca5
commit
559398fa78
@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
|
||||
use crate::deriving::generic::*;
|
||||
use crate::deriving::path_std;
|
||||
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::{self as ast, MetaItem};
|
||||
use rustc_ast::MetaItem;
|
||||
use rustc_expand::base::{Annotatable, ExtCtxt};
|
||||
use rustc_span::symbol::{sym, Ident};
|
||||
use rustc_span::Span;
|
||||
@ -40,43 +39,25 @@ pub fn expand_deriving_ord(
|
||||
trait_def.expand(cx, mitem, item, push)
|
||||
}
|
||||
|
||||
pub fn ordering_collapsed(
|
||||
cx: &mut ExtCtxt<'_>,
|
||||
span: Span,
|
||||
self_arg_tags: &[Ident],
|
||||
) -> P<ast::Expr> {
|
||||
let lft = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[0]));
|
||||
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
|
||||
let fn_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
|
||||
cx.expr_call_global(span, fn_cmp_path, vec![lft, rgt])
|
||||
}
|
||||
|
||||
pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
|
||||
let test_id = Ident::new(sym::cmp, span);
|
||||
let equals_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
|
||||
|
||||
let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
|
||||
let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
|
||||
|
||||
// Builds:
|
||||
//
|
||||
// match ::std::cmp::Ord::cmp(&self_field1, &other_field1) {
|
||||
// ::std::cmp::Ordering::Equal =>
|
||||
// match ::std::cmp::Ord::cmp(&self_field2, &other_field2) {
|
||||
// ::std::cmp::Ordering::Equal => {
|
||||
// ...
|
||||
// match ::core::cmp::Ord::cmp(&self.x, &other.x) {
|
||||
// ::std::cmp::Ordering::Equal =>
|
||||
// ::core::cmp::Ord::cmp(&self.y, &other.y),
|
||||
// cmp => cmp,
|
||||
// }
|
||||
// cmp => cmp
|
||||
// },
|
||||
// cmp => cmp
|
||||
// }
|
||||
//
|
||||
let expr = cs_fold(
|
||||
// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_expr, other_selflike_exprs| {
|
||||
// match new {
|
||||
// ::std::cmp::Ordering::Equal => old,
|
||||
// ::core::cmp::Ordering::Equal => old,
|
||||
// cmp => cmp
|
||||
// }
|
||||
let new = {
|
||||
@ -90,7 +71,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
|
||||
cx.expr_call_global(span, cmp_path.clone(), args)
|
||||
};
|
||||
|
||||
let eq_arm = cx.arm(span, cx.pat_path(span, equals_path.clone()), old);
|
||||
let eq_arm = cx.arm(span, cx.pat_path(span, equal_path.clone()), old);
|
||||
let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
|
||||
|
||||
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
|
||||
|
||||
new
|
||||
}
|
||||
None => cx.expr_path(equals_path.clone()),
|
||||
None => cx.expr_path(equal_path.clone()),
|
||||
},
|
||||
Box::new(|cx, span, tag_tuple| {
|
||||
if tag_tuple.len() != 2 {
|
||||
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
|
||||
} else {
|
||||
ordering_collapsed(cx, span, tag_tuple)
|
||||
let lft = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[0]));
|
||||
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[1]));
|
||||
let fn_cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
|
||||
cx.expr_call_global(span, fn_cmp_path, vec![lft, rgt])
|
||||
}
|
||||
}),
|
||||
cx,
|
||||
|
@ -36,18 +36,16 @@ pub fn expand_deriving_partial_eq(
|
||||
|
||||
let expr = cs_fold(
|
||||
true, // use foldl
|
||||
|cx, span, subexpr, self_expr, other_selflike_exprs| {
|
||||
|cx, span, old, self_expr, other_selflike_exprs| {
|
||||
let eq = op(cx, span, self_expr, other_selflike_exprs);
|
||||
cx.expr_binary(span, combiner, subexpr, eq)
|
||||
cx.expr_binary(span, combiner, old, eq)
|
||||
},
|
||||
|cx, args| {
|
||||
match args {
|
||||
Some((span, self_expr, other_selflike_exprs)) => {
|
||||
// Special-case the base case to generate cleaner code.
|
||||
op(cx, span, self_expr, other_selflike_exprs)
|
||||
}
|
||||
None => cx.expr_bool(span, base),
|
||||
|cx, args| match args {
|
||||
Some((span, self_expr, other_selflike_exprs)) => {
|
||||
// Special-case the base case to generate cleaner code.
|
||||
op(cx, span, self_expr, other_selflike_exprs)
|
||||
}
|
||||
None => cx.expr_bool(span, base),
|
||||
},
|
||||
Box::new(|cx, span, _| cx.expr_bool(span, !base)),
|
||||
cx,
|
||||
|
@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
|
||||
use crate::deriving::generic::*;
|
||||
use crate::deriving::{path_std, pathvec_std};
|
||||
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::{Expr, MetaItem};
|
||||
use rustc_ast::MetaItem;
|
||||
use rustc_expand::base::{Annotatable, ExtCtxt};
|
||||
use rustc_span::symbol::{sym, Ident};
|
||||
use rustc_span::Span;
|
||||
@ -50,34 +49,25 @@ pub fn expand_deriving_partial_ord(
|
||||
|
||||
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
|
||||
let test_id = Ident::new(sym::cmp, span);
|
||||
let ordering = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
|
||||
let ordering_expr = cx.expr_path(ordering.clone());
|
||||
|
||||
let equal_path = cx.path_global(span, cx.std_path(&[sym::cmp, sym::Ordering, sym::Equal]));
|
||||
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
|
||||
|
||||
// Builds:
|
||||
//
|
||||
// match ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1) {
|
||||
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) =>
|
||||
// match ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2) {
|
||||
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => {
|
||||
// ...
|
||||
// match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
|
||||
// ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
|
||||
// ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
|
||||
// cmp => cmp,
|
||||
// }
|
||||
// cmp => cmp
|
||||
// },
|
||||
// cmp => cmp
|
||||
// }
|
||||
//
|
||||
let expr = cs_fold(
|
||||
// foldr nests the if-elses correctly, leaving the first field
|
||||
// as the outermost one, and the last as the innermost.
|
||||
false,
|
||||
|cx, span, old, self_expr, other_selflike_exprs| {
|
||||
// match new {
|
||||
// Some(::std::cmp::Ordering::Equal) => old,
|
||||
// Some(::core::cmp::Ordering::Equal) => old,
|
||||
// cmp => cmp
|
||||
// }
|
||||
|
||||
let new = {
|
||||
let [other_expr] = other_selflike_exprs else {
|
||||
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<'_
|
||||
cx.expr_call_global(span, partial_cmp_path.clone(), args)
|
||||
};
|
||||
|
||||
let eq_arm = cx.arm(span, cx.pat_some(span, cx.pat_path(span, ordering.clone())), old);
|
||||
let eq_arm =
|
||||
cx.arm(span, cx.pat_some(span, cx.pat_path(span, equal_path.clone())), old);
|
||||
let neq_arm = cx.arm(span, cx.pat_ident(span, test_id), cx.expr_ident(span, test_id));
|
||||
|
||||
cx.expr_match(span, new, vec![eq_arm, neq_arm])
|
||||
},
|
||||
|cx: &mut ExtCtxt<'_>, args: Option<(Span, P<Expr>, &[P<Expr>])>| match args {
|
||||
|cx, args| match args {
|
||||
Some((span, self_expr, other_selflike_exprs)) => {
|
||||
let new = {
|
||||
let [other_expr] = other_selflike_exprs else {
|
||||
@ -111,7 +102,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
|
||||
|
||||
new
|
||||
}
|
||||
None => cx.expr_some(span, ordering_expr.clone()),
|
||||
None => cx.expr_some(span, cx.expr_path(equal_path.clone())),
|
||||
},
|
||||
Box::new(|cx, span, tag_tuple| {
|
||||
if tag_tuple.len() != 2 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user