Skip to content

Commit 25bdc89

Browse files
committed
Change maybe_body_owned_by to take local def id
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent 3924dac commit 25bdc89

File tree

19 files changed

+50
-47
lines changed

19 files changed

+50
-47
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
353353

354354
// We use the statements were the binding was initialized, and inspect the HIR to look
355355
// for the branching codepaths that aren't covered, to point at them.
356-
let hir_id = self.mir_hir_id();
357356
let map = self.infcx.tcx.hir();
358-
let body_id = map.body_owned_by(hir_id);
357+
let body_id = map.body_owned_by(self.mir_def_id());
359358
let body = map.body(body_id);
360359

361360
let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] };

compiler/rustc_driver/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
328328
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
329329
self.tcx
330330
.hir()
331-
.maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
331+
.maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id))
332332
.map(|body_id| self.tcx.typeck_body(body_id))
333333
});
334334

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ pub fn find_param_with_region<'tcx>(
4949
};
5050

5151
let hir = &tcx.hir();
52-
let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
53-
let body_id = hir.maybe_body_owned_by(hir_id)?;
54-
let body = hir.body(body_id);
52+
let local_did = id.as_local()?;
53+
let hir_id = hir.local_def_id_to_hir_id(local_did);
5554

55+
// FIXME: use def_kind
5656
// Don't perform this on closures
5757
match hir.get(hir_id) {
5858
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
@@ -61,11 +61,14 @@ pub fn find_param_with_region<'tcx>(
6161
_ => {}
6262
}
6363

64+
let body_id = hir.maybe_body_owned_by(local_did)?;
65+
6466
let owner_id = hir.body_owner(body_id);
6567
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
6668
let poly_fn_sig = tcx.fn_sig(id);
6769

6870
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
71+
let body = hir.body(body_id);
6972
body.params
7073
.iter()
7174
.take(if fn_sig.c_variadic {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,16 +1614,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16141614
}
16151615

16161616
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
1617-
let def_id = self.tcx.hir().local_def_id(id);
1618-
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
1619-
let body_id = self.tcx.hir().body_owned_by(id);
1617+
let local_did = self.tcx.hir().local_def_id(id);
1618+
debug!("EncodeContext::encode_info_for_anon_const({:?})", local_did);
1619+
let body_id = self.tcx.hir().body_owned_by(local_did);
16201620
let const_data = self.encode_rendered_const_for_body(body_id);
1621-
let qualifs = self.tcx.mir_const_qualif(def_id);
1621+
let qualifs = self.tcx.mir_const_qualif(local_did);
16221622

1623-
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst);
1624-
record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
1625-
record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
1626-
self.encode_item_type(def_id.to_def_id());
1623+
record!(self.tables.kind[local_did.to_def_id()] <- EntryKind::AnonConst);
1624+
record!(self.tables.mir_const_qualif[local_did.to_def_id()] <- qualifs);
1625+
record!(self.tables.rendered_const[local_did.to_def_id()] <- const_data);
1626+
self.encode_item_type(local_did.to_def_id());
16271627
}
16281628

16291629
fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'hir> Map<'hir> {
398398

399399
pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
400400
for (parent, _) in self.parent_iter(hir_id) {
401-
if let Some(body) = self.maybe_body_owned_by(parent) {
401+
if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) {
402402
return self.body_owner(body);
403403
}
404404
}
@@ -419,19 +419,20 @@ impl<'hir> Map<'hir> {
419419
self.local_def_id(self.body_owner(id))
420420
}
421421

422-
/// Given a `HirId`, returns the `BodyId` associated with it,
422+
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
423423
/// if the node is a body owner, otherwise returns `None`.
424-
pub fn maybe_body_owned_by(self, hir_id: HirId) -> Option<BodyId> {
425-
self.find(hir_id).map(associated_body).flatten()
424+
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
425+
self.get_if_local(id.to_def_id()).map(associated_body).flatten()
426426
}
427427

428428
/// Given a body owner's id, returns the `BodyId` associated with it.
429-
pub fn body_owned_by(self, id: HirId) -> BodyId {
429+
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
430430
self.maybe_body_owned_by(id).unwrap_or_else(|| {
431+
let hir_id = self.local_def_id_to_hir_id(id);
431432
span_bug!(
432-
self.span(id),
433+
self.span(hir_id),
433434
"body_owned_by: {} has no associated body",
434-
self.node_to_string(id)
435+
self.node_to_string(hir_id)
435436
);
436437
})
437438
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ pub fn provide(providers: &mut Providers) {
157157
};
158158
providers.fn_arg_names = |tcx, id| {
159159
let hir = tcx.hir();
160-
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
161-
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
160+
let local_did = id.expect_local();
161+
let hir_id = hir.local_def_id_to_hir_id(local_did);
162+
if let Some(body_id) = hir.maybe_body_owned_by(local_did) {
162163
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
163164
} else if let Node::TraitItem(&TraitItem {
164165
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),

compiler/rustc_mir_build/src/thir/cx/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) fn thir_body<'tcx>(
2121
owner_def: ty::WithOptConstParam<LocalDefId>,
2222
) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
2323
let hir = tcx.hir();
24-
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did)));
24+
let body = hir.body(hir.body_owned_by(owner_def.did));
2525
let mut cx = Cx::new(tcx, owner_def);
2626
if let Some(reported) = cx.typeck_results.tainted_by_errors {
2727
return Err(reported);

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span};
2626
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
2727
let body_id = match def_id.as_local() {
2828
None => return,
29-
Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)),
29+
Some(did) => tcx.hir().body_owned_by(did),
3030
};
3131

3232
let pattern_arena = TypedArena::default();

compiler/rustc_mir_transform/src/check_unsafety.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,15 @@ fn check_unused_unsafe(
464464
def_id: LocalDefId,
465465
used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>,
466466
) -> Vec<(HirId, UnusedUnsafe)> {
467-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
468-
let body_id = tcx.hir().maybe_body_owned_by(hir_id);
467+
let body_id = tcx.hir().maybe_body_owned_by(def_id);
469468

470469
let Some(body_id) = body_id else {
471470
debug!("check_unused_unsafe({:?}) - no body found", def_id);
472471
return vec![];
473472
};
474-
let body = tcx.hir().body(body_id);
475473

474+
let body = tcx.hir().body(body_id);
475+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
476476
let context = match tcx.hir().fn_sig_by_hir_id(hir_id) {
477477
Some(sig) if sig.header.unsafety == hir::Unsafety::Unsafe => Context::UnsafeFn(hir_id),
478478
_ => Context::Safe,

compiler/rustc_passes/src/upvars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub fn provide(providers: &mut Providers) {
1515
return None;
1616
}
1717

18-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
19-
let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?);
18+
let local_did = def_id.expect_local();
19+
let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_did)?);
2020

2121
let mut local_collector = LocalCollector::default();
2222
local_collector.visit_body(body);

0 commit comments

Comments
 (0)