Skip to content

Commit ca6ebc5

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

7 files changed

Lines changed: 77 additions & 6 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_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,9 @@ impl AttributeExt for Attribute {
13721372
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13731373
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
13741374
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
1375+
Attribute::Parsed(AttributeKind::RustcSymbolName(span)) => *span,
1376+
Attribute::Parsed(AttributeKind::RustcDefPath(span)) => *span,
1377+
13751378
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13761379
}
13771380
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 0 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

compiler/rustc_symbol_mangling/src/test.rs

Lines changed: 12 additions & 6 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::Attribute;
8+
use rustc_hir::attrs::AttributeKind;
79
use rustc_hir::def_id::LocalDefId;
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,7 +52,11 @@ 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+
for attr in tcx
56+
.get_all_attrs(def_id)
57+
.into_iter()
58+
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcSymbolName(..))))
59+
{
5860
let def_id = def_id.to_def_id();
5961
let instance = Instance::new_raw(
6062
def_id,
@@ -80,7 +82,11 @@ impl SymbolNamesTest<'_> {
8082
}
8183
}
8284

83-
for attr in tcx.get_attrs(def_id, DEF_PATH) {
85+
for attr in tcx
86+
.get_all_attrs(def_id)
87+
.into_iter()
88+
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcDefPath(..))))
89+
{
8490
tcx.dcx().emit_err(TestOutput {
8591
span: attr.span(),
8692
kind: Kind::DefPath,

0 commit comments

Comments
 (0)