Skip to content

Commit d1f3c9e

Browse files
authored
Rollup merge of #152296 - jdonszelmann:port-rust-nonnull-guaranteed, r=jonathanbrouwer
Port `rust_nonnull_optimization_guaranteed` and `rustc_do_not_const_check` to the new attribute parser r? @JonathanBrouwer another two of them :)
2 parents c9a7f8a + 12e6628 commit d1f3c9e

12 files changed

Lines changed: 62 additions & 13 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,30 @@ 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+
1196+
pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
1197+
1198+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonnullOptimizationGuaranteedParser {
1199+
const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
1200+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1201+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
1202+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
1203+
}
1204+
11811205
pub(crate) struct RustcSymbolName;
11821206

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

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 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>>,
@@ -295,6 +296,7 @@ attribute_parsers!(
295296
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
296297
Single<WithoutArgs<RustcNoMirInlineParser>>,
297298
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
299+
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
298300
Single<WithoutArgs<RustcNounwindParser>>,
299301
Single<WithoutArgs<RustcObjectLifetimeDefaultParser>>,
300302
Single<WithoutArgs<RustcOffloadKernelParser>>,

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::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_const_eval/src/interpret/call.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use either::{Left, Right};
77
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
88
use rustc_data_structures::assert_matches;
99
use rustc_errors::msg;
10+
use rustc_hir::attrs::AttributeKind;
1011
use rustc_hir::def_id::DefId;
12+
use rustc_hir::find_attr;
1113
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
1214
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
1315
use rustc_middle::{bug, mir, span_bug};
14-
use rustc_span::sym;
1516
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
1617
use tracing::field::Empty;
1718
use tracing::{info, instrument, trace};
@@ -145,7 +146,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
145146
// Check if the inner type is one of the NPO-guaranteed ones.
146147
// For that we first unpeel transparent *structs* (but not unions).
147148
let is_npo = |def: AdtDef<'tcx>| {
148-
self.tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
149+
find_attr!(
150+
self.tcx.get_all_attrs(def.did()),
151+
AttributeKind::RustcNonnullOptimizationGuaranteed
152+
)
149153
};
150154
let inner = self.unfold_transparent(inner, /* may_unfold */ |def| {
151155
// Stop at NPO types so that we don't miss that attribute in the check below!

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 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

@@ -1265,6 +1268,9 @@ pub enum AttributeKind {
12651268
/// Represents `#[rustc_non_const_trait_method]`.
12661269
RustcNonConstTraitMethod,
12671270

1271+
/// Represents `#[rustc_nonnull_optimization_guaranteed]`.
1272+
RustcNonnullOptimizationGuaranteed,
1273+
12681274
/// Represents `#[rustc_nounwind]`
12691275
RustcNounwind,
12701276

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 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,
@@ -148,6 +149,7 @@ impl AttributeKind {
148149
RustcNoImplicitBounds => No,
149150
RustcNoMirInline => Yes,
150151
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
152+
RustcNonnullOptimizationGuaranteed => Yes,
151153
RustcNounwind => No,
152154
RustcObjcClass { .. } => No,
153155
RustcObjcSelector { .. } => 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_lint/src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::iter;
22

33
use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange};
4-
use rustc_hir::{Expr, ExprKind, HirId, LangItem};
4+
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::{Expr, ExprKind, HirId, LangItem, find_attr};
56
use rustc_middle::bug;
67
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
78
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
@@ -686,7 +687,7 @@ pub(crate) fn nonnull_optimization_guaranteed<'tcx>(
686687
tcx: TyCtxt<'tcx>,
687688
def: ty::AdtDef<'tcx>,
688689
) -> bool {
689-
tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
690+
find_attr!(tcx.get_all_attrs(def.did()), AttributeKind::RustcNonnullOptimizationGuaranteed)
690691
}
691692

692693
/// `repr(transparent)` structs can have a single non-1-ZST field, this function returns that

0 commit comments

Comments
 (0)