Skip to content

Commit 71ef7ba

Browse files
committed
Port rustc_intrinsic to the new attribute parser
1 parent 9f4b56a commit 71ef7ba

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
@@ -497,3 +497,12 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
497497
]);
498498
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
499499
}
500+
501+
pub(crate) struct RustcIntrinsicParser;
502+
503+
impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicParser {
504+
const PATH: &'static [Symbol] = &[sym::rustc_intrinsic];
505+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
506+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
507+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsic;
508+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ attribute_parsers!(
256256
Single<WithoutArgs<RustcDumpVtableParser>>,
257257
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
258258
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
259+
Single<WithoutArgs<RustcIntrinsicParser>>,
259260
Single<WithoutArgs<RustcLintOptTyParser>>,
260261
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
261262
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
@@ -1077,6 +1077,9 @@ pub enum AttributeKind {
10771077
/// Represents `#[rustc_hidden_type_of_opaques]`
10781078
RustcHiddenTypeOfOpaques,
10791079

1080+
/// Represents `#[rustc_intrinsic]`
1081+
RustcIntrinsic,
1082+
10801083
/// Represents `#[rustc_layout]`
10811084
RustcLayout(ThinVec<RustcLayoutType>),
10821085

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl AttributeKind {
112112
RustcDynIncompatibleTrait(..) => No,
113113
RustcHasIncoherentInherentImpls => Yes,
114114
RustcHiddenTypeOfOpaques => No,
115+
RustcIntrinsic => Yes,
115116
RustcLayout(..) => No,
116117
RustcLayoutScalarValidRangeEnd(..) => Yes,
117118
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
@@ -307,6 +307,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
307307
| AttributeKind::RustcDynIncompatibleTrait(..)
308308
| AttributeKind::RustcHasIncoherentInherentImpls
309309
| AttributeKind::RustcHiddenTypeOfOpaques
310+
| AttributeKind::RustcIntrinsic
310311
| AttributeKind::RustcLayout(..)
311312
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
312313
| 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)