Skip to content

Commit 347fce9

Browse files
committed
Port rustc_do_not_const_check to the new attribute parser
1 parent 39219ce commit 347fce9

9 files changed

Lines changed: 35 additions & 7 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,21 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
778778
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
779779
}
780780

781+
pub(crate) struct RustcDoNotConstCheckParser;
782+
783+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
784+
const PATH: &[Symbol] = &[sym::rustc_do_not_const_check];
785+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
786+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
787+
Allow(Target::Fn),
788+
Allow(Target::Method(MethodKind::Inherent)),
789+
Allow(Target::Method(MethodKind::TraitImpl)),
790+
Allow(Target::Method(MethodKind::Trait { body: false })),
791+
Allow(Target::Method(MethodKind::Trait { body: true })),
792+
]);
793+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
794+
}
795+
781796
pub(crate) struct RustcSymbolName;
782797

783798
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ attribute_parsers!(
263263
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
264264
Single<WithoutArgs<RustcDeallocatorParser>>,
265265
Single<WithoutArgs<RustcDelayedBugFromInsideQueryParser>>,
266+
Single<WithoutArgs<RustcDoNotConstCheckParser>>,
266267
Single<WithoutArgs<RustcDumpDefParentsParser>>,
267268
Single<WithoutArgs<RustcDumpItemBoundsParser>>,
268269
Single<WithoutArgs<RustcDumpPredicatesParser>>,

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use std::ops::Deref;
77

88
use rustc_data_structures::assert_matches;
99
use rustc_errors::{Diag, ErrorGuaranteed};
10+
use rustc_hir::attrs::AttributeKind;
1011
use rustc_hir::def::DefKind;
1112
use rustc_hir::def_id::DefId;
12-
use rustc_hir::{self as hir, LangItem};
13+
use rustc_hir::{self as hir, LangItem, find_attr};
1314
use rustc_index::bit_set::DenseBitSet;
1415
use rustc_infer::infer::TyCtxtInferExt;
1516
use rustc_middle::mir::visit::Visitor;
@@ -215,7 +216,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
215216
return;
216217
}
217218

218-
if !tcx.has_attr(def_id, sym::rustc_do_not_const_check) {
219+
if !find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcDoNotConstCheck) {
219220
self.visit_body(body);
220221
}
221222

compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_hir::attrs::AttributeKind;
2+
use rustc_hir::find_attr;
13
use rustc_middle::mir::visit::Visitor;
24
use rustc_middle::mir::{self, BasicBlock, Location};
35
use rustc_middle::ty::TyCtxt;
@@ -34,7 +36,7 @@ pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
3436
return;
3537
}
3638

37-
if tcx.has_attr(body.source.def_id(), sym::rustc_do_not_const_check) {
39+
if find_attr!(tcx.get_all_attrs(body.source.def_id()), AttributeKind::RustcDoNotConstCheck) {
3840
return;
3941
}
4042

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use rustc_abi::{Align, Size};
66
use rustc_ast::Mutability;
77
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry};
88
use rustc_errors::inline_fluent;
9+
use rustc_hir::attrs::AttributeKind;
910
use rustc_hir::def_id::{DefId, LocalDefId};
10-
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem};
11+
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem, find_attr};
1112
use rustc_middle::mir::AssertMessage;
1213
use rustc_middle::mir::interpret::{Pointer, ReportedErrorInfo};
1314
use rustc_middle::query::TyCtxtAt;
@@ -440,7 +441,9 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
440441
// sensitive check here. But we can at least rule out functions that are not const at
441442
// all. That said, we have to allow calling functions inside a `const trait`. These
442443
// *are* const-checked!
443-
if !ecx.tcx.is_const_fn(def) || ecx.tcx.has_attr(def, sym::rustc_do_not_const_check) {
444+
if !ecx.tcx.is_const_fn(def)
445+
|| find_attr!(ecx.tcx.get_all_attrs(def), AttributeKind::RustcDoNotConstCheck)
446+
{
444447
// We certainly do *not* want to actually call the fn
445448
// though, so be sure we return here.
446449
throw_unsup_format!("calling non-const function `{}`", instance)

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,9 @@ pub enum AttributeKind {
10901090
/// Represents `#[rustc_deny_explicit_impl]`.
10911091
RustcDenyExplicitImpl(Span),
10921092

1093+
/// Represents `#[rustc_do_not_const_check]`
1094+
RustcDoNotConstCheck,
1095+
10931096
/// Represents `#[rustc_dummy]`.
10941097
RustcDummy,
10951098

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl AttributeKind {
108108
RustcDefPath(..) => No,
109109
RustcDelayedBugFromInsideQuery => No,
110110
RustcDenyExplicitImpl(..) => No,
111+
RustcDoNotConstCheck => Yes,
111112
RustcDummy => No,
112113
RustcDumpDefParents => No,
113114
RustcDumpItemBounds => No,

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
914914
}
915915

916916
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
917-
if self.has_rustc_attrs && self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
917+
if self.has_rustc_attrs
918+
&& find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::RustcDoNotConstCheck)
919+
{
918920
return;
919921
}
920922

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
303303
| AttributeKind::RustcDefPath(..)
304304
| AttributeKind::RustcDelayedBugFromInsideQuery
305305
| AttributeKind::RustcDenyExplicitImpl(..)
306+
| AttributeKind::RustcDoNotConstCheck
306307
| AttributeKind::RustcDummy
307308
| AttributeKind::RustcDumpDefParents
308309
| AttributeKind::RustcDumpItemBounds
@@ -394,7 +395,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
394395
| sym::rustc_inherit_overflow_checks
395396
| sym::rustc_trivial_field_reads
396397
| sym::rustc_on_unimplemented
397-
| sym::rustc_do_not_const_check
398398
| sym::rustc_reservation_impl
399399
| sym::rustc_doc_primitive
400400
| sym::rustc_conversion_suggestion

0 commit comments

Comments
 (0)