Skip to content

Commit e2a69ce

Browse files
committed
Add #[rustc_should_not_be_called_on_const_items] attribute
1 parent e22dab3 commit e2a69ce

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::{
@@ -244,6 +245,7 @@ attribute_parsers!(
244245
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
245246
Single<WithoutArgs<RustcMainParser>>,
246247
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,
248+
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
247249
Single<WithoutArgs<SpecializationTraitParser>>,
248250
Single<WithoutArgs<StdInternalSymbolParser>>,
249251
Single<WithoutArgs<TrackCallerParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12671267
EncodeCrossCrate::Yes,
12681268
"`#[rustc_as_ptr]` is used to mark functions returning pointers to their inner allocations."
12691269
),
1270+
rustc_attr!(
1271+
rustc_should_not_be_called_on_const_items, Normal, template!(Word), ErrorFollowing,
1272+
EncodeCrossCrate::Yes,
1273+
"`#[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."
1274+
),
12701275
rustc_attr!(
12711276
rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
12721277
EncodeCrossCrate::Yes,

compiler/rustc_hir/src/attrs/data_structures.rs

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

704+
/// Represents `#[rustc_should_not_be_called_on_const_items]`
705+
RustcShouldNotBeCalledOnConstItems(Span),
706+
704707
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
705708
RustcSimdMonomorphizeLaneLimit(Limit),
706709

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

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

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
254254
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
255255
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
256256
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
257+
| AttributeKind::RustcShouldNotBeCalledOnConstItems(..)
257258
| AttributeKind::ExportStable
258259
| AttributeKind::FfiConst(..)
259260
| AttributeKind::UnstableFeatureBound(..)

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,7 @@ symbols! {
19861986
rustc_regions,
19871987
rustc_reservation_impl,
19881988
rustc_serialize,
1989+
rustc_should_not_be_called_on_const_items,
19891990
rustc_simd_monomorphize_lane_limit,
19901991
rustc_skip_during_method_dispatch,
19911992
rustc_specialization_trait,

0 commit comments

Comments
 (0)