Skip to content

Commit a64a0d1

Browse files
committed
Auto merge of #164217 - tehforsch:fix-160621, r=<try>
Prepare to make `rustc_trivial_field_reads` usable in user code
2 parents d64de46 + 8d945ce commit a64a0d1

29 files changed

Lines changed: 563 additions & 157 deletions

‎compiler/rustc_attr_ir/src/data_structures.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ pub enum AttributeKind {
14321432
RustcThenThisWouldNeed(ThinVec<Ident>),
14331433

14341434
/// Represents `#[rustc_trivial_field_reads]`
1435-
RustcTrivialFieldReads,
1435+
RustcTrivialFieldReads(Span),
14361436

14371437
/// Represents `#[sanitize]`
14381438
///

‎compiler/rustc_attr_ir/src/encode_cross_crate.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl AttributeKind {
194194
RustcStrictCoherence(..) => Yes,
195195
RustcTestMarker(..) => No,
196196
RustcThenThisWouldNeed(..) => No,
197-
RustcTrivialFieldReads => Yes,
197+
RustcTrivialFieldReads(..) => Yes,
198198
Sanitize { .. } => No,
199199
ShouldPanic { .. } => No,
200200
Splat(..) => Yes,
@@ -408,7 +408,7 @@ impl AttributeKind {
408408
RustcStrictCoherence(..) => false,
409409
RustcTestMarker(..) => false,
410410
RustcThenThisWouldNeed(..) => false,
411-
RustcTrivialFieldReads => false,
411+
RustcTrivialFieldReads(..) => false,
412412
Sanitize { .. } => false,
413413
ShouldPanic { .. } => false,
414414
Splat(..) => false,

‎compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,13 @@ pub(crate) struct RustcTrivialFieldReadsParser;
440440

441441
impl NoArgsAttributeParser for RustcTrivialFieldReadsParser {
442442
const PATH: &[Symbol] = &[sym::rustc_trivial_field_reads];
443-
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[Allow(Target::Trait)]);
443+
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
444+
Allow(Target::Method(MethodKind::TraitImpl)),
445+
Allow(Target::Method(MethodKind::Inherent)),
446+
Allow(Target::Fn),
447+
]);
444448
const STABILITY: AttributeStability = unstable!(rustc_attrs);
445-
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcTrivialFieldReads;
449+
const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::RustcTrivialFieldReads(span);
446450
}
447451

448452
pub(crate) struct RustcNoMirInlineParser;

‎compiler/rustc_builtin_macros/src/deriving/clone.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ pub(crate) fn expand_deriving_clone(
9191
nonself_args: SmallVec::new(),
9292
has_other_selflike_arg: false,
9393
ret_ty: cx.ty_self(span),
94-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
94+
attributes: thin_vec![
95+
cx.attr_word(sym::inline, span),
96+
cx.attr_word(sym::rustc_trivial_field_reads, span)
97+
],
9598
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
9699
combine_substructure: substructure,
97100
}],

‎compiler/rustc_builtin_macros/src/deriving/debug.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ pub(crate) fn expand_deriving_debug(
3636
nonself_args: smallvec![(fmtr, sym::character('f'))],
3737
has_other_selflike_arg: false,
3838
ret_ty: cx.ty_path(path_std!(cx, span, fmt::Result)),
39-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
39+
attributes: thin_vec![
40+
cx.attr_word(sym::inline, span),
41+
cx.attr_word(sym::rustc_trivial_field_reads, span)
42+
],
4043
fieldless_variants_strategy:
4144
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
4245
combine_substructure: combine_substructure(|cx, span, substr| show_substructure(

‎compiler/rustc_middle/src/middle/dead_code.rs‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
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};
33
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>);
423

524
/// A single snapshot of dead-code liveness analysis state.
625
#[derive(Clone, Debug, StableHash)]
726
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+
}
1257
}
1358

1459
/// Dead-code liveness data for both analysis phases.

‎compiler/rustc_middle/src/queries.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ rustc_queries! {
12291229
}
12301230

12311231
/// Return dead-code liveness summary for the crate.
1232-
query live_symbols_and_ignored_derived_traits(_: ()) -> Result<&'tcx DeadCodeLivenessSummary, ErrorGuaranteed> {
1232+
query live_symbols_and_trivial_usage_sites(_: ()) -> Result<&'tcx DeadCodeLivenessSummary, ErrorGuaranteed> {
12331233
arena_cache
12341234
desc { "finding live symbols in crate" }
12351235
}

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
391391
AttributeKind::RustcStrictCoherence(..) => (),
392392
AttributeKind::RustcTestMarker(..) => (),
393393
AttributeKind::RustcThenThisWouldNeed(..) => (),
394-
AttributeKind::RustcTrivialFieldReads => (),
394+
AttributeKind::RustcTrivialFieldReads(..) => (),
395395
AttributeKind::Sanitize { .. } => {}
396396
AttributeKind::ShouldPanic { .. } => (),
397397
AttributeKind::Splat(..) => (),

0 commit comments

Comments
 (0)