Skip to content

Commit 0e0bbee

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

7 files changed

Lines changed: 73 additions & 8 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,47 @@ impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
305305
Some(AttributeKind::RustcScalableVector { element_count: Some(n), span: cx.attr_span })
306306
}
307307
}
308+
309+
const SYMBOL_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
310+
Allow(Target::Fn),
311+
Allow(Target::Method(MethodKind::TraitImpl)),
312+
Allow(Target::Method(MethodKind::Inherent)),
313+
Allow(Target::Method(MethodKind::Trait { body: true })),
314+
Allow(Target::ForeignFn),
315+
Allow(Target::ForeignStatic),
316+
Allow(Target::Impl { of_trait: false }),
317+
]);
318+
319+
pub(crate) struct RustcSymbolName;
320+
321+
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
322+
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
323+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
324+
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
325+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
326+
const TEMPLATE: AttributeTemplate = template!(Word);
327+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
328+
if let Err(span) = args.no_args() {
329+
cx.expected_no_args(span);
330+
return None;
331+
}
332+
Some(AttributeKind::RustcSymbolName(cx.attr_span))
333+
}
334+
}
335+
336+
pub(crate) struct RustcDefPath;
337+
338+
impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
339+
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
340+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
341+
const PATH: &[Symbol] = &[sym::rustc_def_path];
342+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
343+
const TEMPLATE: AttributeTemplate = template!(Word);
344+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
345+
if let Err(span) = args.no_args() {
346+
cx.expected_no_args(span);
347+
return None;
348+
}
349+
Some(AttributeKind::RustcDefPath(cx.attr_span))
350+
}
351+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ use crate::attributes::proc_macro_attrs::{
6262
use crate::attributes::prototype::CustomMirParser;
6363
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6464
use crate::attributes::rustc_internal::{
65-
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
65+
RustcDefPath, RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
6666
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser,
6767
RustcLintOptTyParser, RustcLintQueryInstabilityParser,
6868
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
6969
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
7070
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
71-
RustcSimdMonomorphizeLaneLimitParser,
71+
RustcSimdMonomorphizeLaneLimitParser, RustcSymbolName,
7272
};
7373
use crate::attributes::semantics::MayDangleParser;
7474
use crate::attributes::stability::{
@@ -215,6 +215,7 @@ attribute_parsers!(
215215
Single<ProcMacroDeriveParser>,
216216
Single<RecursionLimitParser>,
217217
Single<RustcBuiltinMacroParser>,
218+
Single<RustcDefPath>,
218219
Single<RustcForceInlineParser>,
219220
Single<RustcLayoutScalarValidRangeEndParser>,
220221
Single<RustcLayoutScalarValidRangeStartParser>,
@@ -224,6 +225,7 @@ attribute_parsers!(
224225
Single<RustcObjectLifetimeDefaultParser>,
225226
Single<RustcScalableVectorParser>,
226227
Single<RustcSimdMonomorphizeLaneLimitParser>,
228+
Single<RustcSymbolName>,
227229
Single<SanitizeParser>,
228230
Single<ShouldPanicParser>,
229231
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ pub enum AttributeKind {
880880
/// Represents `#[rustc_coherence_is_core]`
881881
RustcCoherenceIsCore(Span),
882882

883+
/// Represents `#[rustc_def_path]`
884+
RustcDefPath(Span),
885+
883886
/// Represents `#[rustc_layout_scalar_valid_range_end]`.
884887
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),
885888

@@ -936,6 +939,9 @@ pub enum AttributeKind {
936939
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
937940
RustcSimdMonomorphizeLaneLimit(Limit),
938941

942+
/// Represents `#[rustc_symbol_name]`
943+
RustcSymbolName(Span),
944+
939945
/// Represents `#[sanitize]`
940946
///
941947
/// the on set and off set are distjoint since there's a third option: unset.

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl AttributeKind {
9595
Repr { .. } => No,
9696
RustcBuiltinMacro { .. } => Yes,
9797
RustcCoherenceIsCore(..) => No,
98+
RustcDefPath(..) => No,
9899
RustcLayoutScalarValidRangeEnd(..) => Yes,
99100
RustcLayoutScalarValidRangeStart(..) => Yes,
100101
RustcLegacyConstGenerics { .. } => Yes,
@@ -112,6 +113,7 @@ impl AttributeKind {
112113
RustcScalableVector { .. } => Yes,
113114
RustcShouldNotBeCalledOnConstItems(..) => Yes,
114115
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
116+
RustcSymbolName(..) => Yes,
115117
Sanitize { .. } => No,
116118
ShouldPanic { .. } => No,
117119
SkipDuringMethodDispatch { .. } => No,

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,9 @@ impl AttributeExt for Attribute {
13641364
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
13651365
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
13661366
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
1367+
Attribute::Parsed(AttributeKind::RustcSymbolName(span)) => *span,
1368+
Attribute::Parsed(AttributeKind::RustcDefPath(span)) => *span,
1369+
13671370
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13681371
}
13691372
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
306306
| AttributeKind::CfgAttrTrace
307307
| AttributeKind::ThreadLocal
308308
| AttributeKind::CfiEncoding { .. }
309+
| AttributeKind::RustcSymbolName(..)
310+
| AttributeKind::RustcDefPath(..)
309311
) => { /* do nothing */ }
310312
Attribute::Unparsed(attr_item) => {
311313
style = Some(attr_item.style);

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)