From fdef27acf5a233220272776cd565378a4fc59459 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 28 Jun 2017 13:59:16 +0200 Subject: [PATCH] Copy `path_to_def` from clippy --- src/terminator/mod.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/terminator/mod.rs b/src/terminator/mod.rs index dc6610a2213..450cae3b49c 100644 --- a/src/terminator/mod.rs +++ b/src/terminator/mod.rs @@ -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 { + 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 + }) + } }