Skip to content

Commit 13f7889

Browse files
committed
Use tcx.hir_node_by_def_id() whenever possible in compiler
1 parent 75dde88 commit 13f7889

File tree

11 files changed

+14
-22
lines changed

11 files changed

+14
-22
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
119119
match self_ty.kind() {
120120
Param(param_ty) => {
121121
debug!(?param_ty);
122-
let caller_hir_id = tcx.local_def_id_to_hir_id(caller);
123-
if let Some(generics) = tcx.hir_node(caller_hir_id).generics() {
122+
if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
124123
let constraint = with_no_trimmed_paths!(format!(
125124
"~const {}",
126125
trait_ref.print_only_trait_path()

compiler/rustc_hir_analysis/src/collect.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,7 @@ fn convert_variant(
835835
fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
836836
use rustc_hir::*;
837837

838-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
839-
let Node::Item(item) = tcx.hir_node(hir_id) else {
838+
let Node::Item(item) = tcx.hir_node_by_def_id(def_id) else {
840839
bug!();
841840
};
842841

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ pub(super) fn explicit_item_bounds(
105105
None => {}
106106
}
107107

108-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
109-
let bounds = match tcx.hir_node(hir_id) {
108+
let bounds = match tcx.hir_node_by_def_id(def_id) {
110109
hir::Node::TraitItem(hir::TraitItem {
111110
kind: hir::TraitItemKind::Type(bounds, _),
112111
span,

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,7 @@ pub(super) fn type_of_opaque(
517517
if let Some(def_id) = def_id.as_local() {
518518
use rustc_hir::*;
519519

520-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
521-
Ok(ty::EarlyBinder::bind(match tcx.hir_node(hir_id) {
520+
Ok(ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id) {
522521
Node::Item(item) => match item.kind {
523522
ItemKind::OpaqueTy(OpaqueTy {
524523
origin: hir::OpaqueTyOrigin::TyAlias { .. },

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2869,11 +2869,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28692869
let id = item
28702870
.def_id
28712871
.as_local()
2872-
.map(|def_id| self.tcx.local_def_id_to_hir_id(def_id));
2872+
.map(|def_id| self.tcx.hir_node_by_def_id(def_id));
28732873
if let Some(hir::Node::TraitItem(hir::TraitItem {
28742874
kind: hir::TraitItemKind::Fn(fn_sig, method),
28752875
..
2876-
})) = id.map(|id| self.tcx.hir_node(id))
2876+
})) = id
28772877
{
28782878
let self_first_arg = match method {
28792879
hir::TraitFn::Required([ident, ..]) => {

compiler/rustc_infer/src/errors/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,7 @@ impl AddToDiagnostic for AddLifetimeParamsSuggestion<'_> {
363363
return false;
364364
};
365365

366-
let hir_id = self.tcx.local_def_id_to_hir_id(anon_reg.def_id);
367-
368-
let node = self.tcx.hir_node(hir_id);
366+
let node = self.tcx.hir_node_by_def_id(anon_reg.def_id);
369367
let is_impl = matches!(&node, hir::Node::ImplItem(_));
370368
let generics = match node {
371369
hir::Node::Item(&hir::Item {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub fn find_anon_type<'tcx>(
2626
br: &ty::BoundRegionKind,
2727
) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnSig<'tcx>)> {
2828
let anon_reg = tcx.is_suitable_region(region)?;
29-
let hir_id = tcx.local_def_id_to_hir_id(anon_reg.def_id);
30-
let fn_sig = tcx.hir_node(hir_id).fn_sig()?;
29+
let fn_sig = tcx.hir_node_by_def_id(anon_reg.def_id).fn_sig()?;
3130

3231
fn_sig
3332
.decl

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ pub fn find_param_with_region<'tcx>(
5050

5151
let hir = &tcx.hir();
5252
let def_id = id.as_local()?;
53-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
5453

5554
// FIXME: use def_kind
5655
// Don't perform this on closures
57-
match tcx.hir_node(hir_id) {
56+
match tcx.hir_node_by_def_id(def_id) {
5857
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
5958
return None;
6059
}

compiler/rustc_middle/src/ty/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2221,8 +2221,7 @@ impl<'tcx> TyCtxt<'tcx> {
22212221
/// Whether the trait impl is marked const. This does not consider stability or feature gates.
22222222
pub fn is_const_trait_impl_raw(self, def_id: DefId) -> bool {
22232223
let Some(local_def_id) = def_id.as_local() else { return false };
2224-
let hir_id = self.local_def_id_to_hir_id(local_def_id);
2225-
let node = self.hir_node(hir_id);
2224+
let node = self.hir_node_by_def_id(local_def_id);
22262225

22272226
matches!(
22282227
node,

compiler/rustc_middle/src/values.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ pub fn recursive_type_error(
154154
let (_, field_id) = item_and_field_ids[i];
155155
let (next_item_id, _) = item_and_field_ids[(i + 1) % cycle_len];
156156
// Find the span(s) that contain the next item in the cycle
157-
let hir_id = tcx.local_def_id_to_hir_id(field_id);
158-
let hir::Node::Field(field) = tcx.hir_node(hir_id) else { bug!("expected field") };
157+
let hir::Node::Field(field) = tcx.hir_node_by_def_id(field_id) else {
158+
bug!("expected field")
159+
};
159160
let mut found = Vec::new();
160161
find_item_ty_spans(tcx, field.ty, next_item_id, &mut found, representable_ids);
161162

compiler/rustc_ty_utils/src/implied_bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
161161
}
162162

163163
fn fn_sig_spans(tcx: TyCtxt<'_>, def_id: LocalDefId) -> impl Iterator<Item = Span> + '_ {
164-
let node = tcx.hir_node(tcx.local_def_id_to_hir_id(def_id));
164+
let node = tcx.hir_node_by_def_id(def_id);
165165
if let Some(decl) = node.fn_decl() {
166166
decl.inputs.iter().map(|ty| ty.span).chain(iter::once(decl.output.span()))
167167
} else {

0 commit comments

Comments
 (0)