Skip to main content

rustc_middle/middle/
dead_code.rs

1use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, LocalDefIdSet};
2use rustc_macros::StableHash;
3use rustc_span::Span;
4
5/// A single snapshot of dead-code liveness analysis state.
6#[derive(#[automatically_derived]impl ::core::clone::Clone for DeadCodeLivenessSnapshot
    {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        DeadCodeLivenessSnapshot {
            live: ::core::clone::Clone::clone(&self.live),
            sites_by_defid: ::core::clone::Clone::clone(&self.sites_by_defid),
            sites: ::core::clone::Clone::clone(&self.sites),
        }
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for DeadCodeLivenessSnapshot {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "DeadCodeLivenessSnapshot live sites_by_defid sites", &self.live,
            &self.sites_by_defid, &&self.sites)
    }
}Debug, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            DeadCodeLivenessSnapshot {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    DeadCodeLivenessSnapshot {
                        live: ref __binding_0,
                        sites_by_defid: ref __binding_1,
                        sites: ref __binding_2 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                        { __binding_2.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
7pub struct DeadCodeLivenessSnapshot {
8    pub live: LocalDefIdSet,
9    /// This maps defs that were only used from items annotated with
10    /// `#[rustc_trivial_field_reads]` to those items
11    pub sites_by_defid: LocalDefIdMap<Vec<LocalDefId>>,
12    /// For each of the items annotated with `#[rustc_trivial_field_reads]`,
13    /// we store the attr span here, so we can use it for diagnostics if needed
14    pub sites: LocalDefIdMap<Span>,
15}
16
17impl DeadCodeLivenessSnapshot {
18    pub fn is_live(&self, id: &LocalDefId) -> bool {
19        self.live.contains(id)
20    }
21
22    pub fn trivial_usage_sites(&self, def_id: &LocalDefId) -> impl Iterator<Item = LocalDefId> {
23        self.sites_by_defid.get(def_id).into_iter().flatten().copied()
24    }
25
26    pub fn trivial_usage_site_span_by_id(&self, site_id: LocalDefId) -> Span {
27        *self.sites.get(&site_id).unwrap()
28    }
29}
30
31/// Dead-code liveness data for both analysis phases.
32///
33/// `pre_deferred_seeding` is computed before reachable-public and `#[allow(dead_code)]` seeding,
34/// and is used for lint `dead_code_pub_in_binary`.
35/// `final_result` is the final liveness snapshot used for lint `dead_code`.
36#[derive(#[automatically_derived]impl ::core::clone::Clone for DeadCodeLivenessSummary
    {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        DeadCodeLivenessSummary {
            pre_deferred_seeding: ::core::clone::Clone::clone(&self.pre_deferred_seeding),
            final_result: ::core::clone::Clone::clone(&self.final_result),
        }
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for DeadCodeLivenessSummary {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "DeadCodeLivenessSummary pre_deferred_seeding final_result",
            &self.pre_deferred_seeding, &&self.final_result)
    }
}Debug, const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            DeadCodeLivenessSummary {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    DeadCodeLivenessSummary {
                        pre_deferred_seeding: ref __binding_0,
                        final_result: ref __binding_1 } => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
37pub struct DeadCodeLivenessSummary {
38    pub pre_deferred_seeding: DeadCodeLivenessSnapshot,
39    pub final_result: DeadCodeLivenessSnapshot,
40}