From f357d559ca0835ba2fcad11eb30a8ce821b6df58 Mon Sep 17 00:00:00 2001 From: mitaa Date: Wed, 12 Aug 2015 20:22:25 +0200 Subject: [PATCH 1/2] Replace get_item_path[-1] with get_item_name --- src/librustc/metadata/csearch.rs | 6 ++++++ src/librustc/metadata/decoder.rs | 4 ++++ src/librustc/middle/intrinsicck.rs | 18 +----------------- src/librustc/middle/ty.rs | 8 ++++++++ src/librustc_trans/trans/debuginfo/metadata.rs | 9 +-------- src/librustc_trans/trans/meth.rs | 12 ++---------- src/librustdoc/clean/inline.rs | 3 +-- 7 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs index efa17912a32..693d6881b7f 100644 --- a/src/librustc/metadata/csearch.rs +++ b/src/librustc/metadata/csearch.rs @@ -95,6 +95,12 @@ pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec }) } +pub fn get_item_name(tcx: &ty::ctxt, def: ast::DefId) -> ast::Name { + let cstore = &tcx.sess.cstore; + let cdata = cstore.get_crate_data(def.krate); + decoder::get_item_name(&cstore.intr, &cdata, def.node) +} + pub enum FoundAst<'ast> { Found(&'ast ast::InlinedItem), FoundParent(ast::DefId, &'ast ast::InlinedItem), diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index df5f798217f..3aacc8d68af 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -770,6 +770,10 @@ pub fn get_item_path(cdata: Cmd, id: ast::NodeId) -> Vec { item_path(lookup_item(id, cdata.data())) } +pub fn get_item_name(intr: &IdentInterner, cdata: Cmd, id: ast::NodeId) -> ast::Name { + item_name(intr, lookup_item(id, cdata.data())) +} + pub type DecodeInlinedItem<'a> = Box FnMut(Cmd, &ty::ctxt<'tcx>, diff --git a/src/librustc/middle/intrinsicck.rs b/src/librustc/middle/intrinsicck.rs index 93899a5cf80..dc45f9a91cd 100644 --- a/src/librustc/middle/intrinsicck.rs +++ b/src/librustc/middle/intrinsicck.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ast_map::NodeForeignItem; -use metadata::csearch; use middle::def::DefFn; use middle::subst::{Subst, Substs, EnumeratedItems}; use middle::ty::{TransmuteRestriction, ctxt, TyBareFn}; @@ -57,21 +55,7 @@ fn def_id_is_transmute(&self, def_id: DefId) -> bool { ty::TyBareFn(_, ref bfty) => bfty.abi == RustIntrinsic, _ => return false }; - if def_id.krate == ast::LOCAL_CRATE { - match self.tcx.map.get(def_id.node) { - NodeForeignItem(ref item) if intrinsic => { - item.ident.name == "transmute" - } - _ => false, - } - } else { - match csearch::get_item_path(self.tcx, def_id).last() { - Some(ref last) if intrinsic => { - last.name() == "transmute" - } - _ => false, - } - } + intrinsic && self.tcx.item_name(def_id) == "transmute" } fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>, id: ast::NodeId) { diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 2fe1f14d521..78b380589d5 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -5927,6 +5927,14 @@ pub fn with_path(&self, id: ast::DefId, f: F) -> T where } } + pub fn item_name(&self, id: ast::DefId) -> ast::Name { + if id.krate == ast::LOCAL_CRATE { + self.map.get_path_elem(id.node).name() + } else { + csearch::get_item_name(self, id) + } + } + /// Returns `(normalized_type, ty)`, where `normalized_type` is the /// IntType representation of one of {i64,i32,i16,i8,u64,u32,u16,u8}, /// and `ty` is the original type (i.e. may include `isize` or diff --git a/src/librustc_trans/trans/debuginfo/metadata.rs b/src/librustc_trans/trans/debuginfo/metadata.rs index 0be155b7727..1110a467934 100644 --- a/src/librustc_trans/trans/debuginfo/metadata.rs +++ b/src/librustc_trans/trans/debuginfo/metadata.rs @@ -23,7 +23,6 @@ use llvm::{self, ValueRef}; use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor, DICompositeType}; -use metadata::csearch; use middle::pat_util; use middle::subst::{self, Substs}; use rustc::ast_map; @@ -1686,13 +1685,7 @@ fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, fn get_enum_discriminant_name(cx: &CrateContext, def_id: ast::DefId) -> token::InternedString { - let name = if def_id.krate == ast::LOCAL_CRATE { - cx.tcx().map.get_path_elem(def_id.node).name() - } else { - csearch::get_item_path(cx.tcx(), def_id).last().unwrap().name() - }; - - name.as_str() + cx.tcx().item_name(def_id).as_str() } } diff --git a/src/librustc_trans/trans/meth.rs b/src/librustc_trans/trans/meth.rs index 499e0911da8..3fafd6e7d01 100644 --- a/src/librustc_trans/trans/meth.rs +++ b/src/librustc_trans/trans/meth.rs @@ -12,12 +12,10 @@ use back::abi; use back::link; use llvm::{ValueRef, get_params}; -use metadata::csearch; use middle::subst::{Subst, Substs}; use middle::subst::VecPerParamSpace; use middle::subst; use middle::traits; -use rustc::ast_map; use trans::base::*; use trans::build::*; use trans::callee::*; @@ -165,14 +163,8 @@ pub fn trans_static_method_callee<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, tcx.item_path_str(trait_id), expr_id); - let mname = if method_id.krate == ast::LOCAL_CRATE { - match tcx.map.get(method_id.node) { - ast_map::NodeTraitItem(trait_item) => trait_item.ident.name, - _ => panic!("callee is not a trait method") - } - } else { - csearch::get_item_path(tcx, method_id).last().unwrap().name() - }; + let mname = tcx.item_name(method_id); + debug!("trans_static_method_callee: method_id={:?}, expr_id={}, \ name={}", method_id, expr_id, mname); diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 7fd1555d066..6f51e70b0a0 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -113,11 +113,10 @@ fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt, } _ => return None, }; - let fqn = csearch::get_item_path(tcx, did); cx.inlined.borrow_mut().as_mut().unwrap().insert(did); ret.push(clean::Item { source: clean::Span::empty(), - name: Some(fqn.last().unwrap().to_string()), + name: Some(tcx.item_name(did).to_string()), attrs: load_attrs(cx, tcx, did), inner: inner, visibility: Some(ast::Public), From d81feb85f7b575b04a7249ccbf3a6b3fcc8c0e9d Mon Sep 17 00:00:00 2001 From: mitaa Date: Tue, 11 Aug 2015 19:22:22 +0200 Subject: [PATCH 2/2] Remove duplicated path_to_string conversion --- src/librustc/middle/ty.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 78b380589d5..451e1526f83 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -5683,13 +5683,9 @@ pub fn note_and_explain_type_err(&self, err: &TypeError<'tcx>, sp: Span) { &format!("a default was defined here...")); } (_, _) => { - let elems = csearch::get_item_path(self, expected.def_id) - .into_iter() - .map(|p| p.to_string()) - .collect::>(); self.sess.note( &format!("a default is defined on `{}`", - elems.join("::"))); + self.item_path_str(expected.def_id))); } } @@ -5704,13 +5700,9 @@ pub fn note_and_explain_type_err(&self, err: &TypeError<'tcx>, sp: Span) { &format!("a second default was defined here...")); } (_, _) => { - let elems = csearch::get_item_path(self, found.def_id) - .into_iter() - .map(|p| p.to_string()) - .collect::>(); - self.sess.note( - &format!("a second default is defined on `{}`", elems.join(" "))); + &format!("a second default is defined on `{}`", + self.item_path_str(found.def_id))); } }