Skip to content

Commit 10f0c2d

Browse files
committed
Port #[rustc_symbol_name] and #[rustc_def_path] to attr parser
1 parent efc9e1b commit 10f0c2d

6 files changed

Lines changed: 77 additions & 12 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
@@ -760,3 +760,53 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
760760
]);
761761
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
762762
}
763+
764+
pub(crate) struct RustcSymbolName;
765+
766+
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
767+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
768+
Allow(Target::Fn),
769+
Allow(Target::Method(MethodKind::TraitImpl)),
770+
Allow(Target::Method(MethodKind::Inherent)),
771+
Allow(Target::Method(MethodKind::Trait { body: true })),
772+
Allow(Target::ForeignFn),
773+
Allow(Target::ForeignStatic),
774+
Allow(Target::Impl { of_trait: false }),
775+
]);
776+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
777+
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
778+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
779+
const TEMPLATE: AttributeTemplate = template!(Word);
780+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
781+
if let Err(span) = args.no_args() {
782+
cx.expected_no_args(span);
783+
return None;
784+
}
785+
Some(AttributeKind::RustcSymbolName(cx.attr_span))
786+
}
787+
}
788+
789+
pub(crate) struct RustcDefPath;
790+
791+
impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
792+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
793+
Allow(Target::Fn),
794+
Allow(Target::Method(MethodKind::TraitImpl)),
795+
Allow(Target::Method(MethodKind::Inherent)),
796+
Allow(Target::Method(MethodKind::Trait { body: true })),
797+
Allow(Target::ForeignFn),
798+
Allow(Target::ForeignStatic),
799+
Allow(Target::Impl { of_trait: false }),
800+
]);
801+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
802+
const PATH: &[Symbol] = &[sym::rustc_def_path];
803+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
804+
const TEMPLATE: AttributeTemplate = template!(Word);
805+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
806+
if let Err(span) = args.no_args() {
807+
cx.expected_no_args(span);
808+
return None;
809+
}
810+
Some(AttributeKind::RustcDefPath(cx.attr_span))
811+
}
812+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ attribute_parsers!(
194194
Single<RustcAbiParser>,
195195
Single<RustcAllocatorZeroedVariantParser>,
196196
Single<RustcBuiltinMacroParser>,
197+
Single<RustcDefPath>,
197198
Single<RustcForceInlineParser>,
198199
Single<RustcIfThisChangedParser>,
199200
Single<RustcLayoutScalarValidRangeEndParser>,
@@ -204,6 +205,7 @@ attribute_parsers!(
204205
Single<RustcObjectLifetimeDefaultParser>,
205206
Single<RustcScalableVectorParser>,
206207
Single<RustcSimdMonomorphizeLaneLimitParser>,
208+
Single<RustcSymbolName>,
207209
Single<SanitizeParser>,
208210
Single<ShouldPanicParser>,
209211
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ pub enum AttributeKind {
10811081
/// Represents `#[rustc_deallocator]`
10821082
RustcDeallocator,
10831083

1084+
/// Represents `#[rustc_def_path]`
1085+
RustcDefPath(Span),
1086+
10841087
/// Represents `#[rustc_deny_explicit_impl]`.
10851088
RustcDenyExplicitImpl(Span),
10861089

@@ -1218,6 +1221,9 @@ pub enum AttributeKind {
12181221
/// Represents `#[rustc_std_internal_symbol]`.
12191222
RustcStdInternalSymbol(Span),
12201223

1224+
/// Represents `#[rustc_symbol_name]`
1225+
RustcSymbolName(Span),
1226+
12211227
/// Represents `#[rustc_then_this_would_need]`
12221228
RustcThenThisWouldNeed(Span, ThinVec<Ident>),
12231229

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl AttributeKind {
105105
RustcConstStability { .. } => Yes,
106106
RustcConstStabilityIndirect => No,
107107
RustcDeallocator => No,
108+
RustcDefPath(..) => No,
108109
RustcDenyExplicitImpl(..) => No,
109110
RustcDummy => No,
110111
RustcDumpDefParents => No,
@@ -149,6 +150,7 @@ impl AttributeKind {
149150
RustcSkipDuringMethodDispatch { .. } => No,
150151
RustcSpecializationTrait(..) => No,
151152
RustcStdInternalSymbol(..) => No,
153+
RustcSymbolName(..) => Yes,
152154
RustcThenThisWouldNeed(..) => No,
153155
RustcUnsafeSpecializationMarker(..) => No,
154156
RustcVariance => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
300300
| AttributeKind::RustcConfusables { .. }
301301
| AttributeKind::RustcConstStabilityIndirect
302302
| AttributeKind::RustcDeallocator
303+
| AttributeKind::RustcDefPath(..)
303304
| AttributeKind::RustcDenyExplicitImpl(..)
304305
| AttributeKind::RustcDummy
305306
| AttributeKind::RustcDumpDefParents
@@ -340,6 +341,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
340341
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
341342
| AttributeKind::RustcSpecializationTrait(..)
342343
| AttributeKind::RustcStdInternalSymbol (..)
344+
| AttributeKind::RustcSymbolName(..)
343345
| AttributeKind::RustcThenThisWouldNeed(..)
344346
| AttributeKind::RustcUnsafeSpecializationMarker(..)
345347
| AttributeKind::RustcVariance
@@ -403,10 +405,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
403405
| sym::rustc_strict_coherence
404406
| sym::rustc_mir
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: 15 additions & 10 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,42 @@ 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) {
55+
56+
if let Some(attr_span) = find_attr!(
57+
tcx.get_all_attrs(def_id),
58+
AttributeKind::RustcSymbolName(span) => span
59+
) {
5860
let def_id = def_id.to_def_id();
5961
let instance = Instance::new_raw(
6062
def_id,
6163
tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
6264
);
6365
let mangled = tcx.symbol_name(instance);
6466
tcx.dcx().emit_err(TestOutput {
65-
span: attr.span(),
67+
span: *attr_span,
6668
kind: Kind::SymbolName,
6769
content: format!("{mangled}"),
6870
});
6971
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
7072
tcx.dcx().emit_err(TestOutput {
71-
span: attr.span(),
73+
span: *attr_span,
7274
kind: Kind::Demangling,
7375
content: format!("{demangling}"),
7476
});
7577
tcx.dcx().emit_err(TestOutput {
76-
span: attr.span(),
78+
span: *attr_span,
7779
kind: Kind::DemanglingAlt,
7880
content: format!("{demangling:#}"),
7981
});
8082
}
8183
}
8284

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

0 commit comments

Comments
 (0)