Skip to content

Commit 1304f22

Browse files
committed
Port #[rustc_symbol_name] and #[rustc_def_path] to attr parser
1 parent 0a13b43 commit 1304f22

6 files changed

Lines changed: 96 additions & 30 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,53 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
721721
Some(ident)
722722
}
723723
}
724+
725+
pub(crate) struct RustcSymbolName;
726+
727+
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
728+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
729+
Allow(Target::Fn),
730+
Allow(Target::Method(MethodKind::TraitImpl)),
731+
Allow(Target::Method(MethodKind::Inherent)),
732+
Allow(Target::Method(MethodKind::Trait { body: true })),
733+
Allow(Target::ForeignFn),
734+
Allow(Target::ForeignStatic),
735+
Allow(Target::Impl { of_trait: false }),
736+
]);
737+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
738+
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
739+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
740+
const TEMPLATE: AttributeTemplate = template!(Word);
741+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
742+
if let Err(span) = args.no_args() {
743+
cx.expected_no_args(span);
744+
return None;
745+
}
746+
Some(AttributeKind::RustcSymbolName(cx.attr_span))
747+
}
748+
}
749+
750+
pub(crate) struct RustcDefPath;
751+
752+
impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
753+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
754+
Allow(Target::Fn),
755+
Allow(Target::Method(MethodKind::TraitImpl)),
756+
Allow(Target::Method(MethodKind::Inherent)),
757+
Allow(Target::Method(MethodKind::Trait { body: true })),
758+
Allow(Target::ForeignFn),
759+
Allow(Target::ForeignStatic),
760+
Allow(Target::Impl { of_trait: false }),
761+
]);
762+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
763+
const PATH: &[Symbol] = &[sym::rustc_def_path];
764+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
765+
const TEMPLATE: AttributeTemplate = template!(Word);
766+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
767+
if let Err(span) = args.no_args() {
768+
cx.expected_no_args(span);
769+
return None;
770+
}
771+
Some(AttributeKind::RustcDefPath(cx.attr_span))
772+
}
773+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ attribute_parsers!(
193193
Single<ReexportTestHarnessMainParser>,
194194
Single<RustcAllocatorZeroedVariantParser>,
195195
Single<RustcBuiltinMacroParser>,
196+
Single<RustcDefPath>,
196197
Single<RustcForceInlineParser>,
197198
Single<RustcIfThisChangedParser>,
198199
Single<RustcLayoutScalarValidRangeEndParser>,
@@ -203,6 +204,7 @@ attribute_parsers!(
203204
Single<RustcObjectLifetimeDefaultParser>,
204205
Single<RustcScalableVectorParser>,
205206
Single<RustcSimdMonomorphizeLaneLimitParser>,
207+
Single<RustcSymbolName>,
206208
Single<SanitizeParser>,
207209
Single<ShouldPanicParser>,
208210
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,9 @@ pub enum AttributeKind {
10701070
/// Represents `#[rustc_deallocator]`
10711071
RustcDeallocator,
10721072

1073+
/// Represents `#[rustc_def_path]`
1074+
RustcDefPath(Span),
1075+
10731076
/// Represents `#[rustc_deny_explicit_impl]`.
10741077
RustcDenyExplicitImpl(Span),
10751078

@@ -1204,6 +1207,9 @@ pub enum AttributeKind {
12041207
/// Represents `#[rustc_std_internal_symbol]`.
12051208
RustcStdInternalSymbol(Span),
12061209

1210+
/// Represents `#[rustc_symbol_name]`
1211+
RustcSymbolName(Span),
1212+
12071213
/// Represents `#[rustc_then_this_would_need]`
12081214
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
12091215

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl AttributeKind {
104104
RustcConstStability { .. } => Yes,
105105
RustcConstStabilityIndirect => No,
106106
RustcDeallocator => No,
107+
RustcDefPath(..) => No,
107108
RustcDenyExplicitImpl(..) => No,
108109
RustcDummy => No,
109110
RustcDumpDefParents => No,
@@ -147,6 +148,7 @@ impl AttributeKind {
147148
RustcSkipDuringMethodDispatch { .. } => No,
148149
RustcSpecializationTrait(..) => No,
149150
RustcStdInternalSymbol(..) => No,
151+
RustcSymbolName(..) => Yes,
150152
RustcThenThisWouldNeed(..) => No,
151153
RustcUnsafeSpecializationMarker(..) => No,
152154
RustcVariance => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
299299
| AttributeKind::RustcConfusables { .. }
300300
| AttributeKind::RustcConstStabilityIndirect
301301
| AttributeKind::RustcDeallocator
302+
| AttributeKind::RustcDefPath(..)
302303
| AttributeKind::RustcDenyExplicitImpl(..)
303304
| AttributeKind::RustcDummy
304305
| AttributeKind::RustcDumpDefParents
@@ -338,6 +339,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
338339
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
339340
| AttributeKind::RustcSpecializationTrait(..)
340341
| AttributeKind::RustcStdInternalSymbol (..)
342+
| AttributeKind::RustcSymbolName(..)
341343
| AttributeKind::RustcThenThisWouldNeed(..)
342344
| AttributeKind::RustcUnsafeSpecializationMarker(..)
343345
| AttributeKind::RustcVariance
@@ -403,10 +405,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
403405
| sym::rustc_mir
404406
| sym::rustc_effective_visibility
405407
| sym::rustc_outlives
406-
| sym::rustc_symbol_name
407408
| sym::rustc_evaluate_where_clauses
408409
| sym::rustc_delayed_bug_from_inside_query
409-
| sym::rustc_def_path
410410
| sym::rustc_partition_reused
411411
| sym::rustc_partition_codegened
412412
| sym::rustc_expected_cgu_reuse

compiler/rustc_symbol_mangling/src/test.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
//! def-path. This is used for unit testing the code that generates
55
//! paths etc in all kinds of annoying scenarios.
66
7+
use rustc_hir::attrs::AttributeKind;
78
use rustc_hir::def_id::LocalDefId;
9+
use rustc_hir::find_attr;
810
use rustc_middle::ty::print::with_no_trimmed_paths;
911
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
10-
use rustc_span::{Symbol, sym};
1112

1213
use crate::errors::{Kind, TestOutput};
1314

14-
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
15-
const DEF_PATH: Symbol = sym::rustc_def_path;
16-
1715
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
1816
// if the `rustc_attrs` feature is not enabled, then the
1917
// attributes we are interested in cannot be present anyway, so
@@ -54,35 +52,43 @@ impl SymbolNamesTest<'_> {
5452
// The formatting of `tag({})` is chosen so that tests can elect
5553
// to test the entirety of the string, if they choose, or else just
5654
// some subset.
57-
for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
58-
let def_id = def_id.to_def_id();
59-
let instance = Instance::new_raw(
60-
def_id,
61-
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
62-
);
63-
let mangled = tcx.symbol_name(instance);
55+
56+
let Some(attr_span) = find_attr!(
57+
tcx.get_all_attrs(def_id),
58+
AttributeKind::RustcSymbolName(span) => span
59+
) else {
60+
return;
61+
};
62+
let def_id = def_id.to_def_id();
63+
let instance = Instance::new_raw(
64+
def_id,
65+
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
66+
);
67+
let mangled = tcx.symbol_name(instance);
68+
tcx.dcx().emit_err(TestOutput {
69+
span: *attr_span,
70+
kind: Kind::SymbolName,
71+
content: format!("{mangled}"),
72+
});
73+
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
74+
tcx.dcx().emit_err(TestOutput {
75+
span: *attr_span,
76+
kind: Kind::Demangling,
77+
content: format!("{demangling}"),
78+
});
6479
tcx.dcx().emit_err(TestOutput {
65-
span: attr.span(),
66-
kind: Kind::SymbolName,
67-
content: format!("{mangled}"),
80+
span: *attr_span,
81+
kind: Kind::DemanglingAlt,
82+
content: format!("{demangling:#}"),
6883
});
69-
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
70-
tcx.dcx().emit_err(TestOutput {
71-
span: attr.span(),
72-
kind: Kind::Demangling,
73-
content: format!("{demangling}"),
74-
});
75-
tcx.dcx().emit_err(TestOutput {
76-
span: attr.span(),
77-
kind: Kind::DemanglingAlt,
78-
content: format!("{demangling:#}"),
79-
});
80-
}
8184
}
8285

83-
for attr in tcx.get_attrs(def_id, DEF_PATH) {
86+
if let Some(attr_span) = find_attr!(
87+
tcx.get_all_attrs(def_id),
88+
AttributeKind::RustcDefPath(span) => span
89+
) {
8490
tcx.dcx().emit_err(TestOutput {
85-
span: attr.span(),
91+
span: *attr_span,
8692
kind: Kind::DefPath,
8793
content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
8894
});

0 commit comments

Comments
 (0)