Skip to content

Commit 45856e6

Browse files
committed
Remove more local_def_id_to_hir_id.
1 parent 936a09a commit 45856e6

File tree

24 files changed

+128
-155
lines changed

24 files changed

+128
-155
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+31-35
Original file line numberDiff line numberDiff line change
@@ -608,42 +608,39 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
608608
};
609609
(
610610
true,
611-
td.as_local().and_then(|tld| {
612-
let h = hir_map.local_def_id_to_hir_id(tld);
613-
match hir_map.find(h) {
614-
Some(Node::Item(hir::Item {
615-
kind: hir::ItemKind::Trait(_, _, _, _, items),
616-
..
617-
})) => {
618-
let mut f_in_trait_opt = None;
619-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
620-
let hi = fi.hir_id();
621-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
622-
continue;
623-
}
624-
if hir_map.name(hi) != hir_map.name(my_hir) {
625-
continue;
626-
}
627-
f_in_trait_opt = Some(hi);
628-
break;
611+
td.as_local().and_then(|tld| match hir_map.find_def(tld) {
612+
Some(Node::Item(hir::Item {
613+
kind: hir::ItemKind::Trait(_, _, _, _, items),
614+
..
615+
})) => {
616+
let mut f_in_trait_opt = None;
617+
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
618+
let hi = fi.hir_id();
619+
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
620+
continue;
629621
}
630-
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
631-
Some(Node::TraitItem(hir::TraitItem {
632-
kind:
633-
hir::TraitItemKind::Fn(
634-
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
635-
_,
636-
),
637-
..
638-
})) => {
639-
let hir::Ty { span, .. } = inputs[local.index() - 1];
640-
Some(span)
641-
}
642-
_ => None,
643-
})
622+
if hir_map.name(hi) != hir_map.name(my_hir) {
623+
continue;
624+
}
625+
f_in_trait_opt = Some(hi);
626+
break;
644627
}
645-
_ => None,
628+
f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) {
629+
Some(Node::TraitItem(hir::TraitItem {
630+
kind:
631+
hir::TraitItemKind::Fn(
632+
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
633+
_,
634+
),
635+
..
636+
})) => {
637+
let hir::Ty { span, .. } = inputs[local.index() - 1];
638+
Some(span)
639+
}
640+
_ => None,
641+
})
646642
}
643+
_ => None,
647644
}),
648645
)
649646
}
@@ -1053,8 +1050,7 @@ fn annotate_struct_field(
10531050
if let ty::Adt(def, _) = ty.kind() {
10541051
let field = def.all_fields().nth(field.index())?;
10551052
// Use the HIR types to construct the diagnostic message.
1056-
let hir_id = tcx.hir().local_def_id_to_hir_id(field.did.as_local()?);
1057-
let node = tcx.hir().find(hir_id)?;
1053+
let node = tcx.hir().find_def(field.did.as_local()?)?;
10581054
// Now we're dealing with the actual struct that we're going to suggest a change to,
10591055
// we can expect a field that is an immutable reference to a type.
10601056
if let hir::Node::Field(field) = node {

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
7676
//
7777
// As a result, if this id is an FFI item (foreign item) then we only
7878
// let it through if it's included statically.
79-
match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
79+
match tcx.hir().get_def(def_id) {
8080
Node::ForeignItem(..) => {
8181
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
8282
}

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3131
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
3232
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
3333
let def_id = def_id.expect_local();
34-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
35-
36-
let node = tcx.hir().get(hir_id);
34+
let node = tcx.hir().get_def(def_id);
3735

3836
if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
3937
node
4038
{
4139
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
4240
// foreign items cannot be evaluated at compile-time.
41+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
4342
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
4443
tcx.lookup_const_stability(def_id).is_some()
4544
} else {

compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ impl DirtyCleanVisitor<'tcx> {
201201
/// Return all DepNode labels that should be asserted for this item.
202202
/// index=0 is the "name" used for error messages
203203
fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) {
204-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_id);
205-
let node = self.tcx.hir().get(hir_id);
204+
let node = self.tcx.hir().get_def(item_id);
206205
let (name, labels) = match node {
207206
HirNode::Item(item) => {
208207
match item.kind {

compiler/rustc_lint/src/builtin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
611611
// reported for missing docs.
612612
let real_trait = trait_ref.path.res.def_id();
613613
if let Some(def_id) = real_trait.as_local() {
614-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
615-
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) {
614+
if let Some(Node::Item(item)) = cx.tcx.hir().find_def(def_id) {
616615
if let hir::VisibilityKind::Inherited = item.vis.node {
617616
for impl_item_ref in items {
618617
self.private_traits.insert(impl_item_ref.id.hir_id());

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1554,12 +1554,12 @@ impl EncodeContext<'a, 'tcx> {
15541554
}
15551555
}
15561556

1557-
fn encode_info_for_closure(&mut self, def_id: LocalDefId) {
1557+
fn encode_info_for_closure(&mut self, hir_id: hir::HirId) {
1558+
let def_id = self.tcx.hir().local_def_id(hir_id);
15581559
debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
15591560

15601561
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
15611562
// including on the signature, which is inferred in `typeck.
1562-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
15631563
let ty = self.tcx.typeck(def_id).node_type(hir_id);
15641564

15651565
match ty.kind() {
@@ -1580,9 +1580,9 @@ impl EncodeContext<'a, 'tcx> {
15801580
}
15811581
}
15821582

1583-
fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) {
1583+
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
1584+
let def_id = self.tcx.hir().local_def_id(id);
15841585
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
1585-
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
15861586
let body_id = self.tcx.hir().body_owned_by(id);
15871587
let const_data = self.encode_rendered_const_for_body(body_id);
15881588
let qualifs = self.tcx.mir_const_qualif(def_id);
@@ -1898,8 +1898,7 @@ impl Visitor<'tcx> for EncodeContext<'a, 'tcx> {
18981898
}
18991899
fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
19001900
intravisit::walk_anon_const(self, c);
1901-
let def_id = self.tcx.hir().local_def_id(c.hir_id);
1902-
self.encode_info_for_anon_const(def_id);
1901+
self.encode_info_for_anon_const(c.hir_id);
19031902
}
19041903
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
19051904
intravisit::walk_item(self, item);
@@ -1953,8 +1952,7 @@ impl EncodeContext<'a, 'tcx> {
19531952

19541953
fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
19551954
if let hir::ExprKind::Closure(..) = expr.kind {
1956-
let def_id = self.tcx.hir().local_def_id(expr.hir_id);
1957-
self.encode_info_for_closure(def_id);
1955+
self.encode_info_for_closure(expr.hir_id);
19581956
}
19591957
}
19601958

compiler/rustc_middle/src/ty/consts.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl<'tcx> Const<'tcx> {
4040
) -> &'tcx Self {
4141
debug!("Const::from_anon_const(def={:?})", def);
4242

43-
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
44-
45-
let body_id = match tcx.hir().get(hir_id) {
43+
let body_id = match tcx.hir().get_def(def.did) {
4644
hir::Node::AnonConst(ac) => ac.body,
4745
_ => span_bug!(
4846
tcx.def_span(def.did.to_def_id()),
@@ -203,8 +201,7 @@ impl<'tcx> Const<'tcx> {
203201
}
204202

205203
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
206-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
207-
let default_def_id = match tcx.hir().get(hir_id) {
204+
let default_def_id = match tcx.hir().get_def(def_id.expect_local()) {
208205
hir::Node::GenericParam(hir::GenericParam {
209206
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
210207
..

compiler/rustc_middle/src/ty/context.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,7 @@ impl<'tcx> TyCtxt<'tcx> {
14601460
_ => return None, // not a free region
14611461
};
14621462

1463-
let hir_id = self.hir().local_def_id_to_hir_id(suitable_region_binding_scope);
1464-
let is_impl_item = match self.hir().find(hir_id) {
1463+
let is_impl_item = match self.hir().find_def(suitable_region_binding_scope) {
14651464
Some(Node::Item(..) | Node::TraitItem(..)) => false,
14661465
Some(Node::ImplItem(..)) => {
14671466
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
@@ -1481,8 +1480,7 @@ impl<'tcx> TyCtxt<'tcx> {
14811480
self,
14821481
scope_def_id: LocalDefId,
14831482
) -> Vec<&'tcx hir::Ty<'tcx>> {
1484-
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
1485-
let hir_output = match self.hir().get(hir_id) {
1483+
let hir_output = match self.hir().get_def(scope_def_id) {
14861484
Node::Item(hir::Item {
14871485
kind:
14881486
ItemKind::Fn(
@@ -1526,8 +1524,7 @@ impl<'tcx> TyCtxt<'tcx> {
15261524

15271525
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
15281526
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
1529-
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
1530-
match self.hir().get(hir_id) {
1527+
match self.hir().get_def(scope_def_id) {
15311528
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
15321529
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
15331530
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
@@ -1541,6 +1538,7 @@ impl<'tcx> TyCtxt<'tcx> {
15411538
let sig = ret_ty.fn_sig(self);
15421539
let output = self.erase_late_bound_regions(sig.output());
15431540
if output.is_impl_trait() {
1541+
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
15441542
let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap();
15451543
Some((output, fn_decl.output.span()))
15461544
} else {

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ impl<'tcx> TyCtxt<'tcx> {
20452045
/// Yields the parent function's `DefId` if `def_id` is an `impl Trait` definition.
20462046
pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
20472047
if let Some(def_id) = def_id.as_local() {
2048-
if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
2048+
if let Node::Item(item) = tcx.hir().get_def(def_id) {
20492049
if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind {
20502050
return opaque_ty.impl_trait_fn;
20512051
}

compiler/rustc_mir_build/src/lints.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ use std::ops::ControlFlow;
1111

1212
crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1313
let def_id = body.source.def_id().expect_local();
14-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1514

16-
if let Some(fn_kind) = tcx.hir().get(hir_id).fn_kind() {
15+
if let Some(fn_kind) = tcx.hir().get_def(def_id).fn_kind() {
1716
if let FnKind::Closure = fn_kind {
1817
// closures can't recur, so they don't matter.
1918
return;
@@ -37,6 +36,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
3736

3837
vis.reachable_recursive_calls.sort();
3938

39+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
4040
let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span_with_body(hir_id));
4141
tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| {
4242
let mut db = lint.build("function cannot return without recursing");

compiler/rustc_mir_transform/src/const_prop.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
6969
}
7070

7171
let def_id = body.source.def_id().expect_local();
72-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
73-
74-
let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some();
75-
let is_assoc_const = tcx.def_kind(def_id.to_def_id()) == DefKind::AssocConst;
72+
let is_fn_like = tcx.hir().get_def(def_id).fn_kind().is_some();
73+
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
7674

7775
// Only run const prop on functions, methods, closures and associated constants
7876
if !is_fn_like && !is_assoc_const {

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
6262
return;
6363
}
6464

65-
let hir_id = tcx.hir().local_def_id_to_hir_id(mir_source.def_id().expect_local());
66-
let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some();
65+
let is_fn_like = tcx.hir().get_def(mir_source.def_id().expect_local()).fn_kind().is_some();
6766

6867
// Only instrument functions, methods, and closures (not constants since they are evaluated
6968
// at compile time by Miri).

compiler/rustc_mir_transform/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
431431
tcx.ensure().mir_borrowck(def.did);
432432
}
433433

434-
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
435-
let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some();
434+
let is_fn_like = tcx.hir().get_def(def.did).fn_kind().is_some();
436435
if is_fn_like {
437436
let did = def.did.to_def_id();
438437
let def = ty::WithOptConstParam::unknown(did);

compiler/rustc_passes/src/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::mem;
2323
// may need to be marked as live.
2424
fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2525
matches!(
26-
tcx.hir().find(tcx.hir().local_def_id_to_hir_id(def_id)),
26+
tcx.hir().find_def(def_id),
2727
Some(
2828
Node::Item(..)
2929
| Node::ImplItem(..)
@@ -232,7 +232,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
232232
// tuple struct constructor function
233233
let id = self.struct_constructors.get(&id).copied().unwrap_or(id);
234234

235-
if let Some(node) = self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(id)) {
235+
if let Some(node) = self.tcx.hir().find_def(id) {
236236
self.live_symbols.insert(id);
237237
self.visit_node(node);
238238
}

0 commit comments

Comments
 (0)