Skip to content

Commit

Permalink
Inline and remove the cs_fold_* functions.
Browse files Browse the repository at this point in the history
Because they now have a single call site each.

Also rename `cs_fold1` as `cs_fold`, now that it's the only folding
function left.
  • Loading branch information
nnethercote committed Jul 4, 2022
1 parent 0ee79f2 commit 0da063c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 51 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
// cmp => cmp
// }
//
let expr = cs_fold1(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn expand_deriving_partial_eq(
cx.expr_binary(span, op, self_f, other_f.clone())
};

let expr = cs_fold1(
let expr = cs_fold(
true, // use foldl
|cx, span, subexpr, self_f, other_fs| {
let eq = op(cx, span, self_f, other_fs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
// cmp => cmp
// }
//
let expr = cs_fold1(
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,
Expand Down
62 changes: 14 additions & 48 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,46 +1654,6 @@ impl<'a> TraitDef<'a> {
}
}

// helpful premade recipes

fn cs_fold_fields<'a, F>(
use_foldl: bool,
mut f: F,
base: P<Expr>,
cx: &mut ExtCtxt<'_>,
all_fields: &[FieldInfo<'a>],
) -> P<Expr>
where
F: FnMut(&mut ExtCtxt<'_>, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>,
{
if use_foldl {
all_fields
.iter()
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
} else {
all_fields
.iter()
.rev()
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
}
}

fn cs_fold_enumnonmatch(
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substructure: &Substructure<'_>,
) -> P<Expr> {
match *substructure.fields {
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
_ => cx.span_bug(trait_span, "cs_fold_enumnonmatch expected an EnumNonMatchingCollapsed"),
}
}

fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
cx.span_bug(trait_span, "static function in `derive`")
}

/// Function to fold over fields, with three cases, to generate more efficient and concise code.
/// When the `substructure` has grouped fields, there are two cases:
/// Zero fields: call the base case function with `None` (like the usual base case of `cs_fold`).
Expand All @@ -1702,11 +1662,11 @@ fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
/// fields.
/// When the `substructure` is an `EnumNonMatchingCollapsed`, the result of `enum_nonmatch_f`
/// is returned. Statics may not be folded over.
pub fn cs_fold1<F, B>(
pub fn cs_fold<F, B>(
use_foldl: bool,
f: F,
mut f: F,
mut b: B,
enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
cx: &mut ExtCtxt<'_>,
trait_span: Span,
substructure: &Substructure<'_>,
Expand All @@ -1731,12 +1691,18 @@ where
(true, _) => (b(cx, None), &all_fields[..]),
};

cs_fold_fields(use_foldl, f, base, cx, rest)
}
EnumNonMatchingCollapsed(..) => {
cs_fold_enumnonmatch(enum_nonmatch_f, cx, trait_span, substructure)
if use_foldl {
rest.iter().fold(base, |old, field| {
f(cx, field.span, old, field.self_.clone(), &field.other)
})
} else {
rest.iter().rev().fold(base, |old, field| {
f(cx, field.span, old, field.self_.clone(), &field.other)
})
}
}
StaticEnum(..) | StaticStruct(..) => cs_fold_static(cx, trait_span),
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),
}
}

Expand Down

0 comments on commit 0da063c

Please sign in to comment.