Skip to content

Commit 3bbce8f

Browse files
Rollup merge of #151017 - rustc_dump, r=jdonszelmann
Port the rustc dump attributes to the attribute parser Tracking issue has been updated: #131229 Five easy ones r? @jdonszelmann
2 parents 0d3692a + abcbf72 commit 3bbce8f

File tree

8 files changed

+117
-22
lines changed

8 files changed

+117
-22
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub(crate) mod pin_v2;
5757
pub(crate) mod proc_macro_attrs;
5858
pub(crate) mod prototype;
5959
pub(crate) mod repr;
60+
pub(crate) mod rustc_dump;
6061
pub(crate) mod rustc_internal;
6162
pub(crate) mod semantics;
6263
pub(crate) mod stability;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use rustc_hir::Target;
2+
use rustc_hir::attrs::AttributeKind;
3+
use rustc_span::{Span, Symbol, sym};
4+
5+
use crate::attributes::prelude::Allow;
6+
use crate::attributes::{NoArgsAttributeParser, OnDuplicate};
7+
use crate::context::Stage;
8+
use crate::target_checking::AllowedTargets;
9+
10+
pub(crate) struct RustcDumpUserArgs;
11+
12+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpUserArgs {
13+
const PATH: &[Symbol] = &[sym::rustc_dump_user_args];
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
15+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
16+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpUserArgs;
17+
}
18+
19+
pub(crate) struct RustcDumpDefParents;
20+
21+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpDefParents {
22+
const PATH: &[Symbol] = &[sym::rustc_dump_def_parents];
23+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
24+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
25+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpDefParents;
26+
}
27+
28+
pub(crate) struct RustcDumpItemBounds;
29+
30+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpItemBounds {
31+
const PATH: &[Symbol] = &[sym::rustc_dump_item_bounds];
32+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
33+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::AssocTy)]);
34+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpItemBounds;
35+
}
36+
37+
pub(crate) struct RustcDumpPredicates;
38+
39+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpPredicates {
40+
const PATH: &[Symbol] = &[sym::rustc_dump_predicates];
41+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
42+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
43+
Allow(Target::Struct),
44+
Allow(Target::Enum),
45+
Allow(Target::Union),
46+
Allow(Target::Trait),
47+
Allow(Target::AssocTy),
48+
]);
49+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDumpPredicates;
50+
}
51+
52+
pub(crate) struct RustcDumpVtable;
53+
54+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDumpVtable {
55+
const PATH: &[Symbol] = &[sym::rustc_dump_vtable];
56+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
57+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
58+
Allow(Target::Impl { of_trait: true }),
59+
Allow(Target::TyAlias),
60+
]);
61+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcDumpVtable;
62+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ use crate::attributes::proc_macro_attrs::{
6363
};
6464
use crate::attributes::prototype::CustomMirParser;
6565
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
66+
use crate::attributes::rustc_dump::{
67+
RustcDumpDefParents, RustcDumpItemBounds, RustcDumpPredicates, RustcDumpUserArgs,
68+
RustcDumpVtable,
69+
};
6670
use crate::attributes::rustc_internal::{
6771
RustcHasIncoherentInherentImplsParser, RustcLayoutScalarValidRangeEndParser,
6872
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
@@ -267,6 +271,11 @@ attribute_parsers!(
267271
Single<WithoutArgs<ProcMacroParser>>,
268272
Single<WithoutArgs<PubTransparentParser>>,
269273
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
274+
Single<WithoutArgs<RustcDumpDefParents>>,
275+
Single<WithoutArgs<RustcDumpItemBounds>>,
276+
Single<WithoutArgs<RustcDumpPredicates>>,
277+
Single<WithoutArgs<RustcDumpUserArgs>>,
278+
Single<WithoutArgs<RustcDumpVtable>>,
270279
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
271280
Single<WithoutArgs<RustcLintDiagnosticsParser>>,
272281
Single<WithoutArgs<RustcLintOptTyParser>>,

‎compiler/rustc_hir/src/attrs/data_structures.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,21 @@ pub enum AttributeKind {
906906
/// Represents `#[rustc_coherence_is_core]`
907907
RustcCoherenceIsCore(Span),
908908

909+
/// Represents `#[rustc_dump_def_parents]`
910+
RustcDumpDefParents,
911+
912+
/// Represents `#[rustc_dump_item_bounds]`
913+
RustcDumpItemBounds,
914+
915+
/// Represents `#[rustc_dump_predicates]`
916+
RustcDumpPredicates,
917+
918+
/// Represents `#[rustc_dump_user_args]`
919+
RustcDumpUserArgs,
920+
921+
/// Represents `#[rustc_dump_vtable]`
922+
RustcDumpVtable(Span),
923+
909924
/// Represents `#[rustc_has_incoherent_inherent_impls]`
910925
RustcHasIncoherentInherentImpls,
911926

‎compiler/rustc_hir/src/attrs/encode_cross_crate.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ impl AttributeKind {
9797
Repr { .. } => No,
9898
RustcBuiltinMacro { .. } => Yes,
9999
RustcCoherenceIsCore(..) => No,
100+
RustcDumpDefParents => No,
101+
RustcDumpItemBounds => No,
102+
RustcDumpPredicates => No,
103+
RustcDumpUserArgs => No,
104+
RustcDumpVtable(..) => No,
100105
RustcHasIncoherentInherentImpls => Yes,
101106
RustcLayoutScalarValidRangeEnd(..) => Yes,
102107
RustcLayoutScalarValidRangeStart(..) => Yes,

‎compiler/rustc_hir_analysis/src/collect/dump.rs‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_hir as hir;
2+
use rustc_hir::attrs::AttributeKind;
23
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
3-
use rustc_hir::intravisit;
4+
use rustc_hir::{find_attr, intravisit};
45
use rustc_middle::hir::nested_filter;
56
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
67
use rustc_span::sym;
@@ -28,7 +29,7 @@ pub(crate) fn opaque_hidden_types(tcx: TyCtxt<'_>) {
2829

2930
pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
3031
for id in tcx.hir_crate_items(()).owners() {
31-
if tcx.has_attr(id, sym::rustc_dump_predicates) {
32+
if find_attr!(tcx.get_all_attrs(id), AttributeKind::RustcDumpPredicates) {
3233
let preds = tcx.predicates_of(id).instantiate_identity(tcx).predicates;
3334
let span = tcx.def_span(id);
3435

@@ -38,7 +39,7 @@ pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
3839
}
3940
diag.emit();
4041
}
41-
if tcx.has_attr(id, sym::rustc_dump_item_bounds) {
42+
if find_attr!(tcx.get_all_attrs(id), AttributeKind::RustcDumpItemBounds) {
4243
let bounds = tcx.item_bounds(id).instantiate_identity();
4344
let span = tcx.def_span(id);
4445

@@ -54,7 +55,7 @@ pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
5455
pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
5556
for iid in tcx.hir_free_items() {
5657
let did = iid.owner_id.def_id;
57-
if tcx.has_attr(did, sym::rustc_dump_def_parents) {
58+
if find_attr!(tcx.get_all_attrs(did), AttributeKind::RustcDumpDefParents) {
5859
struct AnonConstFinder<'tcx> {
5960
tcx: TyCtxt<'tcx>,
6061
anon_consts: Vec<LocalDefId>,
@@ -102,7 +103,9 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
102103
for id in tcx.hir_free_items() {
103104
let def_id = id.owner_id.def_id;
104105

105-
let Some(attr) = tcx.get_attr(def_id, sym::rustc_dump_vtable) else {
106+
let Some(&attr_span) =
107+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcDumpVtable(span) => span)
108+
else {
106109
continue;
107110
};
108111

@@ -111,14 +114,14 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
111114
let trait_ref = tcx.impl_trait_ref(def_id).instantiate_identity();
112115
if trait_ref.has_non_region_param() {
113116
tcx.dcx().span_err(
114-
attr.span(),
117+
attr_span,
115118
"`rustc_dump_vtable` must be applied to non-generic impl",
116119
);
117120
continue;
118121
}
119122
if !tcx.is_dyn_compatible(trait_ref.def_id) {
120123
tcx.dcx().span_err(
121-
attr.span(),
124+
attr_span,
122125
"`rustc_dump_vtable` must be applied to dyn-compatible trait",
123126
);
124127
continue;
@@ -127,7 +130,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
127130
.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), trait_ref)
128131
else {
129132
tcx.dcx().span_err(
130-
attr.span(),
133+
attr_span,
131134
"`rustc_dump_vtable` applied to impl header that cannot be normalized",
132135
);
133136
continue;
@@ -138,7 +141,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
138141
let ty = tcx.type_of(def_id).instantiate_identity();
139142
if ty.has_non_region_param() {
140143
tcx.dcx().span_err(
141-
attr.span(),
144+
attr_span,
142145
"`rustc_dump_vtable` must be applied to non-generic type",
143146
);
144147
continue;
@@ -147,14 +150,13 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
147150
tcx.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty)
148151
else {
149152
tcx.dcx().span_err(
150-
attr.span(),
153+
attr_span,
151154
"`rustc_dump_vtable` applied to type alias that cannot be normalized",
152155
);
153156
continue;
154157
};
155158
let ty::Dynamic(data, _) = *ty.kind() else {
156-
tcx.dcx()
157-
.span_err(attr.span(), "`rustc_dump_vtable` to type alias of dyn type");
159+
tcx.dcx().span_err(attr_span, "`rustc_dump_vtable` to type alias of dyn type");
158160
continue;
159161
};
160162
if let Some(principal) = data.principal() {
@@ -167,7 +169,7 @@ pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
167169
}
168170
_ => {
169171
tcx.dcx().span_err(
170-
attr.span(),
172+
attr_span,
171173
"`rustc_dump_vtable` only applies to impl, or type alias of dyn type",
172174
);
173175
continue;

‎compiler/rustc_hir_typeck/src/writeback.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use std::ops::ControlFlow;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1515
use rustc_data_structures::unord::ExtendUnord;
1616
use rustc_errors::{E0720, ErrorGuaranteed};
17+
use rustc_hir::attrs::AttributeKind;
1718
use rustc_hir::def_id::LocalDefId;
1819
use rustc_hir::intravisit::{self, InferKind, Visitor};
19-
use rustc_hir::{self as hir, AmbigArg, HirId};
20+
use rustc_hir::{self as hir, AmbigArg, HirId, find_attr};
2021
use rustc_infer::traits::solve::Goal;
2122
use rustc_middle::traits::ObligationCause;
2223
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCoercion};
@@ -25,7 +26,7 @@ use rustc_middle::ty::{
2526
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
2627
fold_regions,
2728
};
28-
use rustc_span::{Span, sym};
29+
use rustc_span::Span;
2930
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
3031
use rustc_trait_selection::opaque_types::opaque_type_has_defining_use_args;
3132
use rustc_trait_selection::solve;
@@ -45,8 +46,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4546

4647
// This attribute causes us to dump some writeback information
4748
// in the form of errors, which is used for unit tests.
48-
let rustc_dump_user_args =
49-
self.has_rustc_attrs && self.tcx.has_attr(item_def_id, sym::rustc_dump_user_args);
49+
let rustc_dump_user_args = self.has_rustc_attrs
50+
&& find_attr!(self.tcx.get_all_attrs(item_def_id), AttributeKind::RustcDumpUserArgs);
5051

5152
let mut wbcx = WritebackCx::new(self, body, rustc_dump_user_args);
5253
for param in body.params {

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
309309
| AttributeKind::CfiEncoding { .. }
310310
| AttributeKind::RustcHasIncoherentInherentImpls
311311
| AttributeKind::MustNotSupend { .. }
312+
| AttributeKind::RustcDumpUserArgs
313+
| AttributeKind::RustcDumpItemBounds
314+
| AttributeKind::RustcDumpPredicates
315+
| AttributeKind::RustcDumpDefParents
316+
| AttributeKind::RustcDumpVtable(..)
312317
) => { /* do nothing */ }
313318
Attribute::Unparsed(attr_item) => {
314319
style = Some(attr_item.style);
@@ -368,25 +373,20 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
368373
| sym::rustc_abi
369374
| sym::rustc_layout
370375
| sym::rustc_proc_macro_decls
371-
| sym::rustc_dump_def_parents
372376
| sym::rustc_never_type_options
373377
| sym::rustc_autodiff
374378
| sym::rustc_capture_analysis
375379
| sym::rustc_regions
376380
| sym::rustc_strict_coherence
377-
| sym::rustc_dump_predicates
378381
| sym::rustc_variance
379382
| sym::rustc_variance_of_opaques
380383
| sym::rustc_hidden_type_of_opaques
381384
| sym::rustc_mir
382-
| sym::rustc_dump_user_args
383385
| sym::rustc_effective_visibility
384386
| sym::rustc_outlives
385387
| sym::rustc_symbol_name
386388
| sym::rustc_evaluate_where_clauses
387-
| sym::rustc_dump_vtable
388389
| sym::rustc_delayed_bug_from_inside_query
389-
| sym::rustc_dump_item_bounds
390390
| sym::rustc_def_path
391391
| sym::rustc_partition_reused
392392
| sym::rustc_partition_codegened

0 commit comments

Comments
 (0)