Skip to content

Commit 756466b

Browse files
committed
Rewrite each_attr to return a vector.
1 parent 27db3f0 commit 756466b

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

src/librustc/middle/traits/error_reporting.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
7070
span: Span) -> Option<String> {
7171
let def_id = trait_ref.def_id;
7272
let mut report = None;
73-
ty::each_attr(infcx.tcx, def_id, |item| {
73+
for item in ty::get_attrs(infcx.tcx, def_id).iter() {
7474
if item.check_name("rustc_on_unimplemented") {
7575
let err_sp = if item.meta().span == DUMMY_SP {
7676
span
@@ -136,11 +136,9 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
136136
eg `#[rustc_on_unimplemented = \"foo\"]`",
137137
trait_str).as_slice());
138138
}
139-
false
140-
} else {
141-
true
139+
break;
142140
}
143-
});
141+
}
144142
report
145143
}
146144

src/librustc/middle/ty.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
6868
use util::nodemap::{FnvHashMap};
6969

7070
use arena::TypedArena;
71-
use std::borrow::BorrowFrom;
71+
use std::borrow::{BorrowFrom, Cow};
7272
use std::cell::{Cell, RefCell};
7373
use std::cmp::{self, Ordering};
7474
use std::fmt::{self, Show};
7575
use std::hash::{Hash, Writer, SipHasher, Hasher};
7676
use std::mem;
7777
use std::ops;
7878
use std::rc::Rc;
79+
use std::vec::CowVec;
7980
use collections::enum_set::{EnumSet, CLike};
8081
use std::collections::{HashMap, HashSet};
8182
use syntax::abi;
@@ -5555,35 +5556,20 @@ pub fn predicates<'tcx>(
55555556
vec
55565557
}
55575558

5558-
/// Iterate over attributes of a definition.
5559-
// (This should really be an iterator.)
5560-
pub fn each_attr<F>(tcx: &ctxt, did: DefId, mut f: F) -> bool where
5561-
F: FnMut(&ast::Attribute) -> bool,
5562-
{
5559+
/// Get the attributes of a definition.
5560+
pub fn get_attrs<'tcx>(tcx: &'tcx ctxt, did: DefId)
5561+
-> CowVec<'tcx, ast::Attribute> {
55635562
if is_local(did) {
55645563
let item = tcx.map.expect_item(did.node);
5565-
item.attrs.iter().all(|attr| f(attr))
5564+
Cow::Borrowed(&item.attrs[])
55665565
} else {
5567-
info!("getting foreign attrs");
5568-
let attrs = csearch::get_item_attrs(&tcx.sess.cstore, did);
5569-
let cont = attrs.iter().all(|attr| f(attr));
5570-
info!("done");
5571-
cont
5566+
Cow::Owned(csearch::get_item_attrs(&tcx.sess.cstore, did))
55725567
}
55735568
}
55745569

55755570
/// Determine whether an item is annotated with an attribute
55765571
pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool {
5577-
let mut found = false;
5578-
each_attr(tcx, did, |item| {
5579-
if item.check_name(attr) {
5580-
found = true;
5581-
false
5582-
} else {
5583-
true
5584-
}
5585-
});
5586-
found
5572+
get_attrs(tcx, did).iter().any(|item| item.check_name(attr))
55875573
}
55885574

55895575
/// Determine whether an item is annotated with `#[repr(packed)]`
@@ -5600,13 +5586,9 @@ pub fn lookup_simd(tcx: &ctxt, did: DefId) -> bool {
56005586
pub fn lookup_repr_hints(tcx: &ctxt, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
56015587
memoized(&tcx.repr_hint_cache, did, |did: DefId| {
56025588
Rc::new(if did.krate == LOCAL_CRATE {
5603-
let mut acc = Vec::new();
5604-
ty::each_attr(tcx, did, |meta| {
5605-
acc.extend(attr::find_repr_attrs(tcx.sess.diagnostic(),
5606-
meta).into_iter());
5607-
true
5608-
});
5609-
acc
5589+
get_attrs(tcx, did).iter().flat_map(|meta| {
5590+
attr::find_repr_attrs(tcx.sess.diagnostic(), meta).into_iter()
5591+
}).collect()
56105592
} else {
56115593
csearch::get_repr_attrs(&tcx.sess.cstore, did)
56125594
})

src/librustc_trans/trans/base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,11 @@ pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId,
352352
// don't do this then linker errors can be generated where the linker
353353
// complains that one object files has a thread local version of the
354354
// symbol and another one doesn't.
355-
ty::each_attr(ccx.tcx(), did, |attr| {
355+
for attr in ty::get_attrs(ccx.tcx(), did).iter() {
356356
if attr.check_name("thread_local") {
357357
llvm::set_thread_local(c, true);
358358
}
359-
true
360-
});
359+
}
361360
ccx.externs().borrow_mut().insert(name.to_string(), c);
362361
return c;
363362
}

0 commit comments

Comments
 (0)