Skip to content

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

+1-1
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

+3-3
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

+1-1
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

+18-19
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

+3-3
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

+1-5
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

+2-2
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

+2-2
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

+4-4
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

+4-3
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));

src/librustc_save_analysis/dump_visitor.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
107107
where
108108
F: FnOnce(&mut Self),
109109
{
110-
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
110+
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id).to_def_id();
111111

112112
let tables = if self.tcx.has_typeck_tables(item_def_id) {
113113
self.tcx.typeck_tables_of(item_def_id)
@@ -423,8 +423,10 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
423423
vis: ast::Visibility,
424424
attrs: &'l [Attribute],
425425
) {
426-
let qualname =
427-
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
426+
let qualname = format!(
427+
"::{}",
428+
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
429+
);
428430

429431
if !self.span.filter_generated(ident.span) {
430432
let sig = sig::assoc_const_signature(id, ident.name, typ, expr, &self.save_ctxt);
@@ -470,7 +472,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
470472
let name = item.ident.to_string();
471473
let qualname = format!(
472474
"::{}",
473-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
475+
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
474476
);
475477

476478
let kind = match item.kind {
@@ -670,7 +672,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
670672
}
671673
v.process_generic_params(generics, "", item.id);
672674
for impl_item in impl_items {
673-
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id));
675+
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id).to_def_id());
674676
}
675677
});
676678
}
@@ -685,7 +687,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
685687
let name = item.ident.to_string();
686688
let qualname = format!(
687689
"::{}",
688-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
690+
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
689691
);
690692
let mut val = name.clone();
691693
if !generics.params.is_empty() {
@@ -751,7 +753,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
751753
self.process_generic_params(generics, &qualname, item.id);
752754
for method in methods {
753755
let map = &self.tcx.hir();
754-
self.process_trait_item(method, map.local_def_id_from_node_id(item.id))
756+
self.process_trait_item(method, map.local_def_id_from_node_id(item.id).to_def_id())
755757
}
756758
}
757759

@@ -1030,7 +1032,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
10301032
let name = trait_item.ident.name.to_string();
10311033
let qualname = format!(
10321034
"::{}",
1033-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(trait_item.id))
1035+
self.tcx.def_path_str(
1036+
self.tcx.hir().local_def_id_from_node_id(trait_item.id).to_def_id()
1037+
)
10341038
);
10351039

10361040
if !self.span.filter_generated(trait_item.ident.span) {
@@ -1134,7 +1138,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
11341138
.tcx
11351139
.hir()
11361140
.opt_local_def_id_from_node_id(id)
1137-
.and_then(|id| self.save_ctxt.tcx.parent(id))
1141+
.and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
11381142
.map(id_from_def_id);
11391143

11401144
match use_tree.kind {
@@ -1173,7 +1177,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
11731177

11741178
// Make a comma-separated list of names of imported modules.
11751179
let def_id = self.tcx.hir().local_def_id_from_node_id(id);
1176-
let names = self.tcx.names_imported_by_glob_use(def_id);
1180+
let names = self.tcx.names_imported_by_glob_use(def_id.to_def_id());
11771181
let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
11781182

11791183
// Otherwise it's a span with wrong macro expansion info, which
@@ -1227,8 +1231,10 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
12271231
// only get called for the root module of a crate.
12281232
assert_eq!(id, ast::CRATE_NODE_ID);
12291233

1230-
let qualname =
1231-
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
1234+
let qualname = format!(
1235+
"::{}",
1236+
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
1237+
);
12321238

12331239
let sm = self.tcx.sess.source_map();
12341240
let filename = sm.span_to_filename(span);
@@ -1273,7 +1279,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
12731279
.tcx
12741280
.hir()
12751281
.opt_local_def_id_from_node_id(item.id)
1276-
.and_then(|id| self.save_ctxt.tcx.parent(id))
1282+
.and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
12771283
.map(id_from_def_id);
12781284
self.dumper.import(
12791285
&Access { public: false, reachable: false },
@@ -1311,7 +1317,9 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
13111317
TyAlias(_, ref ty_params, _, ref ty) => {
13121318
let qualname = format!(
13131319
"::{}",
1314-
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
1320+
self.tcx.def_path_str(
1321+
self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id()
1322+
)
13151323
);
13161324
let value = match ty {
13171325
Some(ty) => ty_to_string(&ty),

0 commit comments

Comments
 (0)