Skip to content

Commit 170859a

Browse files
Rollup merge of rust-lang#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 39219ce + 26ee8ee commit 170859a

File tree

12 files changed

+62
-13
lines changed

12 files changed

+62
-13
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,30 @@ 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+
796+
pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
797+
798+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonnullOptimizationGuaranteedParser {
799+
const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
800+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
801+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
802+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
803+
}
804+
781805
pub(crate) struct RustcSymbolName;
782806

783807
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
@@ -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>>,
@@ -281,6 +282,7 @@ attribute_parsers!(
281282
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
282283
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
283284
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
285+
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
284286
Single<WithoutArgs<RustcNounwindParser>>,
285287
Single<WithoutArgs<RustcOffloadKernelParser>>,
286288
Single<WithoutArgs<RustcOutlivesParser>>,

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_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::inline_fluent;
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};
@@ -142,7 +143,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
142143
// Check if the inner type is one of the NPO-guaranteed ones.
143144
// For that we first unpeel transparent *structs* (but not unions).
144145
let is_npo = |def: AdtDef<'tcx>| {
145-
self.tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
146+
find_attr!(
147+
self.tcx.get_all_attrs(def.did()),
148+
AttributeKind::RustcNonnullOptimizationGuaranteed
149+
)
146150
};
147151
let inner = self.unfold_transparent(inner, /* may_unfold */ |def| {
148152
// 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
@@ -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

@@ -1177,6 +1180,9 @@ pub enum AttributeKind {
11771180
/// Represents `#[rustc_non_const_trait_method]`.
11781181
RustcNonConstTraitMethod,
11791182

1183+
/// Represents `#[rustc_nonnull_optimization_guaranteed]`.
1184+
RustcNonnullOptimizationGuaranteed,
1185+
11801186
/// Represents `#[rustc_nounwind]`
11811187
RustcNounwind,
11821188

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 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,
@@ -137,6 +138,7 @@ impl AttributeKind {
137138
RustcNeverReturnsNullPointer => Yes,
138139
RustcNoImplicitAutorefs => Yes,
139140
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
141+
RustcNonnullOptimizationGuaranteed => Yes,
140142
RustcNounwind => No,
141143
RustcObjcClass { .. } => No,
142144
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)