Skip to content

Commit 62fd7ad

Browse files
committed
Port #[rustc_symbol_name] and #[rustc_def_path] to attr parser
1 parent db3e99b commit 62fd7ad

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
@@ -497,3 +497,53 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
497497
]);
498498
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
499499
}
500+
501+
pub(crate) struct RustcSymbolName;
502+
503+
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
504+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
505+
Allow(Target::Fn),
506+
Allow(Target::Method(MethodKind::TraitImpl)),
507+
Allow(Target::Method(MethodKind::Inherent)),
508+
Allow(Target::Method(MethodKind::Trait { body: true })),
509+
Allow(Target::ForeignFn),
510+
Allow(Target::ForeignStatic),
511+
Allow(Target::Impl { of_trait: false }),
512+
]);
513+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
514+
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
515+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
516+
const TEMPLATE: AttributeTemplate = template!(Word);
517+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
518+
if let Err(span) = args.no_args() {
519+
cx.expected_no_args(span);
520+
return None;
521+
}
522+
Some(AttributeKind::RustcSymbolName(cx.attr_span))
523+
}
524+
}
525+
526+
pub(crate) struct RustcDefPath;
527+
528+
impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
529+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
530+
Allow(Target::Fn),
531+
Allow(Target::Method(MethodKind::TraitImpl)),
532+
Allow(Target::Method(MethodKind::Inherent)),
533+
Allow(Target::Method(MethodKind::Trait { body: true })),
534+
Allow(Target::ForeignFn),
535+
Allow(Target::ForeignStatic),
536+
Allow(Target::Impl { of_trait: false }),
537+
]);
538+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
539+
const PATH: &[Symbol] = &[sym::rustc_def_path];
540+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
541+
const TEMPLATE: AttributeTemplate = template!(Word);
542+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
543+
if let Err(span) = args.no_args() {
544+
cx.expected_no_args(span);
545+
return None;
546+
}
547+
Some(AttributeKind::RustcDefPath(cx.attr_span))
548+
}
549+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ attribute_parsers!(
190190
Single<RecursionLimitParser>,
191191
Single<RustcAllocatorZeroedVariantParser>,
192192
Single<RustcBuiltinMacroParser>,
193+
Single<RustcDefPath>,
193194
Single<RustcForceInlineParser>,
194195
Single<RustcLayoutScalarValidRangeEndParser>,
195196
Single<RustcLayoutScalarValidRangeStartParser>,
@@ -199,6 +200,7 @@ attribute_parsers!(
199200
Single<RustcObjectLifetimeDefaultParser>,
200201
Single<RustcScalableVectorParser>,
201202
Single<RustcSimdMonomorphizeLaneLimitParser>,
203+
Single<RustcSymbolName>,
202204
Single<SanitizeParser>,
203205
Single<ShouldPanicParser>,
204206
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,9 @@ pub enum AttributeKind {
10471047
/// Represents `#[rustc_deallocator]`
10481048
RustcDeallocator,
10491049

1050+
/// Represents `#[rustc_def_path]`
1051+
RustcDefPath(Span),
1052+
10501053
/// Represents `#[rustc_deny_explicit_impl]`.
10511054
RustcDenyExplicitImpl(Span),
10521055

@@ -1178,6 +1181,9 @@ pub enum AttributeKind {
11781181
/// Represents `#[rustc_std_internal_symbol]`.
11791182
RustcStdInternalSymbol(Span),
11801183

1184+
/// Represents `#[rustc_symbol_name]`
1185+
RustcSymbolName(Span),
1186+
11811187
/// Represents `#[rustc_unsafe_specialization_marker]`.
11821188
RustcUnsafeSpecializationMarker(Span),
11831189

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl AttributeKind {
102102
RustcConstStability { .. } => Yes,
103103
RustcConstStabilityIndirect => No,
104104
RustcDeallocator => No,
105+
RustcDefPath(..) => No,
105106
RustcDenyExplicitImpl(..) => No,
106107
RustcDummy => No,
107108
RustcDumpDefParents => No,
@@ -144,6 +145,7 @@ impl AttributeKind {
144145
RustcSkipDuringMethodDispatch { .. } => No,
145146
RustcSpecializationTrait(..) => No,
146147
RustcStdInternalSymbol(..) => No,
148+
RustcSymbolName(..) => Yes,
147149
RustcUnsafeSpecializationMarker(..) => No,
148150
RustcVariance => No,
149151
RustcVarianceOfOpaques => 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
@@ -297,6 +297,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
297297
| AttributeKind::RustcConfusables { .. }
298298
| AttributeKind::RustcConstStabilityIndirect
299299
| AttributeKind::RustcDeallocator
300+
| AttributeKind::RustcDefPath(..)
300301
| AttributeKind::RustcDenyExplicitImpl(..)
301302
| AttributeKind::RustcDummy
302303
| AttributeKind::RustcDumpDefParents
@@ -335,6 +336,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
335336
| AttributeKind::RustcSkipDuringMethodDispatch { .. }
336337
| AttributeKind::RustcSpecializationTrait(..)
337338
| AttributeKind::RustcStdInternalSymbol (..)
339+
| AttributeKind::RustcSymbolName(..)
338340
| AttributeKind::RustcUnsafeSpecializationMarker(..)
339341
| AttributeKind::RustcVariance
340342
| AttributeKind::RustcVarianceOfOpaques

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)