Skip to content

Commit 9a931e8

Browse files
committed
Port #[rustc_allocator_zeroed_variant] to attr parser
1 parent 21c9bd7 commit 9a931e8

6 files changed

Lines changed: 29 additions & 6 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_allocator.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcAllocatorZeroedParser {
2020
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcAllocatorZeroed;
2121
}
2222

23+
pub(crate) struct RustcAllocatorZeroedVariantParser;
24+
25+
impl<S: Stage> SingleAttributeParser<S> for RustcAllocatorZeroedVariantParser {
26+
const PATH: &[Symbol] = &[sym::rustc_allocator_zeroed_variant];
27+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
28+
const ALLOWED_TARGETS: AllowedTargets =
29+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
30+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "function");
31+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
32+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
33+
let Some(name) = args.name_value().and_then(NameValueParser::value_as_str) else {
34+
cx.expected_name_value(cx.attr_span, None);
35+
return None;
36+
};
37+
38+
Some(AttributeKind::RustcAllocatorZeroedVariant { name })
39+
}
40+
}
41+
2342
pub(crate) struct RustcDeallocatorParser;
2443

2544
impl<S: Stage> NoArgsAttributeParser<S> for RustcDeallocatorParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ use crate::attributes::proc_macro_attrs::{
6565
use crate::attributes::prototype::CustomMirParser;
6666
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6767
use crate::attributes::rustc_allocator::{
68-
RustcAllocatorParser, RustcAllocatorZeroedParser, RustcDeallocatorParser,
69-
RustcReallocatorParser,
68+
RustcAllocatorParser, RustcAllocatorZeroedParser, RustcAllocatorZeroedVariantParser,
69+
RustcDeallocatorParser, RustcReallocatorParser,
7070
};
7171
use crate::attributes::rustc_dump::{
7272
RustcDumpDefParents, RustcDumpItemBounds, RustcDumpPredicates, RustcDumpUserArgs,
@@ -227,6 +227,7 @@ attribute_parsers!(
227227
Single<PatternComplexityLimitParser>,
228228
Single<ProcMacroDeriveParser>,
229229
Single<RecursionLimitParser>,
230+
Single<RustcAllocatorZeroedVariantParser>,
230231
Single<RustcBuiltinMacroParser>,
231232
Single<RustcForceInlineParser>,
232233
Single<RustcLayoutScalarValidRangeEndParser>,

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Set and unset common attributes on LLVM values.
22
use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr, RtsanSetting};
33
use rustc_hir::def_id::DefId;
4+
use rustc_hir::find_attr;
45
use rustc_middle::middle::codegen_fn_attrs::{
56
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
67
};
@@ -470,9 +471,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
470471
{
471472
to_add.push(create_alloc_family_attr(cx.llcx));
472473
if let Some(instance) = instance
473-
&& let Some(zv) =
474-
tcx.get_attr(instance.def_id(), rustc_span::sym::rustc_allocator_zeroed_variant)
475-
&& let Some(name) = zv.value_str()
474+
&& let Some(name) = find_attr!(tcx.get_all_attrs(instance.def_id()), rustc_hir::attrs::AttributeKind::RustcAllocatorZeroedVariant {name} => name)
476475
{
477476
to_add.push(llvm::CreateAttrStringValue(
478477
cx.llcx,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,9 @@ pub enum AttributeKind {
912912
/// Represents `#[rustc_allocator_zeroed]`
913913
RustcAllocatorZeroed,
914914

915+
/// Represents `#[rustc_allocator_zeroed_variant]`
916+
RustcAllocatorZeroedVariant { name: Symbol },
917+
915918
/// Represents `#[rustc_builtin_macro]`.
916919
RustcBuiltinMacro { builtin_name: Option<Symbol>, helper_attrs: ThinVec<Symbol>, span: Span },
917920

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl AttributeKind {
9999
Repr { .. } => No,
100100
RustcAllocator => No,
101101
RustcAllocatorZeroed => No,
102+
RustcAllocatorZeroedVariant { .. } => Yes,
102103
RustcBuiltinMacro { .. } => Yes,
103104
RustcCoherenceIsCore(..) => No,
104105
RustcDeallocator => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
318318
| AttributeKind::NeedsAllocator
319319
| AttributeKind::RustcAllocator
320320
| AttributeKind::RustcAllocatorZeroed
321+
| AttributeKind::RustcAllocatorZeroedVariant { .. }
321322
| AttributeKind::RustcDeallocator
322323
| AttributeKind::RustcReallocator
323324
) => { /* do nothing */ }
@@ -365,7 +366,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
365366
| sym::rustc_reservation_impl
366367
| sym::rustc_doc_primitive
367368
| sym::rustc_conversion_suggestion
368-
| sym::rustc_allocator_zeroed_variant
369369
| sym::rustc_deprecated_safe_2024
370370
| sym::rustc_test_marker
371371
| sym::rustc_abi

0 commit comments

Comments
 (0)