8000 Cleanup refactoring around DefPath handling by michaelwoerister · Pull Request #38418 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Cleanup refactoring around DefPath handling #38418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move retrace_path() implementation to DefPathTable
  • Loading branch information
michaelwoerister committed Dec 17, 2016
commit 72f95aac1ba1891fcf13b568514520205e3c848b
33 changes: 31 additions & 2 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
//! There are also some rather random cases (like const initializer
//! expressions) that are mostly just leftovers.



use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
Expand Down Expand Up @@ -57,6 +55,37 @@ impl DefPathTable {
pub fn contains_key(&self, key: &DefKey) -> bool {
self.key_to_index.contains_key(key)
}

pub fn retrace_path(&self,
path_data: &[DisambiguatedDefPathData])
-> Option<DefIndex> {
let root_key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
data: DefPathData::CrateRoot,
disambiguator: 0,
},
};

let root_index = self.key_to_index
.get(&root_key)
.expect("no root key?")
.clone();

debug!("retrace_path: root_index={:?}", root_index);

let mut index = root_index;
for data in path_data {
let key = DefKey { parent: Some(index), disambiguated_data: data.clone() };
debug!("retrace_path: key={:?}", key);
match self.key_to_index.get(&key) {
Some(&i) => index = i,
None => return None,
}
}

Some(index)
}
}


Expand Down
27 changes: 14 additions & 13 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use hir::def::{self, Def};
use hir::def_id::{CrateNum, DefId, DefIndex};
use hir::map as hir_map;
use hir::map::definitions::{Definitions, DefKey};
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
use hir::svh::Svh;
use middle::lang_items;
use ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -336,11 +336,11 @@ pub trait CrateStore<'tcx> {
fn is_no_builtins(&self, cnum: CrateNum) -> bool;

// resolve
fn def_index_for_def_key(&self,
cnum: CrateNum,
def: DefKey)
-> Option<DefIndex>;
fn def_key(&self, def: DefId) -> hir_map::DefKey;
fn retrace_path(&self,
cnum: CrateNum,
path_data: &[DisambiguatedDefPathData])
-> Option<DefId>;
fn def_key(&self, def: DefId) -> DefKey;
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<def::Export>;
Expand Down Expand Up @@ -442,12 +442,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {

// trait info
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
fn def_index_for_def_key(&self,
cnum: CrateNum,
def: DefKey)
-> Option<DefIndex> {
None
}

// impl info
fn associated_item_def_ids(&self, def_id: DefId) -> Vec<DefId>
Expand Down Expand Up @@ -508,7 +502,14 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn is_no_builtins(&self, cnum: CrateNum) -> bool { bug!("is_no_builtins") }

// resolve
fn def_key(&self, def: DefId) -> hir_map::DefKey { bug!("def_key") }
fn retrace_path(&self,
cnum: CrateNum,
path_data: &[DisambiguatedDefPathData])
-> Option<DefId> {
None
}

fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
bug!("relative_def_path")
}
Expand Down
49 changes: 10 additions & 39 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use session::Session;
use middle;
use hir::TraitMap;
use hir::def::Def;
use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::map as ast_map;
use hir::map::{DefKey, DefPathData, DisambiguatedDefPathData};
use hir::map::DisambiguatedDefPathData;
use middle::free_region::FreeRegionMap;
use middle::region::RegionMaps;
use middle::resolve_lifetime;
Expand Down Expand Up @@ -627,50 +627,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

/// Given a def-key `key` and a crate `krate`, finds the def-index
/// that `krate` assigned to `key`. This `DefIndex` will always be
/// relative to `krate`.
///
/// Returns `None` if there is no `DefIndex` with that key.
pub fn def_index_for_def_key(self, krate: CrateNum, key: DefKey)
-> Option<DefIndex> {
if krate == LOCAL_CRATE {
self.map.def_index_for_def_key(key)
} else {
self.sess.cstore.def_index_for_def_key(krate, key)
}
}

pub fn retrace_path(self,
krate: CrateNum,
path_data: &[DisambiguatedDefPathData])
-> Option<DefId> {
debug!("retrace_path(path={:?}, krate={:?})", path_data, self.crate_name(krate));

let root_key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
data: DefPathData::CrateRoot,
disambiguator: 0,
},
};

let root_index = self.def_index_for_def_key(krate, root_key)
.expect("no root key?");

debug!("retrace_path: root_index={:?}", root_index);

let mut index = root_index;
for data in path_data {
let key = DefKey { parent: Some(index), disambiguated_data: data.clone() };
debug!("retrace_path: key={:?}", key);
match self.def_index_for_def_key(krate, key) {
Some(i) => index = i,
None => return None,
}
if krate == LOCAL_CRATE {
self.map
.definitions()
.def_path_table()
.retrace_path(path_data)
.map(|def_index| DefId { krate: krate, index: def_index })
} else {
self.sess.cstore.retrace_path(krate, path_data)
}

Some(DefId { krate: krate, index: index })
}

pub fn type_parameter_def(self,
Expand Down
19 changes: 10 additions & 9 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use rustc::ty::{self, Ty, TyCtxt};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};

use rustc::dep_graph::DepNode;
use rustc::hir::map as hir_map;
use rustc::hir::map::DefKey;
use rustc::hir::map::{DefKey, DefPath, DisambiguatedDefPathData};
use rustc::mir::Mir;
use rustc::util::nodemap::{NodeSet, DefIdMap};
use rustc_back::PanicStrategy;
Expand Down Expand Up @@ -336,18 +335,20 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(cnum).is_no_builtins()
}

fn def_index_for_def_key(&self,
cnum: CrateNum,
def: DefKey)
-> Option<DefIndex> {
fn retrace_path(&self,
cnum: CrateNum,
path: &[DisambiguatedDefPathData])
-> Option<DefId> {
let cdata = self.get_crate_data(cnum);
cdata.def_path_table.def_index_for_def_key(&def)
cdata.def_path_table
.retrace_path(&path)
.map(|index| DefId { krate: cnum, index: index })
}

/// Returns the `DefKey` for a given `DefId`. This indicates the
/// parent `DefId` as well as some idea of what kind of data the
/// `DefId` refers to.
fn def_key(&self, def: DefId) -> hir_map::DefKey {
fn def_key(&self, def: DefId) -> DefKey {
// Note: loading the def-key (or def-path) for a def-id is not
// a *read* of its metadata. This is because the def-id is
// really just an interned shorthand for a def-path, which is the
Expand All @@ -357,7 +358,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def.krate).def_key(def.index)
}

fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
fn relative_def_path(&self, def: DefId) -> Option<DefPath> {
// See `Note` above in `def_key()` for why this read is
// commented out:
//
Expand Down
0