1
Fork 0

Change maybe_body_owned_by to take local def id

Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
This commit is contained in:
Miguel Guarniz 2022-07-15 23:13:04 -04:00
parent 3924dac7bb
commit 25bdc8965e
19 changed files with 50 additions and 47 deletions

View file

@ -353,9 +353,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// We use the statements were the binding was initialized, and inspect the HIR to look
// for the branching codepaths that aren't covered, to point at them.
let hir_id = self.mir_hir_id();
let map = self.infcx.tcx.hir();
let body_id = map.body_owned_by(hir_id);
let body_id = map.body_owned_by(self.mir_def_id());
let body = map.body(body_id);
let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] };

View file

@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
self.tcx
.hir()
.maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
.maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id))
.map(|body_id| self.tcx.typeck_body(body_id))
});

View file

@ -49,10 +49,10 @@ pub fn find_param_with_region<'tcx>(
};
let hir = &tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
let body_id = hir.maybe_body_owned_by(hir_id)?;
let body = hir.body(body_id);
let local_did = id.as_local()?;
let hir_id = hir.local_def_id_to_hir_id(local_did);
// FIXME: use def_kind
// Don't perform this on closures
match hir.get(hir_id) {
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
@ -61,11 +61,14 @@ pub fn find_param_with_region<'tcx>(
_ => {}
}
let body_id = hir.maybe_body_owned_by(local_did)?;
let owner_id = hir.body_owner(body_id);
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
let poly_fn_sig = tcx.fn_sig(id);
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
let body = hir.body(body_id);
body.params
.iter()
.take(if fn_sig.c_variadic {

View file

@ -1614,16 +1614,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
fn encode_info_for_anon_const(&mut self, id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
let body_id = self.tcx.hir().body_owned_by(id);
let local_did = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", local_did);
let body_id = self.tcx.hir().body_owned_by(local_did);
let const_data = self.encode_rendered_const_for_body(body_id);
let qualifs = self.tcx.mir_const_qualif(def_id);
let qualifs = self.tcx.mir_const_qualif(local_did);
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst);
record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
self.encode_item_type(def_id.to_def_id());
record!(self.tables.kind[local_did.to_def_id()] <- EntryKind::AnonConst);
record!(self.tables.mir_const_qualif[local_did.to_def_id()] <- qualifs);
record!(self.tables.rendered_const[local_did.to_def_id()] <- const_data);
self.encode_item_type(local_did.to_def_id());
}
fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {

View file

@ -398,7 +398,7 @@ impl<'hir> Map<'hir> {
pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
for (parent, _) in self.parent_iter(hir_id) {
if let Some(body) = self.maybe_body_owned_by(parent) {
if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) {
return self.body_owner(body);
}
}
@ -419,19 +419,20 @@ impl<'hir> Map<'hir> {
self.local_def_id(self.body_owner(id))
}
/// Given a `HirId`, returns the `BodyId` associated with it,
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
/// if the node is a body owner, otherwise returns `None`.
pub fn maybe_body_owned_by(self, hir_id: HirId) -> Option<BodyId> {
self.find(hir_id).map(associated_body).flatten()
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
self.get_if_local(id.to_def_id()).map(associated_body).flatten()
}
/// Given a body owner's id, returns the `BodyId` associated with it.
pub fn body_owned_by(self, id: HirId) -> BodyId {
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
self.maybe_body_owned_by(id).unwrap_or_else(|| {
let hir_id = self.local_def_id_to_hir_id(id);
span_bug!(
self.span(id),
self.span(hir_id),
"body_owned_by: {} has no associated body",
self.node_to_string(id)
self.node_to_string(hir_id)
);
})
}

View file

@ -157,8 +157,9 @@ pub fn provide(providers: &mut Providers) {
};
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
let local_did = id.expect_local();
let hir_id = hir.local_def_id_to_hir_id(local_did);
if let Some(body_id) = hir.maybe_body_owned_by(local_did) {
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
} else if let Node::TraitItem(&TraitItem {
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),

View file

@ -21,7 +21,7 @@ pub(crate) fn thir_body<'tcx>(
owner_def: ty::WithOptConstParam<LocalDefId>,
) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
let hir = tcx.hir();
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did)));
let body = hir.body(hir.body_owned_by(owner_def.did));
let mut cx = Cx::new(tcx, owner_def);
if let Some(reported) = cx.typeck_results.tainted_by_errors {
return Err(reported);

View file

@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span};
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
let body_id = match def_id.as_local() {
None => return,
Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)),
Some(did) => tcx.hir().body_owned_by(did),
};
let pattern_arena = TypedArena::default();

View file

@ -464,15 +464,15 @@ fn check_unused_unsafe(
def_id: LocalDefId,
used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>,
) -> Vec<(HirId, UnusedUnsafe)> {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let body_id = tcx.hir().maybe_body_owned_by(hir_id);
let body_id = tcx.hir().maybe_body_owned_by(def_id);
let Some(body_id) = body_id else {
debug!("check_unused_unsafe({:?}) - no body found", def_id);
return vec![];
};
let body = tcx.hir().body(body_id);
let body = tcx.hir().body(body_id);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let context = match tcx.hir().fn_sig_by_hir_id(hir_id) {
Some(sig) if sig.header.unsafety == hir::Unsafety::Unsafe => Context::UnsafeFn(hir_id),
_ => Context::Safe,

View file

@ -15,8 +15,8 @@ pub fn provide(providers: &mut Providers) {
return None;
}
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?);
let local_did = def_id.expect_local();
let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_did)?);
let mut local_collector = LocalCollector::default();
local_collector.visit_body(body);

View file

@ -1783,8 +1783,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let generator_body = generator_did
.as_local()
.map(|def_id| hir.local_def_id_to_hir_id(def_id))
.and_then(|hir_id| hir.maybe_body_owned_by(hir_id))
.and_then(|local_did| hir.maybe_body_owned_by(local_did))
.map(|body_id| hir.body(body_id));
let is_async = match generator_did.as_local() {
Some(_) => generator_body
@ -2752,7 +2751,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let body_hir_id = obligation.cause.body_id;
let item_id = self.tcx.hir().get_parent_node(body_hir_id);
if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(item_id) {
if let Some(body_id) =
self.tcx.hir().maybe_body_owned_by(self.tcx.hir().local_def_id(item_id))
{
let body = self.tcx.hir().body(body_id);
if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);

View file

@ -207,9 +207,10 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
constness,
);
let body_id = hir_id.map_or(hir::CRATE_HIR_ID, |id| {
tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id)
});
let body_id = local_did
.and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id))
.or(hir_id)
.map_or(hir::CRATE_HIR_ID, |did| did);
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
}

View file

@ -768,7 +768,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// the first place.
assert_ne!(hir::HirId::make_owner(encl_item_id), encl_body_owner_id);
let encl_body_id = self.tcx.hir().body_owned_by(encl_body_owner_id);
let encl_body_id =
self.tcx.hir().body_owned_by(self.tcx.hir().local_def_id(encl_body_owner_id));
let encl_body = self.tcx.hir().body(encl_body_id);
err.encl_body_span = Some(encl_body.value.span);

View file

@ -107,8 +107,7 @@ impl<'tcx> InheritedBuilder<'tcx> {
impl<'a, 'tcx> Inherited<'a, 'tcx> {
fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
let tcx = infcx.tcx;
let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
let body_id = tcx.hir().maybe_body_owned_by(item_id);
let body_id = tcx.hir().maybe_body_owned_by(def_id);
let typeck_results =
infcx.in_progress_typeck_results.expect("building `FnCtxt` without typeck results");

View file

@ -814,8 +814,7 @@ pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
return tcx.region_scope_tree(typeck_root_def_id);
}
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) {
let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(def_id.expect_local()) {
let mut visitor = RegionResolutionVisitor {
tcx,
scope_tree: ScopeTree::default(),

View file

@ -236,8 +236,7 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
match n.kind() {
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) => {
let mut s = if let Some(def) = def.as_local() {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did);
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(hir_id))
print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(def.did))
} else {
inline::print_inlined_const(cx.tcx, def.did)
};

View file

@ -313,7 +313,7 @@ pub(crate) fn create_config(
}
let hir = tcx.hir();
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(def_id)));
let body = hir.body(hir.body_owned_by(def_id));
debug!("visiting body for {:?}", def_id);
EmitIgnoredResolutionErrors::new(tcx).visit_body(body);
(rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id)

View file

@ -143,8 +143,7 @@ where
// then we need to exit before calling typeck (which will panic). See
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
let hir = tcx.hir();
let owner = hir.local_def_id_to_hir_id(ex.hir_id.owner);
if hir.maybe_body_owned_by(owner).is_none() {
if hir.maybe_body_owned_by(ex.hir_id.owner).is_none() {
return;
}

View file

@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
let hir = cx.tcx.hir();
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
if let Some(body_id) = hir.maybe_body_owned_by(hir.local_def_id(hir_id)) {
check_node(cx, hir_id, |v| {
v.expr(&v.bind("expr", &hir.body(body_id).value));
});