Skip to content

Commit 896da64

Browse files
committed
Simplify find_attr! for HirId usage
Add a HasAttrs<'tcx, Tcx> trait to rustc_hir that allows find_attr! to accept DefId, LocalDefId, OwnerId, and HirId directly, instead of requiring callers to manually fetch the attribute slice first. Before: find_attr!(tcx.hir_attrs(hir_id), SomeAttr) After: find_attr!(tcx, hir_id, SomeAttr) The trait is defined in rustc_hir with a generic Tcx parameter to avoid a dependency cycle (rustc_hir cannot depend on rustc_middle). The four concrete impls for TyCtxt are in rustc_middle. Closes #153103
1 parent b41f22d commit 896da64

File tree

13 files changed

+67
-25
lines changed

13 files changed

+67
-25
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness {
3737
}
3838
}
3939
Node::TraitItem(ti @ TraitItem { kind: TraitItemKind::Fn(..), .. }) => {
40-
if find_attr!(tcx.hir_attrs(ti.hir_id()), RustcNonConstTraitMethod) {
40+
if find_attr!(tcx, ti.hir_id(), RustcNonConstTraitMethod) {
4141
Constness::NotConst
4242
} else {
4343
tcx.trait_def(tcx.local_parent(def_id)).constness

compiler/rustc_hir/src/attrs/mod.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ pub mod diagnostic;
1313
mod encode_cross_crate;
1414
mod pretty_printing;
1515

16+
/// A trait for types that can provide a list of attributes given a `TyCtxt`.
17+
///
18+
/// It allows `find_attr!` to accept either a `DefId`, `LocalDefId`, `OwnerId`, or `HirId`.
19+
/// It is defined here with a generic `Tcx` because `rustc_hir` can't depend on `rustc_middle`.
20+
/// The concrete implementations are in `rustc_middle`.
21+
pub trait HasAttrs<'tcx, Tcx> {
22+
fn get_attrs(self, tcx: &Tcx) -> &'tcx [crate::Attribute];
23+
}
24+
1625
/// Finds attributes in sequences of attributes by pattern matching.
1726
///
1827
/// A little like `matches` but for attributes.
@@ -34,10 +43,12 @@ mod pretty_printing;
3443
///
3544
/// As a convenience, this macro can do that for you!
3645
///
37-
/// Instead of providing an attribute list, provide the `tcx` and a `DefId`.
46+
/// Instead of providing an attribute list, provide the `tcx` and an id
47+
/// (a `DefId`, `LocalDefId`, `OwnerId` or `HirId`).
3848
///
3949
/// ```rust,ignore (illustrative)
4050
/// find_attr!(tcx, def_id, <pattern>)
51+
/// find_attr!(tcx, hir_id, <pattern>)
4152
/// ```
4253
///
4354
/// Another common case is finding attributes applied to the root of the current crate.
@@ -55,13 +66,14 @@ macro_rules! find_attr {
5566
$crate::find_attr!($tcx.hir_krate_attrs(), $pattern $(if $guard)? => $e)
5667
};
5768

58-
($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)?) => {
59-
$crate::find_attr!($tcx, $def_id, $pattern $(if $guard)? => ()).is_some()
69+
($tcx: expr, $id: expr, $pattern: pat $(if $guard: expr)?) => {
70+
$crate::find_attr!($tcx, $id, $pattern $(if $guard)? => ()).is_some()
6071
};
61-
($tcx: expr, $def_id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
62-
#[allow(deprecated)] {
63-
$crate::find_attr!($tcx.get_all_attrs($def_id), $pattern $(if $guard)? => $e)
64-
}
72+
($tcx: expr, $id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
73+
$crate::find_attr!(
74+
$crate::attrs::HasAttrs::get_attrs($id, &$tcx),
75+
$pattern $(if $guard)? => $e
76+
)
6577
}};
6678

6779

compiler/rustc_hir_typeck/src/loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
207207
};
208208

209209
// A `#[const_continue]` must break to a block in a `#[loop_match]`.
210-
if find_attr!(self.tcx.hir_attrs(e.hir_id), ConstContinue(_)) {
210+
if find_attr!(self.tcx, e.hir_id, ConstContinue(_)) {
211211
let Some(label) = break_destination.label else {
212212
let span = e.span;
213213
self.tcx.dcx().emit_fatal(ConstContinueBadLabel { span });
@@ -420,7 +420,7 @@ impl<'hir> CheckLoopVisitor<'hir> {
420420
e: &'hir hir::Expr<'hir>,
421421
body: &'hir hir::Block<'hir>,
422422
) -> Option<Destination> {
423-
if !find_attr!(self.tcx.hir_attrs(e.hir_id), LoopMatch(_)) {
423+
if !find_attr!(self.tcx, e.hir_id, LoopMatch(_)) {
424424
return None;
425425
}
426426

compiler/rustc_interface/src/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn proc_macro_decls_static(tcx: TyCtxt<'_>, (): ()) -> Option<LocalDefId> {
77
let mut decls = None;
88

99
for id in tcx.hir_free_items() {
10-
if find_attr!(tcx.hir_attrs(id.hir_id()), RustcProcMacroDecls) {
10+
if find_attr!(tcx, id.hir_id(), RustcProcMacroDecls) {
1111
decls = Some(id.owner_id.def_id);
1212
}
1313
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,36 @@ impl<'tcx> TyCtxt<'tcx> {
21992199
}
22002200
}
22012201

2202+
// `HasAttrs` impls: allow `find_attr!(tcx, id, ...)` to work with both DefId-like types and HirId.
2203+
2204+
impl<'tcx> hir::attrs::HasAttrs<'tcx, TyCtxt<'tcx>> for DefId {
2205+
fn get_attrs(self, tcx: &TyCtxt<'tcx>) -> &'tcx [hir::Attribute] {
2206+
if let Some(did) = self.as_local() {
2207+
tcx.hir_attrs(tcx.local_def_id_to_hir_id(did))
2208+
} else {
2209+
tcx.attrs_for_def(self)
2210+
}
2211+
}
2212+
}
2213+
2214+
impl<'tcx> hir::attrs::HasAttrs<'tcx, TyCtxt<'tcx>> for LocalDefId {
2215+
fn get_attrs(self, tcx: &TyCtxt<'tcx>) -> &'tcx [hir::Attribute] {
2216+
tcx.hir_attrs(tcx.local_def_id_to_hir_id(self))
2217+
}
2218+
}
2219+
2220+
impl<'tcx> hir::attrs::HasAttrs<'tcx, TyCtxt<'tcx>> for hir::OwnerId {
2221+
fn get_attrs(self, tcx: &TyCtxt<'tcx>) -> &'tcx [hir::Attribute] {
2222+
hir::attrs::HasAttrs::get_attrs(self.def_id, tcx)
2223+
}
2224+
}
2225+
2226+
impl<'tcx> hir::attrs::HasAttrs<'tcx, TyCtxt<'tcx>> for hir::HirId {
2227+
fn get_attrs(self, tcx: &TyCtxt<'tcx>) -> &'tcx [hir::Attribute] {
2228+
tcx.hir_attrs(self)
2229+
}
2230+
}
2231+
22022232
pub fn provide(providers: &mut Providers) {
22032233
closure::provide(providers);
22042234
context::provide(providers);

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ fn construct_fn<'tcx>(
491491
};
492492

493493
if let Some((dialect, phase)) =
494-
find_attr!(tcx.hir_attrs(fn_id), CustomMir(dialect, phase, _) => (dialect, phase))
494+
find_attr!(tcx, fn_id, CustomMir(dialect, phase, _) => (dialect, phase))
495495
{
496496
return custom::build_custom_mir(
497497
tcx,

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
873873
hir::ExprKind::Ret(v) => ExprKind::Return { value: v.map(|v| self.mirror_expr(v)) },
874874
hir::ExprKind::Become(call) => ExprKind::Become { value: self.mirror_expr(call) },
875875
hir::ExprKind::Break(dest, ref value) => {
876-
if find_attr!(self.tcx.hir_attrs(expr.hir_id), ConstContinue(_)) {
876+
if find_attr!(self.tcx, expr.hir_id, ConstContinue(_)) {
877877
match dest.target_id {
878878
Ok(target_id) => {
879879
let (Some(value), Some(_)) = (value, dest.label) else {
@@ -938,7 +938,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
938938
match_source,
939939
},
940940
hir::ExprKind::Loop(body, ..) => {
941-
if find_attr!(self.tcx.hir_attrs(expr.hir_id), LoopMatch(_)) {
941+
if find_attr!(self.tcx, expr.hir_id, LoopMatch(_)) {
942942
let dcx = self.tcx.dcx();
943943

944944
// Accept either `state = expr` or `state = expr;`.

compiler/rustc_mir_build/src/thir/cx/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
104104
typing_env: ty::TypingEnv::non_body_analysis(tcx, def),
105105
typeck_results,
106106
body_owner: def.to_def_id(),
107-
apply_adjustments: !find_attr!(tcx.hir_attrs(hir_id), CustomMir(..) => ()).is_some(),
107+
apply_adjustments: !find_attr!(tcx, hir_id, CustomMir(..)),
108108
}
109109
}
110110

src/tools/clippy/clippy_lints/src/functions/must_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
2424

2525
pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
2626
let attrs = cx.tcx.hir_attrs(item.hir_id());
27-
let attr = find_attr!(cx.tcx.hir_attrs(item.hir_id()), MustUse { span, reason } => (span, reason));
27+
let attr = find_attr!(cx.tcx, item.hir_id(), MustUse { span, reason } => (span, reason));
2828
if let hir::ItemKind::Fn {
2929
ref sig,
3030
body: ref body_id,
@@ -65,7 +65,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
6565
let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id);
6666
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
6767
let attrs = cx.tcx.hir_attrs(item.hir_id());
68-
let attr = find_attr!(cx.tcx.hir_attrs(item.hir_id()), MustUse { span, reason } => (span, reason));
68+
let attr = find_attr!(cx.tcx, item.hir_id(), MustUse { span, reason } => (span, reason));
6969
if let Some((attr_span, reason)) = attr {
7070
check_needless_must_use(
7171
cx,
@@ -98,7 +98,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
9898
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
9999

100100
let attrs = cx.tcx.hir_attrs(item.hir_id());
101-
let attr = find_attr!(cx.tcx.hir_attrs(item.hir_id()), MustUse { span, reason } => (span, reason));
101+
let attr = find_attr!(cx.tcx, item.hir_id(), MustUse { span, reason } => (span, reason));
102102
if let Some((attr_span, reason)) = attr {
103103
check_needless_must_use(
104104
cx,

src/tools/clippy/clippy_lints/src/incompatible_msrv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,5 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv {
269269
fn is_under_cfg_attribute(cx: &LateContext<'_>, hir_id: HirId) -> bool {
270270
cx.tcx
271271
.hir_parent_id_iter(hir_id)
272-
.any(|id| find_attr!(cx.tcx.hir_attrs(id), CfgTrace(..) | CfgAttrTrace))
272+
.any(|id| find_attr!(cx.tcx, id, CfgTrace(..) | CfgAttrTrace))
273273
}

0 commit comments

Comments
 (0)