Skip to content

Commit e4f42f2

Browse files
authored
Unrolled build for rust-lang#122513
Rollup merge of rust-lang#122513 - petrochenkov:somehir4, r=fmease hir: Remove `opt_local_def_id_to_hir_id` and `opt_hir_node_by_def_id` Also replace a few `hir_node()` calls with `hir_node_by_def_id()`. Follow up to rust-lang#120943.
2 parents c5b5713 + 89b536d commit e4f42f2

File tree

37 files changed

+135
-199
lines changed

37 files changed

+135
-199
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
422422
(None, &[][..], 0)
423423
};
424424
if let Some(def_id) = def_id
425-
&& let node =
426-
self.infcx.tcx.hir_node(self.infcx.tcx.local_def_id_to_hir_id(def_id))
425+
&& let node = self.infcx.tcx.hir_node_by_def_id(def_id)
427426
&& let Some(fn_sig) = node.fn_sig()
428427
&& let Some(ident) = node.ident()
429428
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
672672
};
673673
(
674674
true,
675-
td.as_local().and_then(|tld| match self.infcx.tcx.opt_hir_node_by_def_id(tld) {
676-
Some(Node::Item(hir::Item {
677-
kind: hir::ItemKind::Trait(_, _, _, _, items),
678-
..
679-
})) => {
675+
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
676+
Node::Item(hir::Item { kind: hir::ItemKind::Trait(_, _, _, _, items), .. }) => {
680677
let mut f_in_trait_opt = None;
681678
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
682679
let hi = fi.hir_id();
@@ -1475,11 +1472,9 @@ fn get_mut_span_in_struct_field<'tcx>(
14751472
if let ty::Ref(_, ty, _) = ty.kind()
14761473
&& let ty::Adt(def, _) = ty.kind()
14771474
&& let field = def.all_fields().nth(field.index())?
1478-
// Use the HIR types to construct the diagnostic message.
1479-
&& let node = tcx.opt_hir_node_by_def_id(field.did.as_local()?)?
14801475
// Now we're dealing with the actual struct that we're going to suggest a change to,
14811476
// we can expect a field that is an immutable reference to a type.
1482-
&& let hir::Node::Field(field) = node
1477+
&& let hir::Node::Field(field) = tcx.hir_node_by_def_id(field.did.as_local()?)
14831478
&& let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
14841479
{
14851480
return Some(lt.ident.span.between(ty.span));

compiler/rustc_hir_analysis/src/check/entry.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
4242
if !def_id.is_local() {
4343
return None;
4444
}
45-
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
46-
match tcx.hir_node(hir_id) {
45+
match tcx.hir_node_by_def_id(def_id.expect_local()) {
4746
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
4847
generics.params.is_empty().not().then_some(generics.span)
4948
}
@@ -57,8 +56,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
5756
if !def_id.is_local() {
5857
return None;
5958
}
60-
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
61-
match tcx.hir_node(hir_id) {
59+
match tcx.hir_node_by_def_id(def_id.expect_local()) {
6260
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
6361
Some(generics.where_clause_span)
6462
}
@@ -79,8 +77,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
7977
if !def_id.is_local() {
8078
return None;
8179
}
82-
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
83-
match tcx.hir_node(hir_id) {
80+
match tcx.hir_node_by_def_id(def_id.expect_local()) {
8481
Node::Item(hir::Item { kind: hir::ItemKind::Fn(fn_sig, _, _), .. }) => {
8582
Some(fn_sig.decl.output.span())
8683
}

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn get_owner_return_paths(
130130
) -> Option<(LocalDefId, ReturnsVisitor<'_>)> {
131131
let hir_id = tcx.local_def_id_to_hir_id(def_id);
132132
let parent_id = tcx.hir().get_parent_item(hir_id).def_id;
133-
tcx.opt_hir_node_by_def_id(parent_id).and_then(|node| node.body_id()).map(|body_id| {
133+
tcx.hir_node_by_def_id(parent_id).body_id().map(|body_id| {
134134
let body = tcx.hir().body(body_id);
135135
let mut visitor = ReturnsVisitor::default();
136136
visitor.visit_body(body);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1969,13 +1969,10 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
19691969
// Match the existing behavior.
19701970
if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
19711971
let pred = self.normalize(span, None, pred);
1972-
let hir_node = tcx.opt_hir_node_by_def_id(self.body_def_id);
19731972

19741973
// only use the span of the predicate clause (#90869)
1975-
1976-
if let Some(hir::Generics { predicates, .. }) =
1977-
hir_node.and_then(|node| node.generics())
1978-
{
1974+
let hir_node = tcx.hir_node_by_def_id(self.body_def_id);
1975+
if let Some(hir::Generics { predicates, .. }) = hir_node.generics() {
19791976
span = predicates
19801977
.iter()
19811978
// There seems to be no better way to find out which predicate we are in

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,8 @@ pub(super) fn implied_predicates_with_filter(
609609
return tcx.super_predicates_of(trait_def_id);
610610
};
611611

612-
let trait_hir_id = tcx.local_def_id_to_hir_id(trait_def_id);
613-
614-
let Node::Item(item) = tcx.hir_node(trait_hir_id) else {
615-
bug!("trait_node_id {} is not an item", trait_hir_id);
612+
let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
613+
bug!("trait_def_id {trait_def_id:?} is not an item");
616614
};
617615

618616
let (generics, bounds) = match item.kind {

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,10 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
371371
return mir_opaque_ty.ty;
372372
}
373373

374-
let scope = tcx.local_def_id_to_hir_id(owner_def_id);
375-
debug!(?scope);
374+
debug!(?owner_def_id);
376375
let mut locator = RpitConstraintChecker { def_id, tcx, found: mir_opaque_ty };
377376

378-
match tcx.hir_node(scope) {
377+
match tcx.hir_node_by_def_id(owner_def_id) {
379378
Node::Item(it) => intravisit::walk_item(&mut locator, it),
380379
Node::ImplItem(it) => intravisit::walk_impl_item(&mut locator, it),
381380
Node::TraitItem(it) => intravisit::walk_trait_item(&mut locator, it),

compiler/rustc_hir_typeck/src/_match.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
234234
}
235235

236236
// Next, make sure that we have no type expectation.
237-
let Some(ret) = self
238-
.tcx
239-
.opt_hir_node_by_def_id(self.body_id)
240-
.and_then(|owner| owner.fn_decl())
241-
.map(|decl| decl.output.span())
237+
let Some(ret) =
238+
self.tcx.hir_node_by_def_id(self.body_id).fn_decl().map(|decl| decl.output.span())
242239
else {
243240
return;
244241
};

compiler/rustc_hir_typeck/src/expr.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -890,21 +890,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
890890

891891
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
892892

893-
if let Some(hir::Node::Item(hir::Item {
894-
kind: hir::ItemKind::Fn(..),
895-
span: encl_fn_span,
896-
..
897-
}))
898-
| Some(hir::Node::TraitItem(hir::TraitItem {
893+
if let hir::Node::Item(hir::Item {
894+
kind: hir::ItemKind::Fn(..), span: encl_fn_span, ..
895+
})
896+
| hir::Node::TraitItem(hir::TraitItem {
899897
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
900898
span: encl_fn_span,
901899
..
902-
}))
903-
| Some(hir::Node::ImplItem(hir::ImplItem {
900+
})
901+
| hir::Node::ImplItem(hir::ImplItem {
904902
kind: hir::ImplItemKind::Fn(..),
905903
span: encl_fn_span,
906904
..
907-
})) = self.tcx.opt_hir_node_by_def_id(encl_item_id.def_id)
905+
}) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
908906
{
909907
// We are inside a function body, so reporting "return statement
910908
// outside of function body" needs an explanation.

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -2172,16 +2172,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21722172
// Try to find earlier invocations of this closure to find if the type mismatch
21732173
// is because of inference. If we find one, point at them.
21742174
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
2175-
let node = self
2176-
.tcx
2177-
.opt_local_def_id_to_hir_id(
2178-
self.tcx.hir().get_parent_item(call_expr.hir_id).def_id,
2179-
)
2180-
.map(|hir_id| self.tcx.hir_node(hir_id));
2181-
match node {
2182-
Some(hir::Node::Item(item)) => call_finder.visit_item(item),
2183-
Some(hir::Node::TraitItem(item)) => call_finder.visit_trait_item(item),
2184-
Some(hir::Node::ImplItem(item)) => call_finder.visit_impl_item(item),
2175+
let parent_def_id = self.tcx.hir().get_parent_item(call_expr.hir_id).def_id;
2176+
match self.tcx.hir_node_by_def_id(parent_def_id) {
2177+
hir::Node::Item(item) => call_finder.visit_item(item),
2178+
hir::Node::TraitItem(item) => call_finder.visit_trait_item(item),
2179+
hir::Node::ImplItem(item) => call_finder.visit_impl_item(item),
21852180
_ => {}
21862181
}
21872182
let typeck = self.typeck_results.borrow();

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -2126,8 +2126,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21262126
let TypeError::FixedArraySize(sz) = terr else {
21272127
return None;
21282128
};
2129-
let tykind = match self.tcx.opt_hir_node_by_def_id(trace.cause.body_id) {
2130-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
2129+
let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
2130+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. }) => {
21312131
let body = hir.body(*body_id);
21322132
struct LetVisitor {
21332133
span: Span,
@@ -2156,7 +2156,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21562156
}
21572157
LetVisitor { span }.visit_body(body).break_value()
21582158
}
2159-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. })) => {
2159+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }) => {
21602160
Some(&ty.peel_refs().kind)
21612161
}
21622162
_ => None,
@@ -2527,15 +2527,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
25272527
.filter(|p| matches!(p.kind, ty::GenericParamDefKind::Lifetime))
25282528
.map(|p| p.name)
25292529
.collect::<Vec<_>>();
2530-
if let Some(hir_id) = self.tcx.opt_local_def_id_to_hir_id(lifetime_scope) {
2531-
// consider late-bound lifetimes ...
2532-
used_names.extend(self.tcx.late_bound_vars(hir_id).into_iter().filter_map(|p| {
2533-
match p {
2534-
ty::BoundVariableKind::Region(lt) => lt.get_name(),
2535-
_ => None,
2536-
}
2537-
}))
2538-
}
2530+
let hir_id = self.tcx.local_def_id_to_hir_id(lifetime_scope);
2531+
// consider late-bound lifetimes ...
2532+
used_names.extend(self.tcx.late_bound_vars(hir_id).into_iter().filter_map(
2533+
|p| match p {
2534+
ty::BoundVariableKind::Region(lt) => lt.get_name(),
2535+
_ => None,
2536+
},
2537+
));
25392538
(b'a'..=b'z')
25402539
.map(|c| format!("'{}", c as char))
25412540
.find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
459459
tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
460460
if let Node::Item(Item {
461461
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
462-
}) = tcx.opt_hir_node_by_def_id(impl_did)?
462+
}) = tcx.hir_node_by_def_id(impl_did)
463463
&& trait_objects.iter().all(|did| {
464464
// FIXME: we should check `self_ty` against the receiver
465465
// type in the `UnifyReceiver` context, but for now, use

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -804,23 +804,22 @@ fn foo(&self) -> Self::T { String::new() }
804804
) -> bool {
805805
let tcx = self.tcx;
806806

807-
let Some(hir_id) = body_owner_def_id.as_local() else {
808-
return false;
809-
};
810-
let Some(hir_id) = tcx.opt_local_def_id_to_hir_id(hir_id) else {
807+
let Some(def_id) = body_owner_def_id.as_local() else {
811808
return false;
812809
};
810+
813811
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
814812
// `expected` and point at it.
813+
let hir_id = tcx.local_def_id_to_hir_id(def_id);
815814
let parent_id = tcx.hir().get_parent_item(hir_id);
816-
let item = tcx.opt_hir_node_by_def_id(parent_id.def_id);
815+
let item = tcx.hir_node_by_def_id(parent_id.def_id);
817816

818817
debug!("expected_projection parent item {:?}", item);
819818

820819
let param_env = tcx.param_env(body_owner_def_id);
821820

822821
match item {
823-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. })) => {
822+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. }) => {
824823
// FIXME: account for `#![feature(specialization)]`
825824
for item in &items[..] {
826825
match item.kind {
@@ -845,10 +844,10 @@ fn foo(&self) -> Self::T { String::new() }
845844
}
846845
}
847846
}
848-
Some(hir::Node::Item(hir::Item {
847+
hir::Node::Item(hir::Item {
849848
kind: hir::ItemKind::Impl(hir::Impl { items, .. }),
850849
..
851-
})) => {
850+
}) => {
852851
for item in &items[..] {
853852
if let hir::AssocItemKind::Type = item.kind {
854853
let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity();

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1339,8 +1339,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13391339
is_doc_hidden: false,
13401340
};
13411341
let attr_iter = tcx
1342-
.opt_local_def_id_to_hir_id(def_id)
1343-
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
1342+
.hir()
1343+
.attrs(tcx.local_def_id_to_hir_id(def_id))
13441344
.iter()
13451345
.filter(|attr| analyze_attr(attr, &mut state));
13461346

compiler/rustc_middle/src/hir/map/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ impl<'tcx> TyCtxt<'tcx> {
158158
self.hir_owner_nodes(owner_id).node()
159159
}
160160

161-
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
162-
#[inline]
163-
pub fn opt_hir_node_by_def_id(self, id: LocalDefId) -> Option<Node<'tcx>> {
164-
Some(self.hir_node_by_def_id(id))
165-
}
166-
167161
/// Retrieves the `hir::Node` corresponding to `id`.
168162
pub fn hir_node(self, id: HirId) -> Node<'tcx> {
169163
self.hir_owner_nodes(id.owner).nodes[id.local_id].node
@@ -239,8 +233,7 @@ impl<'hir> Map<'hir> {
239233
}
240234

241235
pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
242-
id.as_local()
243-
.and_then(|id| Some(self.tcx.hir_node(self.tcx.opt_local_def_id_to_hir_id(id)?)))
236+
id.as_local().map(|id| self.tcx.hir_node_by_def_id(id))
244237
}
245238

246239
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
@@ -304,7 +297,7 @@ impl<'hir> Map<'hir> {
304297
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
305298
/// if the node is a body owner, otherwise returns `None`.
306299
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
307-
let node = self.tcx.opt_hir_node_by_def_id(id)?;
300+
let node = self.tcx.hir_node_by_def_id(id);
308301
let (_, body_id) = associated_body(node)?;
309302
Some(body_id)
310303
}

compiler/rustc_middle/src/ty/context.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,9 @@ impl<'tcx> TyCtxt<'tcx> {
12651265
break (scope, ty::BrNamed(def_id.into(), self.item_name(def_id.into())));
12661266
};
12671267

1268-
let is_impl_item = match self.opt_hir_node_by_def_id(suitable_region_binding_scope) {
1269-
Some(Node::Item(..) | Node::TraitItem(..)) => false,
1270-
Some(Node::ImplItem(..)) => {
1271-
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
1272-
}
1268+
let is_impl_item = match self.hir_node_by_def_id(suitable_region_binding_scope) {
1269+
Node::Item(..) | Node::TraitItem(..) => false,
1270+
Node::ImplItem(..) => self.is_bound_region_in_impl_item(suitable_region_binding_scope),
12731271
_ => false,
12741272
};
12751273

@@ -2355,10 +2353,6 @@ impl<'tcx> TyCtxt<'tcx> {
23552353
self.intrinsic_raw(def_id)
23562354
}
23572355

2358-
pub fn opt_local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> Option<HirId> {
2359-
Some(self.local_def_id_to_hir_id(local_def_id))
2360-
}
2361-
23622356
pub fn next_trait_solver_globally(self) -> bool {
23632357
self.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.globally)
23642358
}

0 commit comments

Comments
 (0)