|
1 | | -use rustc_data_structures::fx::FxIndexSet; |
2 | | -use rustc_hir::def_id::{DefId, LocalDefIdMap, LocalDefIdSet}; |
| 1 | +use rustc_hir::HirId; |
| 2 | +use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, LocalDefIdSet}; |
3 | 3 | use rustc_macros::StableHash; |
| 4 | +use rustc_span::Span; |
| 5 | + |
| 6 | +#[derive(Clone, Debug, StableHash, PartialEq, Eq, Hash)] |
| 7 | +pub enum LiveKind { |
| 8 | + Trivial, |
| 9 | + NonTrivial, |
| 10 | +} |
| 11 | + |
| 12 | +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, StableHash, PartialOrd, Ord)] |
| 13 | +pub struct TrivialUsageSiteId(usize); |
| 14 | + |
| 15 | +#[derive(Debug, Clone, StableHash)] |
| 16 | +pub struct TrivialUsageSite { |
| 17 | + pub attr_span: Span, |
| 18 | + pub hir_id: HirId, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Clone, Debug, StableHash, Default)] |
| 22 | +pub struct TrivialUsageSites(Vec<TrivialUsageSite>); |
4 | 23 |
|
5 | 24 | /// A single snapshot of dead-code liveness analysis state. |
6 | 25 | #[derive(Clone, Debug, StableHash)] |
7 | 26 | pub struct DeadCodeLivenessSnapshot { |
8 | | - pub live_symbols: LocalDefIdSet, |
9 | | - /// Maps each ADT to derived traits (for example `Debug` and `Clone`) that should be ignored |
10 | | - /// when checking for dead code diagnostics. |
11 | | - pub ignored_derived_traits: LocalDefIdMap<FxIndexSet<DefId>>, |
| 27 | + pub live: LocalDefIdSet, |
| 28 | + /// This maps defs that were only used from items annotated with |
| 29 | + /// `#[rustc_trivial_field_reads]` to those items |
| 30 | + pub sites_by_defid: LocalDefIdMap<Vec<TrivialUsageSiteId>>, |
| 31 | + /// List of all items annotated with `#[rustc_trivial_field_reads]` |
| 32 | + pub sites: TrivialUsageSites, |
| 33 | +} |
| 34 | + |
| 35 | +impl TrivialUsageSites { |
| 36 | + pub fn add_trivial_usage_site(&mut self, site: TrivialUsageSite) -> TrivialUsageSiteId { |
| 37 | + self.0.push(site); |
| 38 | + TrivialUsageSiteId(self.0.len() - 1) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl DeadCodeLivenessSnapshot { |
| 43 | + pub fn is_live(&self, id: &LocalDefId) -> bool { |
| 44 | + self.live.contains(id) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn trivial_usage_sites( |
| 48 | + &self, |
| 49 | + def_id: &LocalDefId, |
| 50 | + ) -> impl Iterator<Item = &TrivialUsageSite> { |
| 51 | + self.sites_by_defid |
| 52 | + .get(def_id) |
| 53 | + .into_iter() |
| 54 | + .flatten() |
| 55 | + .map(|site_id| &self.sites.0[site_id.0]) |
| 56 | + } |
12 | 57 | } |
13 | 58 |
|
14 | 59 | /// Dead-code liveness data for both analysis phases. |
|
0 commit comments