Skip to content

Commit 513a578

Browse files
committed
Port rustc_nonnull_optimization_guaranteed to the new attribute parser
1 parent 8bc6549 commit 513a578

File tree

8 files changed

+27
-6
lines changed

8 files changed

+27
-6
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
793793
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
794794
}
795795

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+
796805
pub(crate) struct RustcSymbolName;
797806

798807
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
@@ -280,6 +280,7 @@ attribute_parsers!(
280280
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
281281
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
282282
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
283+
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
283284
Single<WithoutArgs<RustcNounwindParser>>,
284285
Single<WithoutArgs<RustcOffloadKernelParser>>,
285286
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ pub enum AttributeKind {
11741174
/// Represents `#[rustc_non_const_trait_method]`.
11751175
RustcNonConstTraitMethod,
11761176

1177+
/// Represents `#[rustc_nonnull_optimization_guaranteed]`.
1178+
RustcNonnullOptimizationGuaranteed,
1179+
11771180
/// Represents `#[rustc_nounwind]`
11781181
RustcNounwind,
11791182

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl AttributeKind {
136136
RustcNeverReturnsNullPointer => Yes,
137137
RustcNoImplicitAutorefs => Yes,
138138
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
139+
RustcNonnullOptimizationGuaranteed => Yes,
139140
RustcNounwind => No,
140141
RustcObjcClass { .. } => No,
141142
RustcObjcSelector { .. } => No,

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

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
329329
| AttributeKind::RustcNeverReturnsNullPointer
330330
| AttributeKind::RustcNoImplicitAutorefs
331331
| AttributeKind::RustcNonConstTraitMethod
332+
| AttributeKind::RustcNonnullOptimizationGuaranteed
332333
| AttributeKind::RustcNounwind
333334
| AttributeKind::RustcObjcClass { .. }
334335
| AttributeKind::RustcObjcSelector { .. }
@@ -388,7 +389,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
388389
| sym::rustc_diagnostic_item
389390
| sym::rustc_no_mir_inline
390391
| sym::rustc_insignificant_dtor
391-
| sym::rustc_nonnull_optimization_guaranteed
392392
| sym::rustc_inherit_overflow_checks
393393
| sym::rustc_trivial_field_reads
394394
| sym::rustc_on_unimplemented

tests/ui/feature-gates/feature-gate-rustc-attrs-1.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
//~| NOTE the `#[rustc_nonnull_optimization_guaranteed]` attribute is an internal implementation detail that will never be stable
66
//~| NOTE the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to document guaranteed niche optimizations in the standard library
77
//~| NOTE the compiler does not even check whether the type indeed is being non-null-optimized; it is your responsibility to ensure that the attribute is only used on types that are optimized
8-
fn main() {}
8+
struct Foo {}
99

1010
#[rustc_variance]
1111
//~^ ERROR use of an internal attribute [E0658]
1212
//~| NOTE the `#[rustc_variance]` attribute is an internal implementation detail that will never be stable
1313
//~| NOTE the `#[rustc_variance]` attribute is used for rustc unit tests
1414
enum E {}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)