Skip to content

Commit

Permalink
Auto merge of #66131 - eddyb:local-def-id, r=petrochenkov
Browse files Browse the repository at this point in the history
rustc: use LocalDefId instead of DefIndex where possible.

That is, wherever `DefIndex` always referred to a "def" in the local crate, I replaced it with `LocalDefId`.
While `LocalDefId` already existed, it wasn't used a lot, but I hope I'm on the right track.

Unresolved questions:
* [x] ~~should `LocalDefId` implement `rustc_index::Idx`?~~
  * ~~this would get rid of a couple more `DefIndex` uses~~
* [x] ~~should `LocalDefId` be encoded/decoded as just a `DefIndex`?~~
  * ~~right now it's a bit messy, `LocalDefId` encodes/decodes like `DefId`~~
* [x] ~~should `DefId::assert_local` be named something else, like `expect_local`?~~

A future PR should change `tcx.hir().local_def_id(...)` to return `LocalDefId` instead of `DefId`, as changing it in this PR would be too noisy.

r? @michaelwoerister cc @nikomatsakis @petrochenkov @Zoxc
  • Loading branch information
bors committed Mar 19, 2020
2 parents 6724d58 + 16e25f0 commit 3c6f982
Show file tree
Hide file tree
Showing 49 changed files with 357 additions and 436 deletions.
12 changes: 6 additions & 6 deletions src/librustc/dep_graph/dep_node.rs
Expand Up @@ -63,7 +63,7 @@ use crate::ty::subst::SubstsRef;
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::HirId;
use rustc_span::symbol::Symbol;
use std::fmt;
Expand Down Expand Up @@ -413,19 +413,19 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
}
}

impl<'tcx> DepNodeParams<'tcx> for DefIndex {
impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;

fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
tcx.hir().definitions().def_path_hash(*self).0
self.to_def_id().to_fingerprint(tcx)
}

fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
tcx.def_path_str(DefId::local(*self))
self.to_def_id().to_debug_str(tcx)
}

fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
dep_node.extract_def_id(tcx).map(|id| id.index)
dep_node.extract_def_id(tcx).map(|id| id.expect_local())
}
}

Expand Down Expand Up @@ -477,7 +477,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
let HirId { owner, local_id } = *self;

let def_path_hash = tcx.def_path_hash(DefId::local(owner));
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());

def_path_hash.0.combine(local_id)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/graph.rs
Expand Up @@ -902,7 +902,7 @@ impl DepGraph {

fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
def_id.index == hir_id.owner
def_id.index == hir_id.owner.local_def_index
}

/// A "work product" is an intermediate result that we save into the
Expand Down
30 changes: 14 additions & 16 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -10,7 +10,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::svh::Svh;
use rustc_hir as hir;
use rustc_hir::def_id::CRATE_DEF_INDEX;
use rustc_hir::def_id::{DefIndex, LOCAL_CRATE};
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::*;
use rustc_index::vec::{Idx, IndexVec};
Expand All @@ -30,12 +30,12 @@ pub(super) struct NodeCollector<'a, 'hir> {
/// Source map
source_map: &'a SourceMap,

map: IndexVec<DefIndex, HirOwnerData<'hir>>,
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,

/// The parent of this node
parent_node: hir::HirId,

current_dep_node_owner: DefIndex,
current_dep_node_owner: LocalDefId,

definitions: &'a definitions::Definitions,

Expand Down Expand Up @@ -98,7 +98,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
definitions: &'a definitions::Definitions,
mut hcx: StableHashingContext<'a>,
) -> NodeCollector<'a, 'hir> {
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
let root_mod_def_path_hash =
definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX });

let mut hir_body_nodes = Vec::new();

Expand Down Expand Up @@ -126,7 +127,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
krate,
source_map: sess.source_map(),
parent_node: hir::CRATE_HIR_ID,
current_dep_node_owner: CRATE_DEF_INDEX,
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
definitions,
hcx,
hir_body_nodes,
Expand All @@ -148,7 +149,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
crate_disambiguator: CrateDisambiguator,
cstore: &dyn CrateStore,
commandline_args_hash: u64,
) -> (IndexVec<DefIndex, HirOwnerData<'hir>>, Svh) {
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
// Insert bodies into the map
for (id, body) in self.krate.bodies.iter() {
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
Expand Down Expand Up @@ -244,8 +245,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);

if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(node_id) {
Some(def_index) => self.definitions.def_path(def_index).to_string_no_crate(),
let node_str = match self.definitions.opt_local_def_id(node_id) {
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate(),
None => format!("{:?}", node),
};

Expand Down Expand Up @@ -285,7 +286,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
F: FnOnce(&mut Self, Fingerprint),
>(
&mut self,
dep_node_owner: DefIndex,
dep_node_owner: LocalDefId,
item_like: &T,
f: F,
) {
Expand Down Expand Up @@ -341,7 +342,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
debug!("visit_item: {:?}", i);
debug_assert_eq!(
i.hir_id.owner,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
);
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
Expand Down Expand Up @@ -373,7 +374,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
debug_assert_eq!(
ti.hir_id.owner,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
);
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
Expand All @@ -387,7 +388,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
debug_assert_eq!(
ii.hir_id.owner,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
);
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
Expand Down Expand Up @@ -506,10 +507,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
let node_id = self.definitions.hir_to_node_id(macro_def.hir_id);
let def_index = self.definitions.opt_def_index(node_id).unwrap();

self.with_dep_node_owner(def_index, macro_def, |this, hash| {
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
this.insert_with_hash(
macro_def.span,
macro_def.hir_id,
Expand Down

0 comments on commit 3c6f982

Please sign in to comment.