Skip to content

Commit 8bc6549

Browse files
committed
Port rustc_do_not_const_check to the new attribute parser
1 parent 286fbe5 commit 8bc6549

9 files changed

Lines changed: 37 additions & 8 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
@@ -262,6 +262,7 @@ attribute_parsers!(
262262
Single<WithoutArgs<RustcAllocatorZeroedParser>>,
263263
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
264264
Single<WithoutArgs<RustcDeallocatorParser>>,
265+
Single<WithoutArgs<RustcDoNotConstCheckParser>>,
265266
Single<WithoutArgs<RustcDumpDefParentsParser>>,
266267
Single<WithoutArgs<RustcDumpItemBoundsParser>>,
267268
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
@@ -1087,6 +1087,9 @@ pub enum AttributeKind {
10871087
/// Represents `#[rustc_deny_explicit_impl]`.
10881088
RustcDenyExplicitImpl(Span),
10891089

1090+
/// Represents `#[rustc_do_not_const_check]`
1091+
RustcDoNotConstCheck,
1092+
10901093
/// Represents `#[rustc_dummy]`.
10911094
RustcDummy,
10921095

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl AttributeKind {
107107
RustcDeallocator => No,
108108
RustcDefPath(..) => No,
109109
RustcDenyExplicitImpl(..) => No,
110+
RustcDoNotConstCheck => Yes,
110111
RustcDummy => No,
111112
RustcDumpDefParents => No,
112113
RustcDumpItemBounds => No,

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use std::iter;
33
use rustc_abi::{CanonAbi, ExternAbi};
44
use rustc_ast::util::parser::ExprPrecedence;
55
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey, inline_fluent};
6+
use rustc_hir::attrs::AttributeKind;
67
use rustc_hir::def::{self, CtorKind, Namespace, Res};
78
use rustc_hir::def_id::DefId;
8-
use rustc_hir::{self as hir, HirId, LangItem};
9+
use rustc_hir::{self as hir, HirId, LangItem, find_attr};
910
use rustc_hir_analysis::autoderef::Autoderef;
1011
use rustc_infer::infer::BoundRegionConversionTime;
1112
use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode};
@@ -910,7 +911,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
910911
}
911912

912913
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
913-
if self.has_rustc_attrs && self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
914+
if self.has_rustc_attrs
915+
&& find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::RustcDoNotConstCheck)
916+
{
914917
return;
915918
}
916919

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
302302
| AttributeKind::RustcDeallocator
303303
| AttributeKind::RustcDefPath(..)
304304
| AttributeKind::RustcDenyExplicitImpl(..)
305+
| AttributeKind::RustcDoNotConstCheck
305306
| AttributeKind::RustcDummy
306307
| AttributeKind::RustcDumpDefParents
307308
| AttributeKind::RustcDumpItemBounds
@@ -391,7 +392,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
391392
| sym::rustc_inherit_overflow_checks
392393
| sym::rustc_trivial_field_reads
393394
| sym::rustc_on_unimplemented
394-
| sym::rustc_do_not_const_check
395395
| sym::rustc_reservation_impl
396396
| sym::rustc_doc_primitive
397397
| sym::rustc_conversion_suggestion

0 commit comments

Comments
 (0)