Skip to main content

rustc_builtin_macros/deriving/cmp/
eq.rs

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