From d3057b5ca78cc05823f2dc75cb774bbffc5403a6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 5 Jul 2022 08:47:04 +1000 Subject: [PATCH] Rename `FieldInfo` fields. Use `self_exprs` and `other_selflike_exprs` in a manner similar to the previous commit. --- .../src/deriving/clone.rs | 2 +- .../src/deriving/cmp/ord.rs | 20 ++++++++----- .../src/deriving/cmp/partial_eq.rs | 17 ++++++----- .../src/deriving/cmp/partial_ord.rs | 20 ++++++++----- .../src/deriving/debug.rs | 4 +-- .../src/deriving/encodable.rs | 8 ++--- .../src/deriving/generic/mod.rs | 29 ++++++++++--------- .../rustc_builtin_macros/src/deriving/hash.rs | 4 ++- 8 files changed, 59 insertions(+), 45 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index a67d16d6b2f2..a45b6e0407a9 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -161,7 +161,7 @@ fn cs_clone( let all_fields; let fn_path = cx.std_path(&[sym::clone, sym::Clone, sym::clone]); let subcall = |cx: &mut ExtCtxt<'_>, field: &FieldInfo<'_>| { - let args = vec![cx.expr_addr_of(field.span, field.self_.clone())]; + let args = vec![cx.expr_addr_of(field.span, field.self_expr.clone())]; cx.expr_call_global(field.span, fn_path.clone(), args) }; diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs index d80a2293e667..a60db068f27d 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs @@ -74,17 +74,19 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl // 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_f, other_fs| { + |cx, span, old, self_expr, other_selflike_exprs| { // match new { // ::std::cmp::Ordering::Equal => old, // cmp => cmp // } let new = { - let [other_f] = other_fs else { + let [other_expr] = other_selflike_exprs else { cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"); }; - let args = - vec![cx.expr_addr_of(span, self_f), cx.expr_addr_of(span, other_f.clone())]; + let args = vec![ + cx.expr_addr_of(span, self_expr), + cx.expr_addr_of(span, other_expr.clone()), + ]; cx.expr_call_global(span, cmp_path.clone(), args) }; @@ -94,13 +96,15 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl cx.expr_match(span, new, vec![eq_arm, neq_arm]) }, |cx, args| match args { - Some((span, self_f, other_fs)) => { + Some((span, self_expr, other_selflike_exprs)) => { let new = { - let [other_f] = other_fs else { + let [other_expr] = other_selflike_exprs else { cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"); }; - let args = - vec![cx.expr_addr_of(span, self_f), cx.expr_addr_of(span, other_f.clone())]; + let args = vec![ + cx.expr_addr_of(span, self_expr), + cx.expr_addr_of(span, other_expr.clone()), + ]; cx.expr_call_global(span, cmp_path.clone(), args) }; diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs index a9a0634836b8..a18d78b8476c 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -23,25 +23,28 @@ fn cs_op( combiner: BinOpKind, base: bool, ) -> BlockOrExpr { - let op = |cx: &mut ExtCtxt<'_>, span: Span, self_f: P, other_fs: &[P]| { - let [other_f] = other_fs else { + let op = |cx: &mut ExtCtxt<'_>, + span: Span, + self_expr: P, + other_selflike_exprs: &[P]| { + let [other_expr] = other_selflike_exprs else { cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"); }; - cx.expr_binary(span, op, self_f, other_f.clone()) + cx.expr_binary(span, op, self_expr, other_expr.clone()) }; let expr = cs_fold( true, // use foldl - |cx, span, subexpr, self_f, other_fs| { - let eq = op(cx, span, self_f, other_fs); + |cx, span, subexpr, self_expr, other_selflike_exprs| { + let eq = op(cx, span, self_expr, other_selflike_exprs); cx.expr_binary(span, combiner, subexpr, eq) }, |cx, args| { match args { - Some((span, self_f, other_fs)) => { + Some((span, self_expr, other_selflike_exprs)) => { // Special-case the base case to generate cleaner code. - op(cx, span, self_f, other_fs) + op(cx, span, self_expr, other_selflike_exprs) } None => cx.expr_bool(span, base), } diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs index c8c9a6fbb579..b809eaf8eeca 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -72,19 +72,21 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_ // 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_f, other_fs| { + |cx, span, old, self_expr, other_selflike_exprs| { // match new { // Some(::std::cmp::Ordering::Equal) => old, // cmp => cmp // } let new = { - let [other_f] = other_fs else { + let [other_expr] = other_selflike_exprs else { cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"); }; - let args = - vec![cx.expr_addr_of(span, self_f), cx.expr_addr_of(span, other_f.clone())]; + let args = vec![ + cx.expr_addr_of(span, self_expr), + cx.expr_addr_of(span, other_expr.clone()), + ]; cx.expr_call_global(span, partial_cmp_path.clone(), args) }; @@ -95,13 +97,15 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_ cx.expr_match(span, new, vec![eq_arm, neq_arm]) }, |cx: &mut ExtCtxt<'_>, args: Option<(Span, P, &[P])>| match args { - Some((span, self_f, other_fs)) => { + Some((span, self_expr, other_selflike_exprs)) => { let new = { - let [other_f] = other_fs else { + let [other_expr] = other_selflike_exprs else { cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"); }; - let args = - vec![cx.expr_addr_of(span, self_f), cx.expr_addr_of(span, other_f.clone())]; + let args = vec![ + cx.expr_addr_of(span, self_expr), + cx.expr_addr_of(span, other_expr.clone()), + ]; cx.expr_call_global(span, partial_cmp_path.clone(), args) }; diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs index 71f77ea8b6a3..b99198054def 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -96,7 +96,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_> args.push(name); } // Use double indirection to make sure this works for unsized types - let field = cx.expr_addr_of(field.span, field.self_.clone()); + let field = cx.expr_addr_of(field.span, field.self_expr.clone()); let field = cx.expr_addr_of(field.span, field); args.push(field); } @@ -116,7 +116,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_> } // Use double indirection to make sure this works for unsized types - let value_ref = cx.expr_addr_of(field.span, field.self_.clone()); + let value_ref = cx.expr_addr_of(field.span, field.self_expr.clone()); value_exprs.push(cx.expr_addr_of(field.span, value_ref)); } diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs index c89558f6b86e..49dbe51f7626 100644 --- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs @@ -168,12 +168,12 @@ fn encodable_substructure( let fn_emit_struct_field_path = cx.def_site_path(&[sym::rustc_serialize, sym::Encoder, sym::emit_struct_field]); let mut stmts = Vec::new(); - for (i, &FieldInfo { name, ref self_, span, .. }) in fields.iter().enumerate() { + for (i, &FieldInfo { name, ref self_expr, span, .. }) in fields.iter().enumerate() { let name = match name { Some(id) => id.name, None => Symbol::intern(&format!("_field{}", i)), }; - let self_ref = cx.expr_addr_of(span, self_.clone()); + let self_ref = cx.expr_addr_of(span, self_expr.clone()); let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]); let lambda = cx.lambda1(span, enc, blkarg); let call = cx.expr_call_global( @@ -237,8 +237,8 @@ fn encodable_substructure( let mut stmts = Vec::new(); if !fields.is_empty() { let last = fields.len() - 1; - for (i, &FieldInfo { ref self_, span, .. }) in fields.iter().enumerate() { - let self_ref = cx.expr_addr_of(span, self_.clone()); + for (i, &FieldInfo { ref self_expr, span, .. }) in fields.iter().enumerate() { + let self_ref = cx.expr_addr_of(span, self_expr.clone()); let enc = cx.expr_call(span, fn_path.clone(), vec![self_ref, blkencoder.clone()]); let lambda = cx.lambda1(span, enc, blkarg); diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index cafca507bd44..6ee7f72f9a4a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -258,10 +258,10 @@ pub struct FieldInfo<'a> { pub name: Option, /// The expression corresponding to this field of `self` /// (specifically, a reference to it). - pub self_: P, + pub self_expr: P, /// The expressions corresponding to references to this field in - /// the other `Self` arguments. - pub other: Vec>, + /// the other selflike arguments. + pub other_selflike_exprs: Vec>, /// The attributes on the field pub attrs: &'a [ast::Attribute], } @@ -1080,13 +1080,13 @@ fn expand_struct_method_body<'b>( let fields = if !raw_fields.is_empty() { let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter()); let first_field = raw_fields.next().unwrap(); - let mut other_fields: Vec> = raw_fields.collect(); + let mut nonself_fields: Vec> = raw_fields.collect(); first_field - .map(|(span, opt_id, field, attrs)| FieldInfo { + .map(|(span, opt_id, expr, attrs)| FieldInfo { span: span.with_ctxt(trait_.span.ctxt()), name: opt_id, - self_: field, - other: other_fields + self_expr: expr, + other_selflike_exprs: nonself_fields .iter_mut() .map(|l| { let (.., ex, _) = l.next().unwrap(); @@ -1289,7 +1289,7 @@ fn expand_enum_method_body<'b>( // and pull out getter for same field in each // of them (using `field_index` tracked above). // That is the heart of the transposition. - let others = selflike_pats_idents + let other_selflike_exprs = selflike_pats_idents .iter() .map(|fields| { let (_, _opt_ident, ref other_getter_expr, _) = fields[field_index]; @@ -1307,8 +1307,8 @@ fn expand_enum_method_body<'b>( FieldInfo { span, name: opt_ident, - self_: self_getter_expr, - other: others, + self_expr: self_getter_expr, + other_selflike_exprs, attrs, } }) @@ -1712,12 +1712,13 @@ pub fn cs_fold( let (base, rest) = match (all_fields.is_empty(), use_foldl) { (false, true) => { let (first, rest) = all_fields.split_first().unwrap(); - let args = (first.span, first.self_.clone(), &first.other[..]); + let args = + (first.span, first.self_expr.clone(), &first.other_selflike_exprs[..]); (b(cx, Some(args)), rest) } (false, false) => { let (last, rest) = all_fields.split_last().unwrap(); - let args = (last.span, last.self_.clone(), &last.other[..]); + let args = (last.span, last.self_expr.clone(), &last.other_selflike_exprs[..]); (b(cx, Some(args)), rest) } (true, _) => (b(cx, None), &all_fields[..]), @@ -1725,11 +1726,11 @@ pub fn cs_fold( if use_foldl { rest.iter().fold(base, |old, field| { - f(cx, field.span, old, field.self_.clone(), &field.other) + f(cx, field.span, old, field.self_expr.clone(), &field.other_selflike_exprs) }) } else { rest.iter().rev().fold(base, |old, field| { - f(cx, field.span, old, field.self_.clone(), &field.other) + f(cx, field.span, old, field.self_expr.clone(), &field.other_selflike_exprs) }) } } diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs index 6ff36e7f4ed0..2213cd6d37d2 100644 --- a/compiler/rustc_builtin_macros/src/deriving/hash.rs +++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs @@ -82,7 +82,9 @@ fn hash_substructure( }; stmts.extend( - fields.iter().map(|FieldInfo { ref self_, span, .. }| call_hash(*span, self_.clone())), + fields + .iter() + .map(|FieldInfo { ref self_expr, span, .. }| call_hash(*span, self_expr.clone())), ); BlockOrExpr::new_stmts(stmts) }