Copy path_to_def from clippy

This commit is contained in:
Oliver Schneider 2017-06-28 13:59:16 +02:00 committed by Oliver Schneider
parent 14c8fe7ace
commit fdef27acf5
No known key found for this signature in database
GPG Key ID: 1D5CB4FC597C3004

View File

@ -1,4 +1,5 @@
use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc::hir::def::Def;
use rustc::mir;
use rustc::ty::{self, TypeVariants, Ty};
use rustc::ty::layout::Layout;
@ -13,6 +14,8 @@
use value::{PrimVal, Value};
use rustc_data_structures::indexed_vec::Idx;
use std::mem;
mod drop;
mod intrinsic;
@ -933,4 +936,34 @@ fn call_c_abi(
self.goto_block(dest_block);
Ok(())
}
/// Get the definition associated to a path.
fn path_to_def(&self, path: &[&str]) -> Option<Def> {
let cstore = &self.tcx.sess.cstore;
let crates = cstore.crates();
crates.iter()
.find(|&&krate| cstore.crate_name(krate) == path[0])
.and_then(|krate| {
let krate = DefId {
krate: *krate,
index: CRATE_DEF_INDEX,
};
let mut items = cstore.item_children(krate, self.tcx.sess);
let mut path_it = path.iter().skip(1).peekable();
while let Some(segment) = path_it.next() {
for item in &mem::replace(&mut items, vec![]) {
if item.ident.name == *segment {
if path_it.peek().is_none() {
return Some(item.def);
}
items = cstore.item_children(item.def.def_id(), self.tcx.sess);
break;
}
}
}
None
})
}
}