Skip to content

Commit 47ca780

Browse files
committed
Port rustc_do_not_const_check to the new attribute parser
1 parent 1396514 commit 47ca780

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
@@ -1178,6 +1178,21 @@ impl<S: Stage> SingleAttributeParser<S> for RustcDiagnosticItemParser {
11781178
}
11791179
}
11801180

1181+
pub(crate) struct RustcDoNotConstCheckParser;
1182+
1183+
impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
1184+
const PATH: &[Symbol] = &[sym::rustc_do_not_const_check];
1185+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1186+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
1187+
Allow(Target::Fn),
1188+
Allow(Target::Method(MethodKind::Inherent)),
1189+
Allow(Target::Method(MethodKind::TraitImpl)),
1190+
Allow(Target::Method(MethodKind::Trait { body: false })),
1191+
Allow(Target::Method(MethodKind::Trait { body: true })),
1192+
]);
1193+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
1194+
}
1195+
11811196
pub(crate) struct RustcSymbolName;
11821197

11831198
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
@@ -274,6 +274,7 @@ attribute_parsers!(
274274
Single<WithoutArgs<RustcConversionSuggestionParser>>,
275275
Single<WithoutArgs<RustcDeallocatorParser>>,
276276
Single<WithoutArgs<RustcDelayedBugFromInsideQueryParser>>,
277+
Single<WithoutArgs<RustcDoNotConstCheckParser>>,
277278
Single<WithoutArgs<RustcDumpDefParentsParser>>,
278279
Single<WithoutArgs<RustcDumpItemBoundsParser>>,
279280
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::msg;
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
@@ -1163,6 +1163,9 @@ pub enum AttributeKind {
11631163
/// Represents `#[rustc_diagnostic_item]`
11641164
RustcDiagnosticItem(Symbol),
11651165

1166+
/// Represents `#[rustc_do_not_const_check]`
1167+
RustcDoNotConstCheck,
1168+
11661169
/// Represents `#[rustc_dummy]`.
11671170
RustcDummy,
11681171

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl AttributeKind {
115115
RustcDenyExplicitImpl(..) => No,
116116
RustcDeprecatedSafe2024 { .. } => Yes,
117117
RustcDiagnosticItem(..) => Yes,
118+
RustcDoNotConstCheck => Yes,
118119
RustcDummy => No,
119120
RustcDumpDefParents => No,
120121
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
@@ -310,6 +310,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
310310
| AttributeKind::RustcDenyExplicitImpl(..)
311311
| AttributeKind::RustcDeprecatedSafe2024 {..}
312312
| AttributeKind::RustcDiagnosticItem(..)
313+
| AttributeKind::RustcDoNotConstCheck
313314
| AttributeKind::RustcDummy
314315
| AttributeKind::RustcDumpDefParents
315316
| AttributeKind::RustcDumpItemBounds
@@ -401,7 +402,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
401402
| sym::rustc_nonnull_optimization_guaranteed
402403
| sym::rustc_inherit_overflow_checks
403404
| sym::rustc_on_unimplemented
404-
| sym::rustc_do_not_const_check
405405
| sym::rustc_doc_primitive
406406
| sym::rustc_layout
407407
| sym::rustc_autodiff

0 commit comments

Comments
 (0)