Skip to content

Commit 47d8a87

Browse files
Rollup merge of #151335 - port_allocator_attrs, r=JonathanBrouwer
Port rustc allocator attributes to attribute parser Tracking issue: #131229 Made a new file since i saw this pattern for `rustc_dump.rs` Very simple attributes so felt like I can do them all in one PR r? @JonathanBrouwer
2 parents 3222939 + 9a931e8 commit 47d8a87

8 files changed

Lines changed: 109 additions & 14 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) mod pin_v2;
5858
pub(crate) mod proc_macro_attrs;
5959
pub(crate) mod prototype;
6060
pub(crate) mod repr;
61+
pub(crate) mod rustc_allocator;
6162
pub(crate) mod rustc_dump;
6263
pub(crate) mod rustc_internal;
6364
pub(crate) mod semantics;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct RustcAllocatorParser;
4+
5+
impl<S: Stage> NoArgsAttributeParser<S> for RustcAllocatorParser {
6+
const PATH: &[Symbol] = &[sym::rustc_allocator];
7+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
8+
const ALLOWED_TARGETS: AllowedTargets =
9+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
10+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcAllocator;
11+
}
12+
13+
pub(crate) struct RustcAllocatorZeroedParser;
14+
15+
impl<S: Stage> NoArgsAttributeParser<S> for RustcAllocatorZeroedParser {
16+
const PATH: &[Symbol] = &[sym::rustc_allocator_zeroed];
17+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
18+
const ALLOWED_TARGETS: AllowedTargets =
19+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
20+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcAllocatorZeroed;
21+
}
22+
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+
42+
pub(crate) struct RustcDeallocatorParser;
43+
44+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDeallocatorParser {
45+
const PATH: &[Symbol] = &[sym::rustc_deallocator];
46+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
47+
const ALLOWED_TARGETS: AllowedTargets =
48+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
49+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDeallocator;
50+
}
51+
52+
pub(crate) struct RustcReallocatorParser;
53+
54+
impl<S: Stage> NoArgsAttributeParser<S> for RustcReallocatorParser {
55+
const PATH: &[Symbol] = &[sym::rustc_reallocator];
56+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
57+
const ALLOWED_TARGETS: AllowedTargets =
58+
AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
59+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcReallocator;
60+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ use crate::attributes::proc_macro_attrs::{
6464
};
6565
use crate::attributes::prototype::CustomMirParser;
6666
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
67+
use crate::attributes::rustc_allocator::{
68+
RustcAllocatorParser, RustcAllocatorZeroedParser, RustcAllocatorZeroedVariantParser,
69+
RustcDeallocatorParser, RustcReallocatorParser,
70+
};
6771
use crate::attributes::rustc_dump::{
6872
RustcDumpDefParents, RustcDumpItemBounds, RustcDumpPredicates, RustcDumpUserArgs,
6973
RustcDumpVtable,
@@ -223,6 +227,7 @@ attribute_parsers!(
223227
Single<PatternComplexityLimitParser>,
224228
Single<ProcMacroDeriveParser>,
225229
Single<RecursionLimitParser>,
230+
Single<RustcAllocatorZeroedVariantParser>,
226231
Single<RustcBuiltinMacroParser>,
227232
Single<RustcForceInlineParser>,
228233
Single<RustcLayoutScalarValidRangeEndParser>,
@@ -273,7 +278,10 @@ attribute_parsers!(
273278
Single<WithoutArgs<ProcMacroAttributeParser>>,
274279
Single<WithoutArgs<ProcMacroParser>>,
275280
Single<WithoutArgs<PubTransparentParser>>,
281+
Single<WithoutArgs<RustcAllocatorParser>>,
282+
Single<WithoutArgs<RustcAllocatorZeroedParser>>,
276283
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
284+
Single<WithoutArgs<RustcDeallocatorParser>>,
277285
Single<WithoutArgs<RustcDumpDefParents>>,
278286
Single<WithoutArgs<RustcDumpItemBounds>>,
279287
Single<WithoutArgs<RustcDumpPredicates>>,
@@ -288,6 +296,7 @@ attribute_parsers!(
288296
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
289297
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
290298
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,
299+
Single<WithoutArgs<RustcReallocatorParser>>,
291300
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
292301
Single<WithoutArgs<SpecializationTraitParser>>,
293302
Single<WithoutArgs<StdInternalSymbolParser>>,

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_codegen_ssa/src/codegen_attrs.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ fn process_builtin_attrs(
335335
AttributeKind::InstructionSet(instruction_set) => {
336336
codegen_fn_attrs.instruction_set = Some(*instruction_set)
337337
}
338+
AttributeKind::RustcAllocator => {
339+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR
340+
}
341+
AttributeKind::RustcDeallocator => {
342+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR
343+
}
344+
AttributeKind::RustcReallocator => {
345+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR
346+
}
347+
AttributeKind::RustcAllocatorZeroed => {
348+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
349+
}
338350
_ => {}
339351
}
340352
}
@@ -344,13 +356,7 @@ fn process_builtin_attrs(
344356
};
345357

346358
match name {
347-
sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
348359
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
349-
sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
350-
sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
351-
sym::rustc_allocator_zeroed => {
352-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
353-
}
354360
sym::patchable_function_entry => {
355361
codegen_fn_attrs.patchable_function_entry =
356362
parse_patchable_function_entry(tcx, attr);

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,12 +906,24 @@ pub enum AttributeKind {
906906
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
907907
Repr { reprs: ThinVec<(ReprAttr, Span)>, first_span: Span },
908908

909+
/// Represents `#[rustc_allocator]`
910+
RustcAllocator,
911+
912+
/// Represents `#[rustc_allocator_zeroed]`
913+
RustcAllocatorZeroed,
914+
915+
/// Represents `#[rustc_allocator_zeroed_variant]`
916+
RustcAllocatorZeroedVariant { name: Symbol },
917+
909918
/// Represents `#[rustc_builtin_macro]`.
910919
RustcBuiltinMacro { builtin_name: Option<Symbol>, helper_attrs: ThinVec<Symbol>, span: Span },
911920

912921
/// Represents `#[rustc_coherence_is_core]`
913922
RustcCoherenceIsCore(Span),
914923

924+
/// Represents `#[rustc_deallocator]`
925+
RustcDeallocator,
926+
915927
/// Represents `#[rustc_dump_def_parents]`
916928
RustcDumpDefParents,
917929

@@ -972,6 +984,9 @@ pub enum AttributeKind {
972984
/// Represents `#[rustc_pass_indirectly_in_non_rustic_abis]`
973985
RustcPassIndirectlyInNonRusticAbis(Span),
974986

987+
/// Represents `#[rustc_reallocator]`
988+
RustcReallocator,
989+
975990
/// Represents `#[rustc_scalable_vector(N)]`
976991
RustcScalableVector {
977992
/// The base multiple of lanes that are in a scalable vector, if provided. `element_count`

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ impl AttributeKind {
9797
PubTransparent(..) => Yes,
9898
RecursionLimit { .. } => No,
9999
Repr { .. } => No,
100+
RustcAllocator => No,
101+
RustcAllocatorZeroed => No,
102+
RustcAllocatorZeroedVariant { .. } => Yes,
100103
RustcBuiltinMacro { .. } => Yes,
101104
RustcCoherenceIsCore(..) => No,
105+
RustcDeallocator => No,
102106
RustcDumpDefParents => No,
103107
RustcDumpItemBounds => No,
104108
RustcDumpPredicates => No,
@@ -119,6 +123,7 @@ impl AttributeKind {
119123
RustcNoImplicitAutorefs => Yes,
120124
RustcObjectLifetimeDefault => No,
121125
RustcPassIndirectlyInNonRusticAbis(..) => No,
126+
RustcReallocator => No,
122127
RustcScalableVector { .. } => Yes,
123128
RustcShouldNotBeCalledOnConstItems(..) => Yes,
124129
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
316316
| AttributeKind::RustcDumpDefParents
317317
| AttributeKind::RustcDumpVtable(..)
318318
| AttributeKind::NeedsAllocator
319+
| AttributeKind::RustcAllocator
320+
| AttributeKind::RustcAllocatorZeroed
321+
| AttributeKind::RustcAllocatorZeroedVariant { .. }
322+
| AttributeKind::RustcDeallocator
323+
| AttributeKind::RustcReallocator
319324
) => { /* do nothing */ }
320325
Attribute::Unparsed(attr_item) => {
321326
style = Some(attr_item.style);
@@ -360,12 +365,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
360365
| sym::rustc_do_not_const_check
361366
| sym::rustc_reservation_impl
362367
| sym::rustc_doc_primitive
363-
| sym::rustc_allocator
364-
| sym::rustc_deallocator
365-
| sym::rustc_reallocator
366368
| sym::rustc_conversion_suggestion
367-
| sym::rustc_allocator_zeroed
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)