Skip to content

Commit 06dacaa

Browse files
committed
Add #[rustc_should_not_be_called_on_const_items] attribute
1 parent c90bcb9 commit 06dacaa

File tree

7 files changed

+24
-0
lines changed

7 files changed

+24
-0
lines changed

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ impl<S: Stage> NoArgsAttributeParser<S> for PassByValueParser {
3838
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PassByValue;
3939
}
4040

41+
pub(crate) struct RustcShouldNotBeCalledOnConstItems;
42+
impl<S: Stage> NoArgsAttributeParser<S> for RustcShouldNotBeCalledOnConstItems {
43+
const PATH: &[Symbol] = &[sym::rustc_should_not_be_called_on_const_items];
44+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
45+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
46+
Allow(Target::Method(MethodKind::Inherent)),
47+
Allow(Target::Method(MethodKind::TraitImpl)),
48+
]);
49+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcShouldNotBeCalledOnConstItems;
50+
}
51+
4152
pub(crate) struct AutomaticallyDerivedParser;
4253
impl<S: Stage> NoArgsAttributeParser<S> for AutomaticallyDerivedParser {
4354
const PATH: &[Symbol] = &[sym::automatically_derived];

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::attributes::link_attrs::{
3939
};
4040
use crate::attributes::lint_helpers::{
4141
AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser,
42+
RustcShouldNotBeCalledOnConstItems,
4243
};
4344
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
4445
use crate::attributes::macro_attrs::{
@@ -245,6 +246,7 @@ attribute_parsers!(
245246
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
246247
Single<WithoutArgs<RustcMainParser>>,
247248
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,
249+
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
248250
Single<WithoutArgs<SpecializationTraitParser>>,
249251
Single<WithoutArgs<StdInternalSymbolParser>>,
250252
Single<WithoutArgs<TrackCallerParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12581258
EncodeCrossCrate::Yes,
12591259
"`#[rustc_as_ptr]` is used to mark functions returning pointers to their inner allocations."
12601260
),
1261+
rustc_attr!(
1262+
rustc_should_not_be_called_on_const_items, Normal, template!(Word), ErrorFollowing,
1263+
EncodeCrossCrate::Yes,
1264+
"`#[rustc_should_not_be_called_on_const_items]` is used to mark methods that don't make sense to be called on interior mutable consts."
1265+
),
12611266
rustc_attr!(
12621267
rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
12631268
EncodeCrossCrate::Yes,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ pub enum AttributeKind {
682682
/// Represents `#[rustc_pass_indirectly_in_non_rustic_abis]`
683683
RustcPassIndirectlyInNonRusticAbis(Span),
684684

685+
/// Represents `#[rustc_should_not_be_called_on_const_items]`
686+
RustcShouldNotBeCalledOnConstItems(Span),
687+
685688
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
686689
RustcSimdMonomorphizeLaneLimit(Limit),
687690

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl AttributeKind {
9292
RustcMain => No,
9393
RustcObjectLifetimeDefault => No,
9494
RustcPassIndirectlyInNonRusticAbis(..) => No,
95+
RustcShouldNotBeCalledOnConstItems(..) => Yes,
9596
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
9697
Sanitize { .. } => No,
9798
ShouldPanic { .. } => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
257257
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
258258
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
259259
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
260+
| AttributeKind::RustcShouldNotBeCalledOnConstItems(..)
260261
| AttributeKind::ExportStable
261262
| AttributeKind::FfiConst(..)
262263
| AttributeKind::UnstableFeatureBound(..)

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,7 @@ symbols! {
19791979
rustc_regions,
19801980
rustc_reservation_impl,
19811981
rustc_serialize,
1982+
rustc_should_not_be_called_on_const_items,
19821983
rustc_simd_monomorphize_lane_limit,
19831984
rustc_skip_during_method_dispatch,
19841985
rustc_specialization_trait,

0 commit comments

Comments
 (0)