]> git.lizzy.rs Git - rust.git/commitdiff
Fix some inconsistencies.
authorNicholas Nethercote <n.nethercote@gmail.com>
Tue, 5 Jul 2022 00:21:37 +0000 (10:21 +1000)
committerNicholas Nethercote <n.nethercote@gmail.com>
Fri, 8 Jul 2022 23:02:50 +0000 (09:02 +1000)
This makes `cs_cmp`, `cs_partial_cmp`, and `cs_op` (for `PartialEq`)
more similar. It also fixes some out of date comments.

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

index a60db068f27de6c3985e909bb1eac2829aa32981..1856be87a20ddfd5afd8d9691e978019de0a8b6e 100644 (file)
@@ -2,8 +2,7 @@
 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 => {
-    // ...
-    // }
-    // cmp => cmp
-    // },
-    // cmp => cmp
+    // match ::core::cmp::Ord::cmp(&self.x, &other.x) {
+    //     ::std::cmp::Ordering::Equal =>
+    //         ::core::cmp::Ord::cmp(&self.y, &other.y),
+    //     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,
index a18d78b8476c533d3e25e42bad8544b695a56512..e4af0166577e15229009272f6fc4c910ee2369d1 100644 (file)
@@ -36,18 +36,16 @@ fn cs_op(
 
         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,
index b809eaf8eecac7127884959957c119fa0fe672ac..bf52c63fad4b75aa782989a462171b7febfd69e6 100644 (file)
@@ -2,8 +2,7 @@
 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) => {
-    // ...
-    // }
-    // cmp => cmp
-    // },
-    // cmp => cmp
+    // 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,
     // }
-    //
     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 {