Skip to content

Commit

Permalink
Auto merge of rust-lang#70986 - marmeladema:issue70853/librustc_middl…
Browse files Browse the repository at this point in the history
…e-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.
  • Loading branch information
bors committed Apr 10, 2020
2 parents 1406186 + b6b0057 commit f363450
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/error_reporting/mod.rs
Expand Up @@ -1594,7 +1594,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
});
self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id.to_def_id());

// It reads better to have the error origin as the final
// thing.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_interface/passes.rs
Expand Up @@ -812,7 +812,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
{
sess.time("match_checking", || {
tcx.par_body_owners(|def_id| {
tcx.ensure().check_match(def_id);
tcx.ensure().check_match(def_id.to_def_id());
});
});
},
Expand All @@ -834,7 +834,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
});

sess.time("MIR_borrow_checking", || {
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id));
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id.to_def_id()));
});

sess.time("dumping_chalk_like_clauses", || {
Expand All @@ -843,7 +843,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {

sess.time("MIR_effect_checking", || {
for def_id in tcx.body_owners() {
mir::transform::check_unsafety::check_unsafety(tcx, def_id)
mir::transform::check_unsafety::check_unsafety(tcx, def_id.to_def_id())
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Expand Up @@ -1166,7 +1166,7 @@ declare_lint_pass!(
);

fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
// trigger the query once for all constants since that will already report the errors
// FIXME: Use ensure here
let _ = cx.tcx.const_eval_poly(def_id);
Expand Down
37 changes: 18 additions & 19 deletions src/librustc_middle/hir/map/mod.rs
Expand Up @@ -150,16 +150,15 @@ impl<'hir> Map<'hir> {
}

pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id.expect_local()))
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
}

pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
self.tcx.definitions.def_path(def_id)
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
#[inline]
pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId {
pub fn local_def_id_from_node_id(&self, node: NodeId) -> LocalDefId {
self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| {
let hir_id = self.node_id_to_hir_id(node);
bug!(
Expand All @@ -173,24 +172,26 @@ impl<'hir> Map<'hir> {
// FIXME(eddyb) this function can and should return `LocalDefId`.
#[inline]
pub fn local_def_id(&self, hir_id: HirId) -> DefId {
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
bug!(
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
hir_id,
self.find_entry(hir_id)
)
})
self.opt_local_def_id(hir_id)
.unwrap_or_else(|| {
bug!(
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
hir_id,
self.find_entry(hir_id)
)
})
.to_def_id()
}

#[inline]
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
let node_id = self.hir_id_to_node_id(hir_id);
self.opt_local_def_id_from_node_id(node_id)
}

#[inline]
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<DefId> {
Some(self.tcx.definitions.opt_local_def_id(node)?.to_def_id())
pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option<LocalDefId> {
self.tcx.definitions.opt_local_def_id(node)
}

#[inline]
Expand Down Expand Up @@ -366,9 +367,8 @@ impl<'hir> Map<'hir> {
parent
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
self.local_def_id(self.body_owner(id))
pub fn body_owner_def_id(&self, id: BodyId) -> LocalDefId {
self.local_def_id(self.body_owner(id)).expect_local()
}

/// Given a `HirId`, returns the `BodyId` associated with it,
Expand Down Expand Up @@ -720,9 +720,8 @@ impl<'hir> Map<'hir> {
scope
}

// FIXME(eddyb) this function can and should return `LocalDefId`.
pub fn get_parent_did(&self, id: HirId) -> DefId {
self.local_def_id(self.get_parent_item(id))
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
self.local_def_id(self.get_parent_item(id)).expect_local()
}

pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/mod.rs
Expand Up @@ -2657,21 +2657,21 @@ pub enum ImplOverlapKind {

impl<'tcx> TyCtxt<'tcx> {
pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> {
self.typeck_tables_of(self.hir().body_owner_def_id(body))
self.typeck_tables_of(self.hir().body_owner_def_id(body).to_def_id())
}

/// Returns an iterator of the `DefId`s for all body-owners in this
/// crate. If you would prefer to iterate over the bodies
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx {
pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + Captures<'tcx> + 'tcx {
self.hir()
.krate()
.body_ids
.iter()
.map(move |&body_id| self.hir().body_owner_def_id(body_id))
}

pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) {
pub fn par_body_owners<F: Fn(LocalDefId) + sync::Sync + sync::Send>(self, f: F) {
par_iter(&self.hir().krate().body_ids)
.for_each(|&body_id| f(self.hir().body_owner_def_id(body_id)));
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/const_eval/fn_queries.rs
Expand Up @@ -84,11 +84,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {

pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
let parent_id = tcx.hir().get_parent_did(hir_id);
if !parent_id.is_top_level_module() {
is_const_impl_raw(tcx, parent_id.expect_local())
} else {
false
}
if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
}

/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/mod.rs
@@ -1,7 +1,7 @@
use crate::{shim, util};
use rustc_ast::ast;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_index::vec::IndexVec;
use rustc_middle::mir::{BodyAndCache, ConstQualifs, MirPhase, Promoted};
Expand Down Expand Up @@ -62,7 +62,7 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &DefIdSet {
let mut set = DefIdSet::default();

// All body-owners have MIR associated with them.
set.extend(tcx.body_owners());
set.extend(tcx.body_owners().map(LocalDefId::to_def_id));

// Additionally, tuple struct/variant constructors have MIR, but
// they don't have a BodyId, so we need to build them separately.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_passes/intrinsicck.rs
Expand Up @@ -131,8 +131,8 @@ impl Visitor<'tcx> for ItemVisitor<'tcx> {
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
let owner_def_id = self.tcx.hir().body_owner_def_id(body_id);
let body = self.tcx.hir().body(body_id);
let param_env = self.tcx.param_env(owner_def_id);
let tables = self.tcx.typeck_tables_of(owner_def_id);
let param_env = self.tcx.param_env(owner_def_id.to_def_id());
let tables = self.tcx.typeck_tables_of(owner_def_id.to_def_id());
ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body);
self.visit_body(body);
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_passes/reachable.rs
Expand Up @@ -10,7 +10,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirIdSet, Node};
Expand Down Expand Up @@ -42,7 +42,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>, attrs: Codegen
fn method_might_be_inlined(
tcx: TyCtxt<'_>,
impl_item: &hir::ImplItem<'_>,
impl_src: DefId,
impl_src: LocalDefId,
) -> bool {
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id());
let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id));
Expand All @@ -54,7 +54,7 @@ fn method_might_be_inlined(
return true;
}
}
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src) {
if let Some(impl_hir_id) = tcx.hir().as_local_hir_id(impl_src.to_def_id()) {
match tcx.hir().find(impl_hir_id) {
Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs),
Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"),
Expand Down Expand Up @@ -171,7 +171,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
true
} else {
let impl_did = self.tcx.hir().get_parent_did(hir_id);
let impl_did = self.tcx.hir().get_parent_did(hir_id).to_def_id();
// Check the impl. If the generics on the self
// type of the impl require inlining, this method
// does too.
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_privacy/lib.rs
Expand Up @@ -248,7 +248,7 @@ fn def_id_visibility<'tcx>(
}
}
Node::TraitItem(..) | Node::Variant(..) => {
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id));
return def_id_visibility(tcx, tcx.hir().get_parent_did(hir_id).to_def_id());
}
Node::ImplItem(impl_item) => {
match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) {
Expand All @@ -270,7 +270,7 @@ fn def_id_visibility<'tcx>(
let (mut ctor_vis, mut span, mut descr) =
def_id_visibility(tcx, parent_did);

let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
let ctor_did = tcx.hir().local_def_id(vdata.ctor_hir_id().unwrap());
let variant = adt_def.variant_with_ctor_id(ctor_did);

Expand Down Expand Up @@ -309,7 +309,8 @@ fn def_id_visibility<'tcx>(
// If the structure is marked as non_exhaustive then lower the
// visibility to within the crate.
if ctor_vis == ty::Visibility::Public {
let adt_def = tcx.adt_def(tcx.hir().get_parent_did(hir_id));
let adt_def =
tcx.adt_def(tcx.hir().get_parent_did(hir_id).to_def_id());
if adt_def.non_enum_variant().is_field_list_non_exhaustive() {
ctor_vis =
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
Expand Down
36 changes: 22 additions & 14 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -107,7 +107,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
where
F: FnOnce(&mut Self),
{
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id).to_def_id();

let tables = if self.tcx.has_typeck_tables(item_def_id) {
self.tcx.typeck_tables_of(item_def_id)
Expand Down Expand Up @@ -423,8 +423,10 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
vis: ast::Visibility,
attrs: &'l [Attribute],
) {
let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
);

if !self.span.filter_generated(ident.span) {
let sig = sig::assoc_const_signature(id, ident.name, typ, expr, &self.save_ctxt);
Expand Down Expand Up @@ -470,7 +472,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
let name = item.ident.to_string();
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
);

let kind = match item.kind {
Expand Down Expand Up @@ -670,7 +672,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
}
v.process_generic_params(generics, "", item.id);
for impl_item in impl_items {
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id));
v.process_impl_item(impl_item, map.local_def_id_from_node_id(item.id).to_def_id());
}
});
}
Expand All @@ -685,7 +687,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
let name = item.ident.to_string();
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id())
);
let mut val = name.clone();
if !generics.params.is_empty() {
Expand Down Expand Up @@ -751,7 +753,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
self.process_generic_params(generics, &qualname, item.id);
for method in methods {
let map = &self.tcx.hir();
self.process_trait_item(method, map.local_def_id_from_node_id(item.id))
self.process_trait_item(method, map.local_def_id_from_node_id(item.id).to_def_id())
}
}

Expand Down Expand Up @@ -1030,7 +1032,9 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
let name = trait_item.ident.name.to_string();
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(trait_item.id))
self.tcx.def_path_str(
self.tcx.hir().local_def_id_from_node_id(trait_item.id).to_def_id()
)
);

if !self.span.filter_generated(trait_item.ident.span) {
Expand Down Expand Up @@ -1134,7 +1138,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
.tcx
.hir()
.opt_local_def_id_from_node_id(id)
.and_then(|id| self.save_ctxt.tcx.parent(id))
.and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
.map(id_from_def_id);

match use_tree.kind {
Expand Down Expand Up @@ -1173,7 +1177,7 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {

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

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

let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id)));
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(id).to_def_id())
);

let sm = self.tcx.sess.source_map();
let filename = sm.span_to_filename(span);
Expand Down Expand Up @@ -1273,7 +1279,7 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
.tcx
.hir()
.opt_local_def_id_from_node_id(item.id)
.and_then(|id| self.save_ctxt.tcx.parent(id))
.and_then(|id| self.save_ctxt.tcx.parent(id.to_def_id()))
.map(id_from_def_id);
self.dumper.import(
&Access { public: false, reachable: false },
Expand Down Expand Up @@ -1311,7 +1317,9 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
TyAlias(_, ref ty_params, _, ref ty) => {
let qualname = format!(
"::{}",
self.tcx.def_path_str(self.tcx.hir().local_def_id_from_node_id(item.id))
self.tcx.def_path_str(
self.tcx.hir().local_def_id_from_node_id(item.id).to_def_id()
)
);
let value = match ty {
Some(ty) => ty_to_string(&ty),
Expand Down

0 comments on commit f363450

Please sign in to comment.