Skip to content

Commit 84d441f

Browse files
Rollup merge of #150086 - Bryntet:parse_never_return_null_pointer, r=JonathanBrouwer
Port `#[rustc_never_returns_null_ptr]` to attribute parser Ports `#[rustc_never_returns_null_ptr]` to be parsed using the attribute parser r? `@JonathanBrouwer`
2 parents 093d175 + 5692064 commit 84d441f

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcMainParser {
1313
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcMain;
1414
}
1515

16+
pub(crate) struct RustcNeverReturnsNullPointerParser;
17+
18+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNeverReturnsNullPointerParser {
19+
const PATH: &[Symbol] = &[sym::rustc_never_returns_null_ptr];
20+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
21+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
22+
Allow(Target::Fn),
23+
Allow(Target::Method(MethodKind::Inherent)),
24+
Allow(Target::Method(MethodKind::Trait { body: false })),
25+
Allow(Target::Method(MethodKind::Trait { body: true })),
26+
Allow(Target::Method(MethodKind::TraitImpl)),
27+
]);
28+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNeverReturnsNullPointer;
29+
}
30+
1631
pub(crate) struct RustcLayoutScalarValidRangeStartParser;
1732

1833
impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeStartParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ use crate::attributes::prototype::CustomMirParser;
6161
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6262
use crate::attributes::rustc_internal::{
6363
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
64-
RustcLegacyConstGenericsParser, RustcMainParser, RustcObjectLifetimeDefaultParser,
65-
RustcScalableVectorParser, RustcSimdMonomorphizeLaneLimitParser,
64+
RustcLegacyConstGenericsParser, RustcMainParser, RustcNeverReturnsNullPointerParser,
65+
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
66+
RustcSimdMonomorphizeLaneLimitParser,
6667
};
6768
use crate::attributes::semantics::MayDangleParser;
6869
use crate::attributes::stability::{
@@ -255,6 +256,7 @@ attribute_parsers!(
255256
Single<WithoutArgs<PubTransparentParser>>,
256257
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
257258
Single<WithoutArgs<RustcMainParser>>,
259+
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
258260
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,
259261
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
260262
Single<WithoutArgs<SpecializationTraitParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,9 @@ pub enum AttributeKind {
878878
/// Represents `#[rustc_main]`.
879879
RustcMain,
880880

881+
/// Represents `#[rustc_never_returns_null_ptr]`
882+
RustcNeverReturnsNullPointer,
883+
881884
/// Represents `#[rustc_object_lifetime_default]`.
882885
RustcObjectLifetimeDefault,
883886

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl AttributeKind {
9595
RustcLayoutScalarValidRangeStart(..) => Yes,
9696
RustcLegacyConstGenerics { .. } => Yes,
9797
RustcMain => No,
98+
RustcNeverReturnsNullPointer => Yes,
9899
RustcObjectLifetimeDefault => No,
99100
RustcPassIndirectlyInNonRusticAbis(..) => No,
100101
RustcScalableVector { .. } => Yes,

compiler/rustc_lint/src/ptr_nulls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::LitKind;
2-
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
2+
use rustc_hir::attrs::AttributeKind;
3+
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind, find_attr};
34
use rustc_middle::ty::RawPtr;
45
use rustc_session::{declare_lint, declare_lint_pass};
56
use rustc_span::{Span, sym};
@@ -72,14 +73,14 @@ fn useless_check<'a, 'tcx: 'a>(
7273
e = e.peel_blocks();
7374
if let ExprKind::MethodCall(_, _expr, [], _) = e.kind
7475
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
75-
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
76+
&& find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::RustcNeverReturnsNullPointer)
7677
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
7778
{
7879
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
7980
} else if let ExprKind::Call(path, _args) = e.kind
8081
&& let ExprKind::Path(ref qpath) = path.kind
8182
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
82-
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
83+
&& find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::RustcNeverReturnsNullPointer)
8384
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
8485
{
8586
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
257257
| AttributeKind::NoLink
258258
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
259259
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
260+
| AttributeKind::RustcNeverReturnsNullPointer
260261
| AttributeKind::RustcScalableVector { .. }
261262
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
262263
| AttributeKind::RustcShouldNotBeCalledOnConstItems(..)
@@ -307,9 +308,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
307308
[sym::rustc_no_implicit_autorefs, ..] => {
308309
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
309310
}
310-
[sym::rustc_never_returns_null_ptr, ..] => {
311-
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
312-
}
313311
[sym::rustc_lint_query_instability, ..] => {
314312
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
315313
}

0 commit comments

Comments
 (0)