Skip to content

Various cleanup #21626

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 4 commits into from
Jan 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 5 additions & 9 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

// Searching for information from the cstore

#![allow(non_camel_case_types)]

pub use self::found_ast::*;

use metadata::common::*;
use metadata::cstore;
use metadata::decoder;
Expand Down Expand Up @@ -101,18 +97,18 @@ pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec<ast_map::PathElem>
r
}

pub enum found_ast<'ast> {
found(&'ast ast::InlinedItem),
found_parent(ast::DefId, &'ast ast::InlinedItem),
not_found,
pub enum FoundAst<'ast> {
Found(&'ast ast::InlinedItem),
FoundParent(ast::DefId, &'ast ast::InlinedItem),
NotFound,
}

// Finds the AST for this item in the crate metadata, if any. If the item was
// not marked for inlining, then the AST will not be present and hence none
// will be returned.
pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
decode_inlined_item: decoder::DecodeInlinedItem)
-> found_ast<'tcx> {
-> FoundAst<'tcx> {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(def.krate);
decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,23 +693,23 @@ pub type DecodeInlinedItem<'a> =

pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeId,
mut decode_inlined_item: DecodeInlinedItem)
-> csearch::found_ast<'tcx> {
-> csearch::FoundAst<'tcx> {
debug!("Looking up item: {}", id);
let item_doc = lookup_item(id, cdata.data());
let path = item_path(item_doc).init().to_vec();
match decode_inlined_item(cdata, tcx, path, item_doc) {
Ok(ii) => csearch::found(ii),
Ok(ii) => csearch::FoundAst::Found(ii),
Err(path) => {
match item_parent_item(item_doc) {
Some(did) => {
let did = translate_def_id(cdata, did);
let parent_item = lookup_item(did.node, cdata.data());
match decode_inlined_item(cdata, tcx, path, parent_item) {
Ok(ii) => csearch::found_parent(did, ii),
Err(_) => csearch::not_found
Ok(ii) => csearch::FoundAst::FoundParent(did, ii),
Err(_) => csearch::FoundAst::NotFound
}
}
None => csearch::not_found
None => csearch::FoundAst::NotFound
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@ fn encode_info_for_items(ecx: &EncodeContext,
&krate.module,
&[],
ast::CRATE_NODE_ID,
ast_map::Values([].iter()).chain(None),
[].iter().cloned().chain(None),
syntax::parse::token::special_idents::invalid,
ast::Public);

Expand Down Expand Up @@ -1949,7 +1949,7 @@ fn encode_misc_info(ecx: &EncodeContext,
}

// Encode reexports for the root module.
encode_reexports(ecx, rbml_w, 0, ast_map::Values([].iter()).chain(None));
encode_reexports(ecx, rbml_w, 0, [].iter().cloned().chain(None));

rbml_w.end_tag();
rbml_w.end_tag();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
debug!("> Decoding inlined fn: {:?}::?",
{
// Do an Option dance to use the path after it is moved below.
let s = ast_map::path_to_string(ast_map::Values(path.iter()));
let s = ast_map::path_to_string(path.iter().cloned());
path_as_str = Some(s);
path_as_str.as_ref().map(|x| &x[])
});
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
}
let expr_id = match csearch::maybe_get_item_ast(tcx, enum_def,
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
csearch::found(&ast::IIItem(ref item)) => match item.node {
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
ast::ItemEnum(ast::EnumDef { ref variants }, _) => {
// NOTE this doesn't do the right thing, it compares inlined
// NodeId's to the original variant_def's NodeId, but they
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn lookup_const_by_id<'a>(tcx: &'a ty::ctxt, def_id: ast::DefId)
}
let expr_id = match csearch::maybe_get_item_ast(tcx, def_id,
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
csearch::found(&ast::IIItem(ref item)) => match item.node {
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
ast::ItemConst(_, ref const_expr) => Some(const_expr.id),
_ => None
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5249,7 +5249,7 @@ pub fn with_path<T, F>(cx: &ctxt, id: ast::DefId, f: F) -> T where
if id.krate == ast::LOCAL_CRATE {
cx.map.with_path(id.node, f)
} else {
f(ast_map::Values(csearch::get_item_path(cx, id).iter()).chain(None))
f(csearch::get_item_path(cx, id).iter().cloned().chain(None))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use flate;
use serialize::hex::ToHex;
use syntax::ast;
use syntax::ast_map::{PathElem, PathElems, PathName};
use syntax::ast_map;
use syntax::attr::AttrMetaMethods;
use syntax::codemap::Span;
use syntax::parse::token;
Expand Down Expand Up @@ -339,7 +338,7 @@ pub fn mangle_internal_name_by_type_and_seq<'a, 'tcx>(ccx: &CrateContext<'a, 'tc
let path = [PathName(token::intern(&s[])),
gensym_name(name)];
let hash = get_symbol_hash(ccx, t);
mangle(ast_map::Values(path.iter()), Some(&hash[]))
mangle(path.iter().cloned(), Some(&hash[]))
}

pub fn mangle_internal_name_by_path_and_seq(path: PathElems, flav: &str) -> String {
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_trans/trans/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
box |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));

let inline_def = match csearch_result {
csearch::not_found => {
csearch::FoundAst::NotFound => {
ccx.external().borrow_mut().insert(fn_id, None);
return None;
}
csearch::found(&ast::IIItem(ref item)) => {
csearch::FoundAst::Found(&ast::IIItem(ref item)) => {
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);

Expand Down Expand Up @@ -90,12 +90,12 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)

local_def(item.id)
}
csearch::found(&ast::IIForeign(ref item)) => {
csearch::FoundAst::Found(&ast::IIForeign(ref item)) => {
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
local_def(item.id)
}
csearch::found_parent(parent_id, &ast::IIItem(ref item)) => {
csearch::FoundAst::FoundParent(parent_id, &ast::IIItem(ref item)) => {
ccx.external().borrow_mut().insert(parent_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, parent_id);

Expand Down Expand Up @@ -124,11 +124,11 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
trans_item(ccx, &**item);
local_def(my_id)
}
csearch::found_parent(_, _) => {
ccx.sess().bug("maybe_get_item_ast returned a found_parent \
csearch::FoundAst::FoundParent(_, _) => {
ccx.sess().bug("maybe_get_item_ast returned a FoundParent \
with a non-item parent");
}
csearch::found(&ast::IITraitItem(_, ref trait_item)) => {
csearch::FoundAst::Found(&ast::IITraitItem(_, ref trait_item)) => {
match *trait_item {
ast::RequiredMethod(_) => ccx.sess().bug("found RequiredMethod IITraitItem"),
ast::ProvidedMethod(ref mth) => {
Expand All @@ -147,7 +147,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
}
}
}
csearch::found(&ast::IIImplItem(impl_did, ref impl_item)) => {
csearch::FoundAst::Found(&ast::IIImplItem(impl_did, ref impl_item)) => {
match *impl_item {
ast::MethodImplItem(ref mth) => {
ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
Expand Down
21 changes: 4 additions & 17 deletions src/libsyntax/ast_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,8 @@ impl<'a> Iterator for LinkedPath<'a> {
}
}

// HACK(eddyb) move this into libstd (value wrapper for slice::Iter).
#[derive(Clone)]
pub struct Values<'a, T:'a>(pub slice::Iter<'a, T>);

impl<'a, T: Copy> Iterator for Values<'a, T> {
type Item = T;

fn next(&mut self) -> Option<T> {
let &mut Values(ref mut items) = self;
items.next().map(|&x| x)
}
}

/// The type of the iterator used by with_path.
pub type PathElems<'a, 'b> = iter::Chain<Values<'a, PathElem>, LinkedPath<'b>>;
pub type PathElems<'a, 'b> = iter::Chain<iter::Cloned<slice::Iter<'a, PathElem>>, LinkedPath<'b>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙀 This is amazing!


pub fn path_to_string<PI: Iterator<Item=PathElem>>(path: PI) -> String {
let itr = token::get_ident_interner();
Expand All @@ -101,7 +88,7 @@ pub fn path_to_string<PI: Iterator<Item=PathElem>>(path: PI) -> String {
}
s.push_str(&e[]);
s
}).to_string()
})
}

#[derive(Copy, Show)]
Expand Down Expand Up @@ -458,9 +445,9 @@ impl<'ast> Map<'ast> {
if parent == id {
match self.find_entry(id) {
Some(RootInlinedParent(data)) => {
f(Values(data.path.iter()).chain(next))
f(data.path.iter().cloned().chain(next))
}
_ => f(Values([].iter()).chain(next))
_ => f([].iter().cloned().chain(next))
}
} else {
self.with_path_next(parent, Some(&LinkedPathNode {
Expand Down
21 changes: 7 additions & 14 deletions src/libsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,20 +670,13 @@ pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {

// are two arrays of segments equal when compared unhygienically?
pub fn segments_name_eq(a : &[ast::PathSegment], b : &[ast::PathSegment]) -> bool {
if a.len() != b.len() {
false
} else {
for (idx,seg) in a.iter().enumerate() {
if seg.identifier.name != b[idx].identifier.name
// FIXME #7743: ident -> name problems in lifetime comparison?
// can types contain idents?
|| seg.parameters != b[idx].parameters
{
return false;
}
}
true
}
a.len() == b.len() &&
a.iter().zip(b.iter()).all(|(s, t)| {
s.identifier.name == t.identifier.name &&
// FIXME #7743: ident -> name problems in lifetime comparison?
// can types contain idents?
s.parameters == t.parameters
})
}

/// Returns true if this literal is a string and false otherwise.
Expand Down