Rename FieldInfo fields.

Use `self_exprs` and `other_selflike_exprs` in a manner similar to the
previous commit.
This commit is contained in:
Nicholas Nethercote 2022-07-05 08:47:04 +10:00
parent 32c9ffb9cc
commit d3057b5ca7
8 changed files with 59 additions and 45 deletions

View file

@ -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)
};

View file

@ -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)
};

View file

@ -23,25 +23,28 @@ pub fn expand_deriving_partial_eq(
combiner: BinOpKind,
base: bool,
) -> BlockOrExpr {
let op = |cx: &mut ExtCtxt<'_>, span: Span, self_f: P<Expr>, other_fs: &[P<Expr>]| {
let [other_f] = other_fs else {
let op = |cx: &mut ExtCtxt<'_>,
span: Span,
self_expr: P<Expr>,
other_selflike_exprs: &[P<Expr>]| {
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),
}

View file

@ -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<Expr>, &[P<Expr>])>| 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)
};

View file

@ -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));
}

View file

@ -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);

View file

@ -258,10 +258,10 @@ pub struct FieldInfo<'a> {
pub name: Option<Ident>,
/// The expression corresponding to this field of `self`
/// (specifically, a reference to it).
pub self_: P<Expr>,
pub self_expr: P<Expr>,
/// The expressions corresponding to references to this field in
/// the other `Self` arguments.
pub other: Vec<P<Expr>>,
/// the other selflike arguments.
pub other_selflike_exprs: Vec<P<Expr>>,
/// The attributes on the field
pub attrs: &'a [ast::Attribute],
}
@ -1080,13 +1080,13 @@ impl<'a> MethodDef<'a> {
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<vec::IntoIter<_>> = raw_fields.collect();
let mut nonself_fields: Vec<vec::IntoIter<_>> = 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 @@ impl<'a> MethodDef<'a> {
// 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 @@ impl<'a> MethodDef<'a> {
FieldInfo {
span,
name: opt_ident,
self_: self_getter_expr,
other: others,
self_expr: self_getter_expr,
other_selflike_exprs,
attrs,
}
})
@ -1712,12 +1712,13 @@ where
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 @@ where
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)
})
}
}

View file

@ -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)
}