Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f363450

Browse files
committed
Auto merge of rust-lang#70986 - marmeladema:issue70853/librustc_middle-local-def-id, r=eddyb
rustc_middle: return `LocalDefId` where possible in hir::map module This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`: * opt_local_def_id_from_node_id * opt_local_def_id * body_owner_def_id * local_def_id_from_node_id * get_parent_id This is another step in the right direction for rust-lang#70853 This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
2 parents 1406186 + b6b0057 commit f363450

File tree

24 files changed

+124
-89
lines changed

24 files changed

+124
-89
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15941594
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
15951595
});
15961596
self.check_and_note_conflicting_crates(diag, terr);
1597-
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);
1597+
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id.to_def_id());
15981598

15991599
// It reads better to have the error origin as the final
16001600
// thing.

src/librustc_interface/passes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
812812
{
813813
sess.time("match_checking", || {
814814
tcx.par_body_owners(|def_id| {
815-
tcx.ensure().check_match(def_id);
815+
tcx.ensure().check_match(def_id.to_def_id());
816816
});
817817
});
818818
},
@@ -834,7 +834,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
834834
});
835835

836836
sess.time("MIR_borrow_checking", || {
837-
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
837+
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id.to_def_id()));
838838
});
839839

840840
sess.time("dumping_chalk_like_clauses", || {
@@ -843,7 +843,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
843843

844844
sess.time("MIR_effect_checking", || {
845845
for def_id in tcx.body_owners() {
846-
mir::transform::check_unsafety::check_unsafety(tcx, def_id)
846+
mir::transform::check_unsafety::check_unsafety(tcx, def_id.to_def_id())
847847
}
848848
});
849849

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ declare_lint_pass!(
11661166
);
11671167

11681168
fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
1169-
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
1169+
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
11701170
// trigger the query once for all constants since that will already report the errors
11711171
// FIXME: Use ensure here
11721172
let _ = cx.tcx.const_eval_poly(def_id);

src/librustc_middle/hir/map/mod.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,15 @@ impl<'hir> Map<'hir> {
150150
}
151151

152152
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
153-
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id.expect_local()))
153+
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
154154
}
155155

156156
pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
157157
self.tcx.definitions.def_path(def_id)
158158
}
159159

160-
// FIXME(eddyb) this function can and should return `LocalDefId`.
161160
#[inline]
162-
pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId {
161+
pub fn local_def_id_from_node_id(&self, node: NodeId) -> LocalDefId {
163162
self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| {
164163
let hir_id = self.node_id_to_hir_id(node);
165164
bug!(
@@ -173,24 +172,26 @@ impl<'hir> Map<'hir> {
173172
// FIXME(eddyb) this function can and should return `LocalDefId`.
174173
#[inline]
175174
pub fn local_def_id(&self, hir_id: HirId) -> DefId {
176-
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
177-
bug!(
178-
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
179-
hir_id,
180-
self.find_entry(hir_id)
181-
)
182-
})
175+
self.opt_local_def_id(hir_id)
176+
.unwrap_or_else(|| {
177+
bug!(
178+
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
179+
hir_id,
180+
self.find_entry(hir_id)
181+
)
182+
})
183+
.to_def_id()
183184
}
184185

185186
#[inline]
186-
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
187+
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
187188
let node_id = self.hir_id_to_node_id(hir_id);
188189
self.opt_local_def_id_from_node_id(node_id)
189190
}
190191

191192
#[inline]
192-
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<DefId> {
193-
Some(self.tcx.definitions.opt_local_def_id(node)?.to_def_id())
193+
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<LocalDefId> {
194+
self.tcx.definitions.opt_local_def_id(node)
194195
}
195196

196197
#[inline]
@@ -366,9 +367,8 @@ impl<'hir> Map<'hir> {
366367
parent
367368
}
368369

369-
// FIXME(eddyb) this function can and should return `LocalDefId`.
370-
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
371-
self.local_def_id(self.body_owner(id))
370+
pub fn body_owner_def_id(&self, id: BodyId) -> LocalDefId {
371+
self.local_def_id(self.body_owner(id)).expect_local()
372372
}
373373

374374
/// Given a `HirId`, returns the `BodyId` associated with it,
@@ -720,9 +720,8 @@ impl<'hir> Map<'hir> {
720720
scope
721721
}
722722

723-
// FIXME(eddyb) this function can and should return `LocalDefId`.
724-
pub fn get_parent_did(&self, id: HirId) -> DefId {
725-
self.local_def_id(self.get_parent_item(id))
723+
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
724+
self.local_def_id(self.get_parent_item(id)).expect_local()
726725
}
727726

728727
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {

src/librustc_middle/ty/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,21 +2657,21 @@ pub enum ImplOverlapKind {
26572657

26582658
impl<'tcx> TyCtxt<'tcx> {
26592659
pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> {
2660-
self.typeck_tables_of(self.hir().body_owner_def_id(body))
2660+
self.typeck_tables_of(self.hir().body_owner_def_id(body).to_def_id())
26612661
}
26622662

26632663
/// Returns an iterator of the `DefId`s for all body-owners in this
26642664
/// crate. If you would prefer to iterate over the bodies
26652665
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
2666-
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx {
2666+
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + Captures<'tcx> + 'tcx {
26672667
self.hir()
26682668
.krate()
26692669
.body_ids
26702670
.iter()
26712671
.map(move |&body_id| self.hir().body_owner_def_id(body_id))
26722672
}
26732673

2674-
pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
2674+
pub fn par_body_owners<F: Fn(LocalDefId) + sync::Sync + sync::Send>(self, f: F) {
26752675
par_iter(&self.hir().krate().body_ids)
26762676
.for_each(|&body_id| f(self.hir().body_owner_def_id(body_id)));
26772677
}

src/librustc_mir/const_eval/fn_queries.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
8484

8585
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
8686
let parent_id = tcx.hir().get_parent_did(hir_id);
87-
if !parent_id.is_top_level_module() {
88-
is_const_impl_raw(tcx, parent_id.expect_local())
89-
} else {
90-
false
91-
}
87+
if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
9288
}
9389

9490
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether

src/librustc_mir/transform/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{shim, util};
22
use rustc_ast::ast;
33
use rustc_hir as hir;
4-
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
4+
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LocalDefId, LOCAL_CRATE};
55
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
66
use rustc_index::vec::IndexVec;
77
use rustc_middle::mir::{BodyAndCache, ConstQualifs, MirPhase, Promoted};
@@ -62,7 +62,7 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &DefIdSet {
6262
let mut set = DefIdSet::default();
6363

6464
// All body-owners have MIR associated with them.
65-
set.extend(tcx.body_owners());
65+
set.extend(tcx.body_owners().map(LocalDefId::to_def_id));
6666

6767
// Additionally, tuple struct/variant constructors have MIR, but
6868
// they don't have a BodyId, so we need to build them separately.

src/librustc_passes/intrinsicck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ impl Visitor<'tcx> for ItemVisitor<'tcx> {
131131
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
132132
let owner_def_id = self.tcx.hir().body_owner_def_id(body_id);
133133
let body = self.tcx.hir().body(body_id);
134-
let param_env = self.tcx.param_env(owner_def_id);
135-
let tables = self.tcx.typeck_tables_of(owner_def_id);
134+
let param_env = self.tcx.param_env(owner_def_id.to_def_id());
135+
let tables = self.tcx.typeck_tables_of(owner_def_id.to_def_id());
136136
ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body);
137137
self.visit_body(body);
138138
}

src/librustc_passes/reachable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::sync::Lrc;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::LOCAL_CRATE;
13-
use rustc_hir::def_id::{CrateNum, DefId};
13+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
1414
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1515
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1616
use rustc_hir::{HirIdSet, Node};
@@ -42,7 +42,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>, attrs: Codegen
4242
fn method_might_be_inlined(
4343
tcx: TyCtxt<'_>,
4444
impl_item: &hir::ImplItem<'_>,
45-
impl_src: DefId,
45+
impl_src: LocalDefId,
4646
) -> bool {
4747
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id());
4848
let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id));
@@ -54,7 +54,7 @@ fn method_might_be_inlined(
5454
return true;
5555
}
5656
}
57-
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) {
57+
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src.to_def_id()) {
5858
match tcx.hir().find(impl_hir_id) {
5959
Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs),
6060
Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"),
@@ -171,7 +171,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
171171
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
172172
true
173173
} else {
174-
let impl_did = self.tcx.hir().get_parent_did(hir_id);
174+
let impl_did = self.tcx.hir().get_parent_did(hir_id).to_def_id();
175175
// Check the impl. If the generics on the self
176176
// type of the impl require inlining, this method
177177
// does too.

src/librustc_privacy/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn def_id_visibility<'tcx>(
248248
}
249249
}
250250
Node::TraitItem(..) | Node::Variant(..) => {
251-
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id));
251+
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id).to_def_id());
252252
}
253253
Node::ImplItem(impl_item) => {
254254
match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) {
@@ -270,7 +270,7 @@ fn def_id_visibility<'tcx>(
270270
let (mut ctor_vis, mut span, mut descr) =
271271
def_id_visibility(tcx, parent_did);
272272

273-
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
273+
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
274274
let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap());
275275
let variant = adt_def.variant_with_ctor_id(ctor_did);
276276

@@ -309,7 +309,8 @@ fn def_id_visibility<'tcx>(
309309
// If the structure is marked as non_exhaustive then lower the
310310
// visibility to within the crate.
311311
if ctor_vis == ty::Visibility::Public {
312-
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
312+
let adt_def =
313+
tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
313314
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
314315
ctor_vis =
315316
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));

0 commit comments

Comments
 (0)