Port rustc_nonnull_optimization_guaranteed to the new attribute parser

This commit is contained in:
Jana Dönszelmann 2026-02-07 16:01:11 +01:00
parent 47ca78040a
commit 12e6628977
No known key found for this signature in database
8 changed files with 27 additions and 6 deletions

View file

@ -1193,6 +1193,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
}
pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonnullOptimizationGuaranteedParser {
const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
}
pub(crate) struct RustcSymbolName;
impl<S: Stage> SingleAttributeParser<S> for RustcSymbolName {

View file

@ -296,6 +296,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
Single<WithoutArgs<RustcNoMirInlineParser>>,
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
Single<WithoutArgs<RustcNounwindParser>>,
Single<WithoutArgs<RustcObjectLifetimeDefaultParser>>,
Single<WithoutArgs<RustcOffloadKernelParser>>,

View file

@ -7,11 +7,12 @@ use either::{Left, Right};
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
use rustc_data_structures::assert_matches;
use rustc_errors::msg;
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def_id::DefId;
use rustc_hir::find_attr;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
use rustc_middle::{bug, mir, span_bug};
use rustc_span::sym;
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
use tracing::field::Empty;
use tracing::{info, instrument, trace};
@ -145,7 +146,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
// Check if the inner type is one of the NPO-guaranteed ones.
// For that we first unpeel transparent *structs* (but not unions).
let is_npo = |def: AdtDef<'tcx>| {
self.tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
find_attr!(
self.tcx.get_all_attrs(def.did()),
AttributeKind::RustcNonnullOptimizationGuaranteed
)
};
let inner = self.unfold_transparent(inner, /* may_unfold */ |def| {
// Stop at NPO types so that we don't miss that attribute in the check below!

View file

@ -1268,6 +1268,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_non_const_trait_method]`.
RustcNonConstTraitMethod,
/// Represents `#[rustc_nonnull_optimization_guaranteed]`.
RustcNonnullOptimizationGuaranteed,
/// Represents `#[rustc_nounwind]`
RustcNounwind,

View file

@ -149,6 +149,7 @@ impl AttributeKind {
RustcNoImplicitBounds => No,
RustcNoMirInline => Yes,
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
RustcNonnullOptimizationGuaranteed => Yes,
RustcNounwind => No,
RustcObjcClass { .. } => No,
RustcObjcSelector { .. } => No,

View file

@ -1,7 +1,8 @@
use std::iter;
use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange};
use rustc_hir::{Expr, ExprKind, HirId, LangItem};
use rustc_hir::attrs::AttributeKind;
use rustc_hir::{Expr, ExprKind, HirId, LangItem, find_attr};
use rustc_middle::bug;
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
@ -686,7 +687,7 @@ pub(crate) fn nonnull_optimization_guaranteed<'tcx>(
tcx: TyCtxt<'tcx>,
def: ty::AdtDef<'tcx>,
) -> bool {
tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
find_attr!(tcx.get_all_attrs(def.did()), AttributeKind::RustcNonnullOptimizationGuaranteed)
}
/// `repr(transparent)` structs can have a single non-1-ZST field, this function returns that

View file

@ -342,6 +342,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcNoImplicitBounds
| AttributeKind::RustcNoMirInline
| AttributeKind::RustcNonConstTraitMethod
| AttributeKind::RustcNonnullOptimizationGuaranteed
| AttributeKind::RustcNounwind
| AttributeKind::RustcObjcClass { .. }
| AttributeKind::RustcObjcSelector { .. }
@ -399,7 +400,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| sym::forbid
// internal
| sym::default_lib_allocator
| sym::rustc_nonnull_optimization_guaranteed
| sym::rustc_inherit_overflow_checks
| sym::rustc_on_unimplemented
| sym::rustc_doc_primitive

View file

@ -5,10 +5,12 @@
//~| NOTE the `#[rustc_nonnull_optimization_guaranteed]` attribute is an internal implementation detail that will never be stable
//~| NOTE the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to document guaranteed niche optimizations in the standard library
//~| 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
fn main() {}
struct Foo {}
#[rustc_variance]
//~^ ERROR use of an internal attribute [E0658]
//~| NOTE the `#[rustc_variance]` attribute is an internal implementation detail that will never be stable
//~| NOTE the `#[rustc_variance]` attribute is used for rustc unit tests
enum E {}
fn main() {}