Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,47 @@ impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
Some(AttributeKind::RustcScalableVector { element_count: Some(n), span: cx.attr_span })
}
}

const SYMBOL_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::ForeignFn),
Allow(Target::ForeignStatic),
Allow(Target::Impl { of_trait: false }),
]);

pub(crate) struct RustcSymbolName;

impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const PATH: &[Symbol] = &[sym::rustc_symbol_name];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
if let Err(span) = args.no_args() {
cx.expected_no_args(span);
return None;
}
Some(AttributeKind::RustcSymbolName(cx.attr_span))
}
}

pub(crate) struct RustcDefPath;

impl<S: Stage> SingleAttributeParser<S> for RustcDefPath {
const ALLOWED_TARGETS: AllowedTargets = SYMBOL_TARGETS;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const PATH: &[Symbol] = &[sym::rustc_def_path];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
if let Err(span) = args.no_args() {
cx.expected_no_args(span);
return None;
}
Some(AttributeKind::RustcDefPath(cx.attr_span))
}
}
6 changes: 4 additions & 2 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ use crate::attributes::proc_macro_attrs::{
use crate::attributes::prototype::CustomMirParser;
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
use crate::attributes::rustc_internal::{
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
RustcDefPath, RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
RustcLegacyConstGenericsParser, RustcLintDiagnosticsParser, RustcLintOptDenyFieldAccessParser,
RustcLintOptTyParser, RustcLintQueryInstabilityParser,
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
RustcSimdMonomorphizeLaneLimitParser,
RustcSimdMonomorphizeLaneLimitParser, RustcSymbolName,
};
use crate::attributes::semantics::MayDangleParser;
use crate::attributes::stability::{
Expand Down Expand Up @@ -215,6 +215,7 @@ attribute_parsers!(
Single<ProcMacroDeriveParser>,
Single<RecursionLimitParser>,
Single<RustcBuiltinMacroParser>,
Single<RustcDefPath>,
Single<RustcForceInlineParser>,
Single<RustcLayoutScalarValidRangeEndParser>,
Single<RustcLayoutScalarValidRangeStartParser>,
Expand All @@ -224,6 +225,7 @@ attribute_parsers!(
Single<RustcObjectLifetimeDefaultParser>,
Single<RustcScalableVectorParser>,
Single<RustcSimdMonomorphizeLaneLimitParser>,
Single<RustcSymbolName>,
Single<SanitizeParser>,
Single<ShouldPanicParser>,
Single<SkipDuringMethodDispatchParser>,
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_coherence_is_core]`
RustcCoherenceIsCore(Span),

/// Represents `#[rustc_def_path]`
RustcDefPath(Span),

/// Represents `#[rustc_layout_scalar_valid_range_end]`.
RustcLayoutScalarValidRangeEnd(Box<u128>, Span),

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

/// Represents `#[rustc_symbol_name]`
RustcSymbolName(Span),

/// Represents `#[sanitize]`
///
/// the on set and off set are distjoint since there's a third option: unset.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl AttributeKind {
Repr { .. } => No,
RustcBuiltinMacro { .. } => Yes,
RustcCoherenceIsCore(..) => No,
RustcDefPath(..) => No,
RustcLayoutScalarValidRangeEnd(..) => Yes,
RustcLayoutScalarValidRangeStart(..) => Yes,
RustcLegacyConstGenerics { .. } => Yes,
Expand All @@ -112,6 +113,7 @@ impl AttributeKind {
RustcScalableVector { .. } => Yes,
RustcShouldNotBeCalledOnConstItems(..) => Yes,
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
RustcSymbolName(..) => Yes,
Sanitize { .. } => No,
ShouldPanic { .. } => No,
SkipDuringMethodDispatch { .. } => No,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,9 @@ impl AttributeExt for Attribute {
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
Attribute::Parsed(AttributeKind::RustcSymbolName(span)) => *span,
Attribute::Parsed(AttributeKind::RustcDefPath(span)) => *span,

a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::CfgAttrTrace
| AttributeKind::ThreadLocal
| AttributeKind::CfiEncoding { .. }
| AttributeKind::RustcSymbolName(..)
| AttributeKind::RustcDefPath(..)
) => { /* do nothing */ }
Attribute::Unparsed(attr_item) => {
style = Some(attr_item.style);
Expand Down
18 changes: 12 additions & 6 deletions compiler/rustc_symbol_mangling/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
//! def-path. This is used for unit testing the code that generates
//! paths etc in all kinds of annoying scenarios.

use rustc_hir::Attribute;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
use rustc_span::{Symbol, sym};

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

const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
const DEF_PATH: Symbol = sym::rustc_def_path;

pub fn report_symbol_names(tcx: TyCtxt<'_>) {
// if the `rustc_attrs` feature is not enabled, then the
// attributes we are interested in cannot be present anyway, so
Expand Down Expand Up @@ -54,7 +52,11 @@ impl SymbolNamesTest<'_> {
// The formatting of `tag({})` is chosen so that tests can elect
// to test the entirety of the string, if they choose, or else just
// some subset.
for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
for attr in tcx
.get_all_attrs(def_id)
.into_iter()
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcSymbolName(..))))
{
let def_id = def_id.to_def_id();
let instance = Instance::new_raw(
def_id,
Expand All @@ -80,7 +82,11 @@ impl SymbolNamesTest<'_> {
}
}

for attr in tcx.get_attrs(def_id, DEF_PATH) {
for attr in tcx
.get_all_attrs(def_id)
.into_iter()
.filter(|attr| matches!(attr, Attribute::Parsed(AttributeKind::RustcDefPath(..))))
{
tcx.dcx().emit_err(TestOutput {
span: attr.span(),
kind: Kind::DefPath,
Expand Down