Skip to content

Commit ec1b63a

Browse files
Rollup merge of rust-lang#152168 - jdonszelmann:port-rustc-intrinsic-const-stable-indirect, r=jonathanbrouwer
Port `rustc_intrinsic_const_stable_indirect` and `rustc_intrinsic` to the new attribute parser r? @JonathanBrouwer
2 parents 7b814be + 087c014 commit ec1b63a

10 files changed

Lines changed: 53 additions & 14 deletions

File tree

compiler/rustc_ast_lowering/src/item.rs

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

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcMirParser {
490490
.collect()
491491
}
492492
}
493+
493494
pub(crate) struct RustcNonConstTraitMethodParser;
494495

495496
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
@@ -721,6 +722,8 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
721722
Some(ident)
722723
}
723724
}
725+
<<<<<<< Conflict 1 of 1
726+
+++++++ Contents of side #1
724727

725728
pub(crate) struct RustcEffectiveVisibilityParser;
726729

@@ -760,3 +763,21 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcEffectiveVisibilityParser {
760763
]);
761764
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEffectiveVisibility;
762765
}
766+
767+
pub(crate) struct RustcIntrinsicParser;
768+
769+
impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicParser {
770+
const PATH: &'static [Symbol] = &[sym::rustc_intrinsic];
771+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
772+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
773+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsic;
774+
}
775+
776+
pub(crate) struct RustcIntrinsicConstStableIndirectParser;
777+
778+
impl<S: Stage> NoArgsAttributeParser<S> for RustcIntrinsicConstStableIndirectParser {
779+
const PATH: &'static [Symbol] = &[sym::rustc_intrinsic_const_stable_indirect];
780+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
781+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
782+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcIntrinsicConstStableIndirect;
783+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ attribute_parsers!(
262262
Single<WithoutArgs<RustcEffectiveVisibilityParser>>,
263263
Single<WithoutArgs<RustcHasIncoherentInherentImplsParser>>,
264264
Single<WithoutArgs<RustcHiddenTypeOfOpaquesParser>>,
265+
Single<WithoutArgs<RustcIntrinsicConstStableIndirectParser>>,
266+
Single<WithoutArgs<RustcIntrinsicParser>>,
265267
Single<WithoutArgs<RustcLintOptTyParser>>,
266268
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
267269
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,12 @@ pub enum AttributeKind {
11171117
/// Represents `#[rustc_if_this_changed]`
11181118
RustcIfThisChanged(Span, Option<Symbol>),
11191119

1120+
/// Represents `#[rustc_intrinsic]`
1121+
RustcIntrinsic,
1122+
1123+
/// Represents `#[rustc_intrinsic_const_stable_indirect]`
1124+
RustcIntrinsicConstStableIndirect,
1125+
11201126
/// Represents `#[rustc_layout]`
11211127
RustcLayout(ThinVec<RustcLayoutType>),
11221128

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ impl AttributeKind {
117117
RustcHasIncoherentInherentImpls => Yes,
118118
RustcHiddenTypeOfOpaques => No,
119119
RustcIfThisChanged(..) => No,
120+
RustcIntrinsic => Yes,
121+
RustcIntrinsicConstStableIndirect => No,
120122
RustcLayout(..) => No,
121123
RustcLayoutScalarValidRangeEnd(..) => Yes,
122124
RustcLayoutScalarValidRangeStart(..) => Yes,

compiler/rustc_middle/src/ty/util.rs

Lines changed: 8 additions & 3 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
@@ -1689,7 +1691,10 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
16891691
Some(ty::IntrinsicDef {
16901692
name: tcx.item_name(def_id),
16911693
must_be_overridden,
1692-
const_stable: tcx.has_attr(def_id, sym::rustc_intrinsic_const_stable_indirect),
1694+
const_stable: find_attr!(
1695+
tcx.get_all_attrs(def_id),
1696+
AttributeKind::RustcIntrinsicConstStableIndirect
1697+
),
16931698
})
16941699
} else {
16951700
None

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_mir_transform/src/cross_crate_inline.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use rustc_hir::attrs::InlineAttr;
1+
use rustc_hir::attrs::{AttributeKind, InlineAttr};
22
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::LocalDefId;
4+
use rustc_hir::find_attr;
45
use rustc_middle::bug;
56
use rustc_middle::mir::visit::Visitor;
67
use rustc_middle::mir::*;
@@ -43,7 +44,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
4344
return true;
4445
}
4546

46-
if tcx.has_attr(def_id, sym::rustc_intrinsic) {
47+
if find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcIntrinsic) {
4748
// Intrinsic fallback bodies are always cross-crate inlineable.
4849
// To ensure that the MIR inliner doesn't cluelessly try to inline fallback
4950
// bodies even when the backend would implement something better, we stop
@@ -157,7 +158,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
157158
// But intrinsics don't have a body that gets assigned to a CGU, so they are
158159
// ignored.
159160
if let Some((fn_def_id, _)) = func.const_fn_def()
160-
&& self.tcx.has_attr(fn_def_id, sym::rustc_intrinsic)
161+
&& find_attr!(tcx.get_all_attrs(fn_def_id), AttributeKind::RustcIntrinsic)
161162
{
162163
return;
163164
}

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
312312
| AttributeKind::RustcHasIncoherentInherentImpls
313313
| AttributeKind::RustcHiddenTypeOfOpaques
314314
| AttributeKind::RustcIfThisChanged(..)
315+
| AttributeKind::RustcIntrinsic
316+
| AttributeKind::RustcIntrinsicConstStableIndirect
315317
| AttributeKind::RustcLayout(..)
316318
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
317319
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
@@ -383,9 +385,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
383385
| sym::rustc_no_mir_inline
384386
| sym::rustc_insignificant_dtor
385387
| sym::rustc_nonnull_optimization_guaranteed
386-
| sym::rustc_intrinsic
387388
| sym::rustc_inherit_overflow_checks
388-
| sym::rustc_intrinsic_const_stable_indirect
389389
| sym::rustc_trivial_field_reads
390390
| sym::rustc_on_unimplemented
391391
| sym::rustc_do_not_const_check

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)