Skip to content

Commit bc4c68f

Browse files
committed
Port rustc_intrinsic to the new attribute parser
1 parent 0a13b43 commit bc4c68f

9 files changed

Lines changed: 27 additions & 9 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13911391
// create a fake body so that the entire rest of the compiler doesn't have to deal with
13921392
// this as a special case.
13931393
return self.lower_fn_body(decl, contract, |this| {
1394-
if attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic))
1394+
if find_attr!(attrs, AttributeKind::RustcIntrinsic)
13951395
|| this.tcx.is_sdylib_interface_build()
13961396
{
13971397
let span = this.lower_span(span);

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,12 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
721721
Some(ident)
722722
}
723723
}
724+
725+
pub(crate) struct RustcIntrinsicParser;
726+
727+
impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicParser {
728+
const PATH: &'static [Symbol] = &[sym::rustc_intrinsic];
729+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
730+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
731+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsic;
732+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ attribute_parsers!(
260260
Single<WithoutArgs<RustcDumpVtableParser>>,
261261
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
262262
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
263+
Single<WithoutArgs<RustcIntrinsicParser>>,
263264
Single<WithoutArgs<RustcLintOptTyParser>>,
264265
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
265266
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,9 @@ pub enum AttributeKind {
11031103
/// Represents `#[rustc_if_this_changed]`
11041104
RustcIfThisChanged(Span, Option<Symbol>),
11051105

1106+
/// Represents `#[rustc_intrinsic]`
1107+
RustcIntrinsic,
1108+
11061109
/// Represents `#[rustc_layout]`
11071110
RustcLayout(ThinVec<RustcLayoutType>),
11081111

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl AttributeKind {
115115
RustcHasIncoherentInherentImpls => Yes,
116116
RustcHiddenTypeOfOpaques => No,
117117
RustcIfThisChanged(..) => No,
118+
RustcIntrinsic => Yes,
118119
RustcLayout(..) => No,
119120
RustcLayoutScalarValidRangeEnd(..) => Yes,
120121
RustcLayoutScalarValidRangeStart(..) => Yes,

compiler/rustc_middle/src/ty/util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1010
use rustc_data_structures::stack::ensure_sufficient_stack;
1111
use rustc_errors::ErrorGuaranteed;
1212
use rustc_hashes::Hash128;
13-
use rustc_hir as hir;
1413
use rustc_hir::attrs::AttributeKind;
1514
use rustc_hir::def::{CtorOf, DefKind, Res};
1615
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
1716
use rustc_hir::limit::Limit;
17+
use rustc_hir::{self as hir, find_attr};
1818
use rustc_index::bit_set::GrowableBitSet;
1919
use rustc_macros::{HashStable, TyDecodable, TyEncodable, extension};
2020
use rustc_span::sym;
@@ -1679,7 +1679,9 @@ pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
16791679
/// the compiler to make some assumptions about its shape; if the user doesn't use a feature gate, they may
16801680
/// cause an ICE that we otherwise may want to prevent.
16811681
pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::IntrinsicDef> {
1682-
if tcx.features().intrinsics() && tcx.has_attr(def_id, sym::rustc_intrinsic) {
1682+
if tcx.features().intrinsics()
1683+
&& find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcIntrinsic)
1684+
{
16831685
let must_be_overridden = match tcx.hir_node_by_def_id(def_id) {
16841686
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { has_body, .. }, .. }) => {
16851687
!has_body

compiler/rustc_mir_transform/src/check_inline.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Check that a body annotated with `#[rustc_force_inline]` will not fail to inline based on its
22
//! definition alone (irrespective of any specific caller).
33
4-
use rustc_hir::attrs::InlineAttr;
4+
use rustc_hir::attrs::{AttributeKind, InlineAttr};
55
use rustc_hir::def_id::DefId;
6+
use rustc_hir::find_attr;
67
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
78
use rustc_middle::mir::{Body, TerminatorKind};
89
use rustc_middle::ty;
@@ -62,7 +63,7 @@ pub(super) fn is_inline_valid_on_fn<'tcx>(
6263
// but at this stage we don't know whether codegen knows the intrinsic,
6364
// so just conservatively don't inline it. This also ensures that we do not
6465
// accidentally inline the body of an intrinsic that *must* be overridden.
65-
if tcx.has_attr(def_id, sym::rustc_intrinsic) {
66+
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcIntrinsic) {
6667
return Err("callee is an intrinsic");
6768
}
6869

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
310310
| AttributeKind::RustcHasIncoherentInherentImpls
311311
| AttributeKind::RustcHiddenTypeOfOpaques
312312
| AttributeKind::RustcIfThisChanged(..)
313+
| AttributeKind::RustcIntrinsic
313314
| AttributeKind::RustcLayout(..)
314315
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
315316
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
@@ -381,7 +382,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
381382
| sym::rustc_no_mir_inline
382383
| sym::rustc_insignificant_dtor
383384
| sym::rustc_nonnull_optimization_guaranteed
384-
| sym::rustc_intrinsic
385385
| sym::rustc_inherit_overflow_checks
386386
| sym::rustc_intrinsic_const_stable_indirect
387387
| sym::rustc_trivial_field_reads

src/tools/clippy/clippy_lints/src/loops/empty_loop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use super::EMPTY_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_help;
3-
use clippy_utils::{is_in_panic_handler, is_no_std_crate, sym};
3+
use clippy_utils::{is_in_panic_handler, is_no_std_crate};
44

5-
use rustc_hir::{Block, Expr, ItemKind, Node};
5+
use rustc_hir::attrs::AttributeKind;
6+
use rustc_hir::{Block, Expr, ItemKind, Node, find_attr};
67
use rustc_lint::LateContext;
78

89
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, loop_block: &Block<'_>) {
910
let parent_hir_id = cx.tcx.parent_hir_id(expr.hir_id);
1011
if let Node::Item(parent_node) = cx.tcx.hir_node(parent_hir_id)
1112
&& matches!(parent_node.kind, ItemKind::Fn { .. })
1213
&& let attrs = cx.tcx.hir_attrs(parent_hir_id)
13-
&& attrs.iter().any(|attr| attr.has_name(sym::rustc_intrinsic))
14+
&& find_attr!(attrs, AttributeKind::RustcIntrinsic)
1415
{
1516
// Intrinsic functions are expanded into an empty loop when lowering the AST
1617
// to simplify the job of later passes which might expect any function to have a body.

0 commit comments

Comments
 (0)