Skip to main content

rustc_builtin_macros/deriving/
eq.rs

1use rustc_ast::{self as ast, Safety};
2use rustc_data_structures::fx::FxHashSet;
3use rustc_expand::base::ExtCtxt;
4use rustc_span::{Span, sym};
5use thin_vec::{ThinVec, thin_vec};
6
7use crate::deriving::generic::*;
8use crate::deriving::path_std;
9
10pub(crate) fn expand_deriving_eq(
11    cx: &ExtCtxt<'_>,
12    span: Span,
13    item: &ast::Item,
14    push: &mut dyn FnMut(Box<ast::Item>),
15    is_const: bool,
16) {
17    let span = cx.with_def_site_ctxt(span);
18
19    let trait_def = TraitDef {
20        span,
21        path: new_path(cx, span, { &[sym::cmp, sym::Eq] }, ::alloc::vec::Vec::new())path_std!(cx, span, cmp::Eq),
22        skip_path_as_bound: false,
23        needs_copy_as_bound_if_packed: true,
24        additional_bounds: SmallVec::new(),
25        supports_unions: true,
26        methods: {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(MethodDef {
                name: sym::assert_fields_are_eq,
                generics: cx.empty_generics(span),
                explicit_self: true,
                nonself_args: ::smallvec::SmallVec::new(),
                has_other_selflike_arg: false,
                ret_ty: cx.ty_unit(span),
                attributes: {
                    let len = [(), (), ()].len();
                    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
                    vec.push(cx.attr_word(sym::inline, span));
                    vec.push(cx.attr_nested_word(sym::doc, sym::hidden, span));
                    vec.push(cx.attr_nested_word(sym::coverage, sym::off,
                            span));
                    vec
                },
                fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
                combine_substructure: combine_substructure(cs_total_eq_assert),
            });
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [MethodDef {
                                name: sym::assert_fields_are_eq,
                                generics: cx.empty_generics(span),
                                explicit_self: true,
                                nonself_args: ::smallvec::SmallVec::new(),
                                has_other_selflike_arg: false,
                                ret_ty: cx.ty_unit(span),
                                attributes: {
                                    let len = [(), (), ()].len();
                                    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
                                    vec.push(cx.attr_word(sym::inline, span));
                                    vec.push(cx.attr_nested_word(sym::doc, sym::hidden, span));
                                    vec.push(cx.attr_nested_word(sym::coverage, sym::off,
                                            span));
                                    vec
                                },
                                fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
                                combine_substructure: combine_substructure(cs_total_eq_assert),
                            }])))
    }
}smallvec![MethodDef {
27            name: sym::assert_fields_are_eq,
28            generics: cx.empty_generics(span),
29            explicit_self: true,
30            nonself_args: smallvec![],
31            has_other_selflike_arg: false,
32            ret_ty: cx.ty_unit(span),
33            attributes: thin_vec![
34                // This method will never be called, so doing codegen etc. for it is unnecessary.
35                // We prevent this by adding `#[inline]`, which improves compile-time.
36                cx.attr_word(sym::inline, span),
37                cx.attr_nested_word(sym::doc, sym::hidden, span),
38                cx.attr_nested_word(sym::coverage, sym::off, span),
39            ],
40            fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
41            combine_substructure: combine_substructure(cs_total_eq_assert),
42        }],
43        is_const,
44        safety: Safety::Default,
45        document: true,
46    };
47    trait_def.expand_ext(cx, item, push, true)
48}
49
50fn cs_total_eq_assert(cx: &ExtCtxt<'_>, trait_span: Span, substr: Substructure<'_>) -> BlockOrExpr {
51    let mut stmts = ThinVec::new();
52    let mut seen_type_names = FxHashSet::default();
53    let mut process_variant = |variant: &ast::VariantData| {
54        for field in variant.fields() {
55            // This basic redundancy checking only prevents duplication of
56            // assertions like `AssertParamIsEq<Foo>` where the type is a
57            // simple name. That's enough to get a lot of cases, though.
58            if let Some(name) = field.ty.kind.is_simple_path()
59                && !seen_type_names.insert(name)
60            {
61                // Already produced an assertion for this type.
62            } else {
63                // let _: AssertParamIsEq<FieldTy>;
64                super::assert_ty_bounds(
65                    cx,
66                    &mut stmts,
67                    field.ty.clone(),
68                    field.span,
69                    &[sym::cmp, sym::AssertParamIsEq],
70                );
71            }
72        }
73    };
74
75    match substr.fields {
76        StaticStruct(vdata, ..) => {
77            process_variant(vdata);
78        }
79        StaticEnum(enum_def, ..) => {
80            for variant in &enum_def.variants {
81                process_variant(&variant.data);
82            }
83        }
84        _ => cx.dcx().span_bug(trait_span, "unexpected substructure in `derive(Eq)`"),
85    }
86    BlockOrExpr::new_stmts(stmts)
87}