Skip to content

Commit c668820

Browse files
authored
Rollup merge of #99311 - kckeiks:clean-up-body-owner-methods, r=cjgillot
change maybe_body_owned_by to take local def id Issue #96341 r? `@cjgillot`
2 parents c907b6f + 94611b8 commit c668820

File tree

25 files changed

+58
-51
lines changed

25 files changed

+58
-51
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_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
853853
let closure_id = self.mir_hir_id();
854854
let fn_call_id = hir.get_parent_node(closure_id);
855855
let node = hir.get(fn_call_id);
856-
let item_id = hir.enclosing_body_owner(fn_call_id);
856+
let def_id = hir.enclosing_body_owner(fn_call_id);
857857
let mut look_at_return = true;
858858
// If we can detect the expression to be an `fn` call where the closure was an argument,
859859
// we point at the `fn` definition argument...
@@ -864,7 +864,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
864864
.filter(|(_, arg)| arg.hir_id == closure_id)
865865
.map(|(pos, _)| pos)
866866
.next();
867-
let def_id = hir.local_def_id(item_id);
868867
let tables = self.infcx.tcx.typeck(def_id);
869868
if let Some(ty::FnDef(def_id, _)) =
870869
tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind())

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(expr.hir_id.owner)
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 def_id = id.as_local()?;
53+
let hir_id = hir.local_def_id_to_hir_id(def_id);
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(def_id)?;
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16161616
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
16171617
let def_id = self.tcx.hir().local_def_id(id);
16181618
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
1619-
let body_id = self.tcx.hir().body_owned_by(id);
1619+
let body_id = self.tcx.hir().body_owned_by(def_id);
16201620
let const_data = self.encode_rendered_const_for_body(body_id);
16211621
let qualifs = self.tcx.mir_const_qualif(def_id);
16221622

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,10 @@ impl<'hir> Map<'hir> {
396396
}
397397
}
398398

399-
pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
399+
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
400400
for (parent, _) in self.parent_iter(hir_id) {
401-
if let Some(body) = self.maybe_body_owned_by(parent) {
402-
return self.body_owner(body);
401+
if let Some(body) = self.find(parent).map(associated_body).flatten() {
402+
return self.body_owner_def_id(body);
403403
}
404404
}
405405

@@ -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
}
@@ -670,7 +671,7 @@ impl<'hir> Map<'hir> {
670671
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
671672
/// Used exclusively for diagnostics, to avoid suggestion function calls.
672673
pub fn is_inside_const_context(self, hir_id: HirId) -> bool {
673-
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
674+
self.body_const_context(self.enclosing_body_owner(hir_id)).is_some()
674675
}
675676

676677
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a

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 def_id = id.expect_local();
161+
let hir_id = hir.local_def_id_to_hir_id(def_id);
162+
if let Some(body_id) = hir.maybe_body_owned_by(def_id) {
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/check_unsafety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
626626
if tcx.is_closure(def.did.to_def_id()) {
627627
let hir = tcx.hir();
628628
let owner = hir.enclosing_body_owner(hir.local_def_id_to_hir_id(def.did));
629-
tcx.ensure().thir_check_unsafety(hir.local_def_id(owner));
629+
tcx.ensure().thir_check_unsafety(owner);
630630
return;
631631
}
632632

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(def_id) => tcx.hir().body_owned_by(def_id),
3030
};
3131

3232
let pattern_arena = TypedArena::default();

0 commit comments

Comments
 (0)