Port rustc_do_not_const_check to the new attribute parser

This commit is contained in:
Jana Dönszelmann 2026-02-07 15:21:35 +01:00
parent 139651428d
commit 47ca78040a
No known key found for this signature in database
9 changed files with 35 additions and 7 deletions

View file

@ -1178,6 +1178,21 @@ impl<S: Stage> SingleAttributeParser<S> for RustcDiagnosticItemParser {
}
}
pub(crate) struct RustcDoNotConstCheckParser;
impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
const PATH: &[Symbol] = &[sym::rustc_do_not_const_check];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Allow(Target::Fn),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
}
pub(crate) struct RustcSymbolName;
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {

View file

@ -274,6 +274,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcConversionSuggestionParser>>,
Single<WithoutArgs<RustcDeallocatorParser>>,
Single<WithoutArgs<RustcDelayedBugFromInsideQueryParser>>,
Single<WithoutArgs<RustcDoNotConstCheckParser>>,
Single<WithoutArgs<RustcDumpDefParentsParser>>,
Single<WithoutArgs<RustcDumpItemBoundsParser>>,
Single<WithoutArgs<RustcDumpPredicatesParser>>,

View file

@ -7,9 +7,10 @@ use std::ops::Deref;
use rustc_data_structures::assert_matches;
use rustc_errors::{Diag, ErrorGuaranteed};
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, LangItem};
use rustc_hir::{self as hir, LangItem, find_attr};
use rustc_index::bit_set::DenseBitSet;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::visit::Visitor;
@ -215,7 +216,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
return;
}
if !tcx.has_attr(def_id, sym::rustc_do_not_const_check) {
if !find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcDoNotConstCheck) {
self.visit_body(body);
}

View file

@ -1,3 +1,5 @@
use rustc_hir::attrs::AttributeKind;
use rustc_hir::find_attr;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{self, BasicBlock, Location};
use rustc_middle::ty::TyCtxt;
@ -34,7 +36,7 @@ pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
return;
}
if tcx.has_attr(body.source.def_id(), sym::rustc_do_not_const_check) {
if find_attr!(tcx.get_all_attrs(body.source.def_id()), AttributeKind::RustcDoNotConstCheck) {
return;
}

View file

@ -6,8 +6,9 @@ use rustc_abi::{Align, Size};
use rustc_ast::Mutability;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry};
use rustc_errors::msg;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem};
use rustc_hir::{self as hir, CRATE_HIR_ID, LangItem, find_attr};
use rustc_middle::mir::AssertMessage;
use rustc_middle::mir::interpret::{Pointer, ReportedErrorInfo};
use rustc_middle::query::TyCtxtAt;
@ -440,7 +441,9 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
// sensitive check here. But we can at least rule out functions that are not const at
// all. That said, we have to allow calling functions inside a `const trait`. These
// *are* const-checked!
if !ecx.tcx.is_const_fn(def) || ecx.tcx.has_attr(def, sym::rustc_do_not_const_check) {
if !ecx.tcx.is_const_fn(def)
|| find_attr!(ecx.tcx.get_all_attrs(def), AttributeKind::RustcDoNotConstCheck)
{
// We certainly do *not* want to actually call the fn
// though, so be sure we return here.
throw_unsup_format!("calling non-const function `{}`", instance)

View file

@ -1163,6 +1163,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_diagnostic_item]`
RustcDiagnosticItem(Symbol),
/// Represents `#[rustc_do_not_const_check]`
RustcDoNotConstCheck,
/// Represents `#[rustc_dummy]`.
RustcDummy,

View file

@ -115,6 +115,7 @@ impl AttributeKind {
RustcDenyExplicitImpl(..) => No,
RustcDeprecatedSafe2024 { .. } => Yes,
RustcDiagnosticItem(..) => Yes,
RustcDoNotConstCheck => Yes,
RustcDummy => No,
RustcDumpDefParents => No,
RustcDumpItemBounds => No,

View file

@ -914,7 +914,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
if self.has_rustc_attrs && self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
if self.has_rustc_attrs
&& find_attr!(self.tcx.get_all_attrs(self.body_id), AttributeKind::RustcDoNotConstCheck)
{
return;
}

View file

@ -310,6 +310,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcDenyExplicitImpl(..)
| AttributeKind::RustcDeprecatedSafe2024 {..}
| AttributeKind::RustcDiagnosticItem(..)
| AttributeKind::RustcDoNotConstCheck
| AttributeKind::RustcDummy
| AttributeKind::RustcDumpDefParents
| AttributeKind::RustcDumpItemBounds
@ -401,7 +402,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| sym::rustc_nonnull_optimization_guaranteed
| sym::rustc_inherit_overflow_checks
| sym::rustc_on_unimplemented
| sym::rustc_do_not_const_check
| sym::rustc_doc_primitive
| sym::rustc_layout
| sym::rustc_autodiff