Skip to content

Commit 86200bf

Browse files
committed
Port rustc_nonnull_optimization_guaranteed to the new attribute parser
1 parent 3c9856f commit 86200bf

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
@@ -1118,6 +1118,15 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcDoNotConstCheckParser {
11181118
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcDoNotConstCheck;
11191119
}
11201120

1121+
pub(crate) struct RustcNonnullOptimizationGuaranteedParser;
1122+
1123+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonnullOptimizationGuaranteedParser {
1124+
const PATH: &[Symbol] = &[sym::rustc_nonnull_optimization_guaranteed];
1125+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1126+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
1127+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonnullOptimizationGuaranteed;
1128+
}
1129+
11211130
pub(crate) struct RustcSymbolName;
11221131

11231132
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
@@ -291,6 +291,7 @@ attribute_parsers!(
291291
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
292292
Single<WithoutArgs<RustcNoMirInlineParser>>,
293293
Single<WithoutArgs<RustcNonConstTraitMethodParser>>,
294+
Single<WithoutArgs<RustcNonnullOptimizationGuaranteedParser>>,
294295
Single<WithoutArgs<RustcNounwindParser>>,
295296
Single<WithoutArgs<RustcObjectLifetimeDefaultParser>>,
296297
Single<WithoutArgs<RustcOffloadKernelParser>>,

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};
@@ -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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,9 @@ pub enum AttributeKind {
12581258
/// Represents `#[rustc_non_const_trait_method]`.
12591259
RustcNonConstTraitMethod,
12601260

1261+
/// Represents `#[rustc_nonnull_optimization_guaranteed]`.
1262+
RustcNonnullOptimizationGuaranteed,
1263+
12611264
/// Represents `#[rustc_nounwind]`
12621265
RustcNounwind,
12631266

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl AttributeKind {
146146
RustcNoImplicitBounds => No,
147147
RustcNoMirInline => Yes,
148148
RustcNonConstTraitMethod => No, // should be reported via other queries like `constness`
149+
RustcNonnullOptimizationGuaranteed => Yes,
149150
RustcNounwind => No,
150151
RustcObjcClass { .. } => No,
151152
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
@@ -339,6 +339,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
339339
| AttributeKind::RustcNoImplicitBounds
340340
| AttributeKind::RustcNoMirInline
341341
| AttributeKind::RustcNonConstTraitMethod
342+
| AttributeKind::RustcNonnullOptimizationGuaranteed
342343
| AttributeKind::RustcNounwind
343344
| AttributeKind::RustcObjcClass { .. }
344345
| AttributeKind::RustcObjcSelector { .. }
@@ -400,7 +401,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
400401
| sym::lang
401402
| sym::default_lib_allocator
402403
| sym::rustc_diagnostic_item
403-
| sym::rustc_nonnull_optimization_guaranteed
404404
| sym::rustc_inherit_overflow_checks
405405
| sym::rustc_on_unimplemented
406406
| sym::rustc_doc_primitive

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)