1
Fork 0

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:
Nicholas Nethercote 2022-07-05 10:21:37 +10:00
parent 65d0bfbca5
commit 559398fa78
3 changed files with 31 additions and 58 deletions

View file

@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
use crate::deriving::generic::*; use crate::deriving::generic::*;
use crate::deriving::path_std; use crate::deriving::path_std;
use rustc_ast::ptr::P; use rustc_ast::MetaItem;
use rustc_ast::{self as ast, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident}; use rustc_span::symbol::{sym, Ident};
use rustc_span::Span; use rustc_span::Span;
@ -40,43 +39,25 @@ pub fn expand_deriving_ord(
trait_def.expand(cx, mitem, item, push) 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 { pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
let test_id = Ident::new(sym::cmp, span); 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]); let cmp_path = cx.std_path(&[sym::cmp, sym::Ord, sym::cmp]);
// Builds: // Builds:
// //
// match ::std::cmp::Ord::cmp(&self_field1, &other_field1) { // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
// ::std::cmp::Ordering::Equal => // ::std::cmp::Ordering::Equal =>
// match ::std::cmp::Ord::cmp(&self_field2, &other_field2) { // ::core::cmp::Ord::cmp(&self.y, &other.y),
// ::std::cmp::Ordering::Equal => { // cmp => cmp,
// ...
// } // }
// cmp => cmp
// },
// cmp => cmp
// }
//
let expr = cs_fold( let expr = cs_fold(
// foldr nests the if-elses correctly, leaving the first field // foldr nests the if-elses correctly, leaving the first field
// as the outermost one, and the last as the innermost. // as the outermost one, and the last as the innermost.
false, false,
|cx, span, old, self_expr, other_selflike_exprs| { |cx, span, old, self_expr, other_selflike_exprs| {
// match new { // match new {
// ::std::cmp::Ordering::Equal => old, // ::core::cmp::Ordering::Equal => old,
// cmp => cmp // cmp => cmp
// } // }
let new = { 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) 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)); 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.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 new
} }
None => cx.expr_path(equals_path.clone()), None => cx.expr_path(equal_path.clone()),
}, },
Box::new(|cx, span, tag_tuple| { Box::new(|cx, span, tag_tuple| {
if tag_tuple.len() != 2 { if tag_tuple.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`") cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
} else { } 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, cx,

View file

@ -36,18 +36,16 @@ pub fn expand_deriving_partial_eq(
let expr = cs_fold( let expr = cs_fold(
true, // use foldl 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); 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| { |cx, args| match args {
match args { Some((span, self_expr, other_selflike_exprs)) => {
Some((span, self_expr, other_selflike_exprs)) => { // Special-case the base case to generate cleaner code.
// Special-case the base case to generate cleaner code. op(cx, span, self_expr, other_selflike_exprs)
op(cx, span, self_expr, other_selflike_exprs)
}
None => cx.expr_bool(span, base),
} }
None => cx.expr_bool(span, base),
}, },
Box::new(|cx, span, _| cx.expr_bool(span, !base)), Box::new(|cx, span, _| cx.expr_bool(span, !base)),
cx, cx,

View file

@ -2,8 +2,7 @@ use crate::deriving::generic::ty::*;
use crate::deriving::generic::*; use crate::deriving::generic::*;
use crate::deriving::{path_std, pathvec_std}; use crate::deriving::{path_std, pathvec_std};
use rustc_ast::ptr::P; use rustc_ast::MetaItem;
use rustc_ast::{Expr, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident}; use rustc_span::symbol::{sym, Ident};
use rustc_span::Span; 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 { pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
let test_id = Ident::new(sym::cmp, span); let test_id = Ident::new(sym::cmp, span);
let ordering = 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 ordering_expr = cx.expr_path(ordering.clone());
let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]); let partial_cmp_path = cx.std_path(&[sym::cmp, sym::PartialOrd, sym::partial_cmp]);
// Builds: // Builds:
// //
// match ::std::cmp::PartialOrd::partial_cmp(&self_field1, &other_field1) { // match ::core::cmp::PartialOrd::partial_cmp(&self.x, &other.x) {
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => // ::core::option::Option::Some(::core::cmp::Ordering::Equal) =>
// match ::std::cmp::PartialOrd::partial_cmp(&self_field2, &other_field2) { // ::core::cmp::PartialOrd::partial_cmp(&self.y, &other.y),
// ::std::option::Option::Some(::std::cmp::Ordering::Equal) => { // cmp => cmp,
// ...
// } // }
// cmp => cmp
// },
// cmp => cmp
// }
//
let expr = cs_fold( let expr = cs_fold(
// foldr nests the if-elses correctly, leaving the first field // foldr nests the if-elses correctly, leaving the first field
// as the outermost one, and the last as the innermost. // as the outermost one, and the last as the innermost.
false, false,
|cx, span, old, self_expr, other_selflike_exprs| { |cx, span, old, self_expr, other_selflike_exprs| {
// match new { // match new {
// Some(::std::cmp::Ordering::Equal) => old, // Some(::core::cmp::Ordering::Equal) => old,
// cmp => cmp // cmp => cmp
// } // }
let new = { let new = {
let [other_expr] = other_selflike_exprs else { let [other_expr] = other_selflike_exprs else {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"); 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) 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)); 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.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)) => { Some((span, self_expr, other_selflike_exprs)) => {
let new = { let new = {
let [other_expr] = other_selflike_exprs else { let [other_expr] = other_selflike_exprs else {
@ -111,7 +102,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
new 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| { Box::new(|cx, span, tag_tuple| {
if tag_tuple.len() != 2 { if tag_tuple.len() != 2 {