Skip to content

Commit 0da063c

Browse files
committed
Inline and remove the cs_fold_* functions.
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.
1 parent 0ee79f2 commit 0da063c

File tree

4 files changed

+17
-51
lines changed

4 files changed

+17
-51
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> Bl
7070
// cmp => cmp
7171
// }
7272
//
73-
let expr = cs_fold1(
73+
let expr = cs_fold(
7474
// foldr nests the if-elses correctly, leaving the first field
7575
// as the outermost one, and the last as the innermost.
7676
false,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn expand_deriving_partial_eq(
3131
cx.expr_binary(span, op, self_f, other_f.clone())
3232
};
3333

34-
let expr = cs_fold1(
34+
let expr = cs_fold(
3535
true, // use foldl
3636
|cx, span, subexpr, self_f, other_fs| {
3737
let eq = op(cx, span, self_f, other_fs);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
6868
// cmp => cmp
6969
// }
7070
//
71-
let expr = cs_fold1(
71+
let expr = cs_fold(
7272
// foldr nests the if-elses correctly, leaving the first field
7373
// as the outermost one, and the last as the innermost.
7474
false,

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+14-48
Original file line numberDiff line numberDiff line change
@@ -1654,46 +1654,6 @@ impl<'a> TraitDef<'a> {
16541654
}
16551655
}
16561656

1657-
// helpful premade recipes
1658-
1659-
fn cs_fold_fields<'a, F>(
1660-
use_foldl: bool,
1661-
mut f: F,
1662-
base: P<Expr>,
1663-
cx: &mut ExtCtxt<'_>,
1664-
all_fields: &[FieldInfo<'a>],
1665-
) -> P<Expr>
1666-
where
1667-
F: FnMut(&mut ExtCtxt<'_>, Span, P<Expr>, P<Expr>, &[P<Expr>]) -> P<Expr>,
1668-
{
1669-
if use_foldl {
1670-
all_fields
1671-
.iter()
1672-
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
1673-
} else {
1674-
all_fields
1675-
.iter()
1676-
.rev()
1677-
.fold(base, |old, field| f(cx, field.span, old, field.self_.clone(), &field.other))
1678-
}
1679-
}
1680-
1681-
fn cs_fold_enumnonmatch(
1682-
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
1683-
cx: &mut ExtCtxt<'_>,
1684-
trait_span: Span,
1685-
substructure: &Substructure<'_>,
1686-
) -> P<Expr> {
1687-
match *substructure.fields {
1688-
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
1689-
_ => cx.span_bug(trait_span, "cs_fold_enumnonmatch expected an EnumNonMatchingCollapsed"),
1690-
}
1691-
}
1692-
1693-
fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
1694-
cx.span_bug(trait_span, "static function in `derive`")
1695-
}
1696-
16971657
/// Function to fold over fields, with three cases, to generate more efficient and concise code.
16981658
/// When the `substructure` has grouped fields, there are two cases:
16991659
/// Zero fields: call the base case function with `None` (like the usual base case of `cs_fold`).
@@ -1702,11 +1662,11 @@ fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
17021662
/// fields.
17031663
/// When the `substructure` is an `EnumNonMatchingCollapsed`, the result of `enum_nonmatch_f`
17041664
/// is returned. Statics may not be folded over.
1705-
pub fn cs_fold1<F, B>(
1665+
pub fn cs_fold<F, B>(
17061666
use_foldl: bool,
1707-
f: F,
1667+
mut f: F,
17081668
mut b: B,
1709-
enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
1669+
mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
17101670
cx: &mut ExtCtxt<'_>,
17111671
trait_span: Span,
17121672
substructure: &Substructure<'_>,
@@ -1731,12 +1691,18 @@ where
17311691
(true, _) => (b(cx, None), &all_fields[..]),
17321692
};
17331693

1734-
cs_fold_fields(use_foldl, f, base, cx, rest)
1735-
}
1736-
EnumNonMatchingCollapsed(..) => {
1737-
cs_fold_enumnonmatch(enum_nonmatch_f, cx, trait_span, substructure)
1694+
if use_foldl {
1695+
rest.iter().fold(base, |old, field| {
1696+
f(cx, field.span, old, field.self_.clone(), &field.other)
1697+
})
1698+
} else {
1699+
rest.iter().rev().fold(base, |old, field| {
1700+
f(cx, field.span, old, field.self_.clone(), &field.other)
1701+
})
1702+
}
17381703
}
1739-
StaticEnum(..) | StaticStruct(..) => cs_fold_static(cx, trait_span),
1704+
EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
1705+
StaticEnum(..) | StaticStruct(..) => cx.span_bug(trait_span, "static function in `derive`"),
17401706
}
17411707
}
17421708

0 commit comments

Comments
 (0)