Skip to content

Commit a1e923d

Browse files
committed
add #[rustc_non_const_trait_method]
1 parent 4742769 commit a1e923d

14 files changed

Lines changed: 107 additions & 10 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser {
329329
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
330330
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel;
331331
}
332+
333+
pub(crate) struct RustcNonConstTraitMethodParser;
334+
335+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
336+
const PATH: &'static [Symbol] = &[sym::rustc_non_const_trait_method];
337+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
338+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
339+
Allow(Target::Method(MethodKind::Trait { body: true })),
340+
Allow(Target::Method(MethodKind::Trait { body: false })),
341+
]);
342+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
343+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ use crate::attributes::rustc_internal::{
7979
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
8080
RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser, RustcLintQueryInstabilityParser,
8181
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
82-
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser, RustcNounwindParser,
83-
RustcObjectLifetimeDefaultParser, RustcOffloadKernelParser, RustcScalableVectorParser,
84-
RustcSimdMonomorphizeLaneLimitParser,
82+
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
83+
RustcNonConstTraitMethodParser, RustcNounwindParser, RustcObjectLifetimeDefaultParser,
84+
RustcOffloadKernelParser, RustcScalableVectorParser, RustcSimdMonomorphizeLaneLimitParser,
8585
};
8686
use crate::attributes::semantics::MayDangleParser;
8787
use crate::attributes::stability::{
@@ -304,6 +304,7 @@ attribute_parsers!(
304304
Single<WithoutArgs<RustcMainParser>>,
305305
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
306306
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
307+
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
307308
Single<WithoutArgs<RustcNounwindParser>>,
308309
Single<WithoutArgs<RustcOffloadKernelParser>>,
309310
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
778778
// than usual.
779779

780780
trace!("attempting to call a trait method");
781-
let trait_is_const = tcx.is_const_trait(trait_did);
781+
let is_const = tcx.constness(callee) == hir::Constness::Const;
782782

783783
// Only consider a trait to be const if the const conditions hold.
784784
// Otherwise, it's really misleading to call something "conditionally"
785785
// const when it's very obviously not conditionally const.
786-
if trait_is_const && has_const_conditions == Some(ConstConditionsHold::Yes) {
786+
if is_const && has_const_conditions == Some(ConstConditionsHold::Yes) {
787787
// Trait calls are always conditionally-const.
788788
self.check_op(ops::ConditionallyConstCall {
789789
callee,

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use rustc_hir::attrs::AttributeKind;
12
use rustc_hir::def_id::{DefId, LocalDefId};
23
use rustc_hir::{
34
Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind,
4-
Node, TraitItem, TraitItemKind, VariantData,
5+
Node, TraitItem, TraitItemKind, VariantData, find_attr,
56
};
67
use rustc_middle::query::Providers;
78
use rustc_middle::ty::TyCtxt;
@@ -36,7 +37,13 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness {
3637
Constness::NotConst => tcx.constness(tcx.local_parent(def_id)),
3738
}
3839
}
39-
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. }) => tcx.trait_def(tcx.local_parent(def_id)).constness,
40+
Node::TraitItem(ti @ TraitItem { kind: TraitItemKind::Fn(..), .. }) => {
41+
if find_attr!(tcx.hir_attrs(ti.hir_id()), AttributeKind::RustcNonConstTraitMethod) {
42+
Constness::NotConst
43+
} else {
44+
tcx.trait_def(tcx.local_parent(def_id)).constness
45+
}
46+
}
4047
_ => {
4148
tcx.dcx().span_bug(
4249
tcx.def_span(def_id),

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,12 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
13291329
"`#[rustc_has_incoherent_inherent_impls]` allows the addition of incoherent inherent impls for \
13301330
the given type by annotating all impl items with `#[rustc_allow_incoherent_impl]`"
13311331
),
1332+
rustc_attr!(
1333+
rustc_non_const_trait_method, AttributeType::Normal, template!(Word),
1334+
ErrorFollowing, EncodeCrossCrate::No,
1335+
"`#[rustc_non_const_trait_method]` should only used by the standard library to mark trait methods \
1336+
as non-const to allow large traits to easier transition to const"
1337+
),
13321338

13331339
BuiltinAttribute {
13341340
name: sym::rustc_diagnostic_item,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,9 @@ pub enum AttributeKind {
11011101
/// Represents `#[rustc_no_implicit_autorefs]`
11021102
RustcNoImplicitAutorefs,
11031103

1104+
/// Represents `#[rustc_non_const_trait_method]`.
1105+
RustcNonConstTraitMethod,
1106+
11041107
/// Represents `#[rustc_nounwind]`
11051108
RustcNounwind,
11061109

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl AttributeKind {
128128
RustcMustImplementOneOf { .. } => No,
129129
RustcNeverReturnsNullPointer => Yes,
130130
RustcNoImplicitAutorefs => Yes,
131+
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
131132
RustcNounwind => No,
132133
RustcObjectLifetimeDefault => No,
133134
RustcOffloadKernel => Yes,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,8 +2180,13 @@ impl<'tcx> TyCtxt<'tcx> {
21802180
DefKind::Impl { of_trait: false } => {
21812181
self.constness(def_id) == hir::Constness::Const
21822182
}
2183-
DefKind::Impl { of_trait: true } | DefKind::Trait => {
2184-
self.is_conditionally_const(parent_def_id)
2183+
DefKind::Impl { of_trait: true } => {
2184+
self.constness(self.trait_item_of(def_id).unwrap()) == hir::Constness::Const
2185+
&& self.is_conditionally_const(parent_def_id)
2186+
}
2187+
DefKind::Trait => {
2188+
self.constness(def_id) == hir::Constness::Const
2189+
&& self.is_conditionally_const(parent_def_id)
21852190
}
21862191
_ => bug!("unexpected parent item of associated fn: {parent_def_id:?}"),
21872192
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
313313
| AttributeKind::RustcMain
314314
| AttributeKind::RustcNeverReturnsNullPointer
315315
| AttributeKind::RustcNoImplicitAutorefs
316+
| AttributeKind::RustcNonConstTraitMethod
316317
| AttributeKind::RustcNounwind
317318
| AttributeKind::RustcOffloadKernel
318319
| AttributeKind::RustcPassIndirectlyInNonRusticAbis(..)
@@ -334,7 +335,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
334335
| AttributeKind::Used { .. }
335336
| AttributeKind::WindowsSubsystem(..)
336337
// tidy-alphabetical-end
337-
338338
) => { /* do nothing */ }
339339
Attribute::Unparsed(attr_item) => {
340340
style = Some(attr_item.style);

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,7 @@ symbols! {
19911991
rustc_no_implicit_autorefs,
19921992
rustc_no_implicit_bounds,
19931993
rustc_no_mir_inline,
1994+
rustc_non_const_trait_method,
19941995
rustc_nonnull_optimization_guaranteed,
19951996
rustc_nounwind,
19961997
rustc_objc_class,

0 commit comments

Comments
 (0)