Skip to content
/ rust Public
forked from rust-lang/rust

Commit c668820

Browse files
authored
Rollup merge of rust-lang#99311 - kckeiks:clean-up-body-owner-methods, r=cjgillot
change maybe_body_owned_by to take local def id Issue rust-lang#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

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

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

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

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

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

+11-10
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

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

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

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

+1-1
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();

compiler/rustc_mir_transform/src/check_unsafety.rs

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

+2-2
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_def_id = def_id.expect_local();
19+
let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_def_id)?);
2020

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

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1790,8 +1790,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17901790

17911791
let generator_body = generator_did
17921792
.as_local()
1793-
.map(|def_id| hir.local_def_id_to_hir_id(def_id))
1794-
.and_then(|hir_id| hir.maybe_body_owned_by(hir_id))
1793+
.and_then(|def_id| hir.maybe_body_owned_by(def_id))
17951794
.map(|body_id| hir.body(body_id));
17961795
let is_async = match generator_did.as_local() {
17971796
Some(_) => generator_body
@@ -2759,7 +2758,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
27592758
let body_hir_id = obligation.cause.body_id;
27602759
let item_id = self.tcx.hir().get_parent_node(body_hir_id);
27612760

2762-
if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(item_id) {
2761+
if let Some(body_id) =
2762+
self.tcx.hir().maybe_body_owned_by(self.tcx.hir().local_def_id(item_id))
2763+
{
27632764
let body = self.tcx.hir().body(body_id);
27642765
if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
27652766
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);

compiler/rustc_ty_utils/src/ty.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,14 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
207207
constness,
208208
);
209209

210-
let body_id = hir_id.map_or(hir::CRATE_HIR_ID, |id| {
211-
tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id)
212-
});
210+
let body_id =
211+
local_did.and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id));
212+
let body_id = match body_id {
213+
Some(id) => id,
214+
None if hir_id.is_some() => hir_id.unwrap(),
215+
_ => hir::CRATE_HIR_ID,
216+
};
217+
213218
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
214219
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
215220
}

compiler/rustc_typeck/src/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
766766

767767
// If this didn't hold, we would not have to report an error in
768768
// the first place.
769-
assert_ne!(hir::HirId::make_owner(encl_item_id), encl_body_owner_id);
769+
assert_ne!(encl_item_id, encl_body_owner_id);
770770

771771
let encl_body_id = self.tcx.hir().body_owned_by(encl_body_owner_id);
772772
let encl_body = self.tcx.hir().body(encl_body_id);

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5858
debug!("FnCtxt::check_asm: {} deferred checks", deferred_asm_checks.len());
5959
for (asm, hir_id) in deferred_asm_checks.drain(..) {
6060
let enclosing_id = self.tcx.hir().enclosing_body_owner(hir_id);
61-
InlineAsmCtxt::new_in_fn(self).check_asm(asm, enclosing_id);
61+
InlineAsmCtxt::new_in_fn(self)
62+
.check_asm(asm, self.tcx.hir().local_def_id_to_hir_id(enclosing_id));
6263
}
6364
}
6465

compiler/rustc_typeck/src/check/inherited.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ impl<'tcx> InheritedBuilder<'tcx> {
107107
impl<'a, 'tcx> Inherited<'a, 'tcx> {
108108
fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
109109
let tcx = infcx.tcx;
110-
let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
111-
let body_id = tcx.hir().maybe_body_owned_by(item_id);
110+
let body_id = tcx.hir().maybe_body_owned_by(def_id);
112111
let typeck_results =
113112
infcx.in_progress_typeck_results.expect("building `FnCtxt` without typeck results");
114113

compiler/rustc_typeck/src/check/region.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,7 @@ pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
814814
return tcx.region_scope_tree(typeck_root_def_id);
815815
}
816816

817-
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
818-
let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) {
817+
let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(def_id.expect_local()) {
819818
let mut visitor = RegionResolutionVisitor {
820819
tcx,
821820
scope_tree: ScopeTree::default(),

compiler/rustc_typeck/src/collect/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
100100
ExprKind::MethodCall(segment, ..) | ExprKind::Path(QPath::TypeRelative(_, segment)),
101101
..
102102
}) => {
103-
let body_owner = tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id));
103+
let body_owner = tcx.hir().enclosing_body_owner(hir_id);
104104
let tables = tcx.typeck(body_owner);
105105
// This may fail in case the method/path does not actually exist.
106106
// As there is no relevant param for `def_id`, we simply return
@@ -134,7 +134,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
134134
| ExprKind::Struct(&QPath::Resolved(_, path), ..),
135135
..
136136
}) => {
137-
let body_owner = tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id));
137+
let body_owner = tcx.hir().enclosing_body_owner(hir_id);
138138
let _tables = tcx.typeck(body_owner);
139139
&*path
140140
}

src/librustdoc/clean/utils.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,7 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
236236
match n.kind() {
237237
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) => {
238238
let mut s = if let Some(def) = def.as_local() {
239-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did);
240-
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(hir_id))
239+
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(def.did))
241240
} else {
242241
inline::print_inlined_const(cx.tcx, def.did)
243242
};

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub(crate) fn create_config(
313313
}
314314

315315
let hir = tcx.hir();
316-
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(def_id)));
316+
let body = hir.body(hir.body_owned_by(def_id));
317317
debug!("visiting body for {:?}", def_id);
318318
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
319319
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)

src/librustdoc/scrape_examples.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ where
143143
// then we need to exit before calling typeck (which will panic). See
144144
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
145145
let hir = tcx.hir();
146-
let owner = hir.local_def_id_to_hir_id(ex.hir_id.owner);
147-
if hir.maybe_body_owned_by(owner).is_none() {
146+
if hir.maybe_body_owned_by(ex.hir_id.owner).is_none() {
148147
return;
149148
}
150149

src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, count_recv: &hi
1212
if_chain! {
1313
if is_trait_method(cx, count_recv, sym::Iterator);
1414
let closure = expr_or_init(cx, map_arg);
15-
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(closure.hir_id);
15+
if let Some(def_id) = cx.tcx.hir().opt_local_def_id(closure.hir_id);
16+
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id);
1617
let closure_body = cx.tcx.hir().body(body_id);
1718
if !cx.typeck_results().expr_ty(&closure_body.value).is_unit();
1819
then {

src/tools/clippy/clippy_lints/src/utils/author.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
138138

139139
fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
140140
let hir = cx.tcx.hir();
141-
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
141+
if let Some(body_id) = hir.maybe_body_owned_by(hir_id.expect_owner()) {
142142
check_node(cx, hir_id, |v| {
143143
v.expr(&v.bind("expr", &hir.body(body_id).value));
144144
});

src/tools/clippy/clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,7 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool
13531353
if is_integer_literal(e, value) {
13541354
return true;
13551355
}
1356-
let enclosing_body = cx.tcx.hir().local_def_id(cx.tcx.hir().enclosing_body_owner(e.hir_id));
1356+
let enclosing_body = cx.tcx.hir().enclosing_body_owner(e.hir_id);
13571357
if let Some((Constant::Int(v), _)) = constant(cx, cx.tcx.typeck(enclosing_body), e) {
13581358
return value == v;
13591359
}

0 commit comments

Comments
 (0)