Skip to content

Commit c366e43

Browse files
committed
auto merge of #20957 : Ms2ger/rust/closures, r=alexcrichton
Returning the vectors directly makes the code a lot cleaner.
2 parents 4fd1e62 + 756466b commit c366e43

File tree

7 files changed

+30
-67
lines changed

7 files changed

+30
-67
lines changed

src/librustc/lint/builtin.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,8 @@ impl LintPass for UnusedResults {
774774
warned |= check_must_use(cx, &it.attrs[], s.span);
775775
}
776776
} else {
777-
csearch::get_item_attrs(&cx.sess().cstore, did, |attrs| {
778-
warned |= check_must_use(cx, &attrs[], s.span);
779-
});
777+
let attrs = csearch::get_item_attrs(&cx.sess().cstore, did);
778+
warned |= check_must_use(cx, &attrs[], s.span);
780779
}
781780
}
782781
_ => {}

src/librustc/metadata/csearch.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,11 @@ pub fn get_methods_if_impl(cstore: &cstore::CStore,
203203
decoder::get_methods_if_impl(cstore.intr.clone(), &*cdata, def.node)
204204
}
205205

206-
pub fn get_item_attrs<F>(cstore: &cstore::CStore,
207-
def_id: ast::DefId,
208-
f: F) where
209-
F: FnOnce(Vec<ast::Attribute>),
210-
{
206+
pub fn get_item_attrs(cstore: &cstore::CStore,
207+
def_id: ast::DefId)
208+
-> Vec<ast::Attribute> {
211209
let cdata = cstore.get_crate_data(def_id.krate);
212-
decoder::get_item_attrs(&*cdata, def_id.node, f)
210+
decoder::get_item_attrs(&*cdata, def_id.node)
213211
}
214212

215213
pub fn get_struct_fields(cstore: &cstore::CStore,

src/librustc/metadata/decoder.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,18 +1025,16 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
10251025
ret
10261026
}
10271027

1028-
pub fn get_item_attrs<F>(cdata: Cmd,
1029-
orig_node_id: ast::NodeId,
1030-
f: F) where
1031-
F: FnOnce(Vec<ast::Attribute>),
1032-
{
1028+
pub fn get_item_attrs(cdata: Cmd,
1029+
orig_node_id: ast::NodeId)
1030+
-> Vec<ast::Attribute> {
10331031
// The attributes for a tuple struct are attached to the definition, not the ctor;
10341032
// we assume that someone passing in a tuple struct ctor is actually wanting to
10351033
// look at the definition
10361034
let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id);
10371035
let node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id);
10381036
let item = lookup_item(node_id, cdata.data());
1039-
f(get_attributes(item));
1037+
get_attributes(item)
10401038
}
10411039

10421040
pub fn get_struct_field_attrs(cdata: Cmd) -> HashMap<ast::NodeId, Vec<ast::Attribute>> {

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 & 34 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,40 +5556,20 @@ pub fn predicates<'tcx>(
55555556
vec
55565557
}
55575558

5558-
/// Iterate over attributes of a definition.
5559-
// (This should really be an iterator, but that would require csearch and
5560-
// decoder to use iterators instead of higher-order functions.)
5561-
pub fn each_attr<F>(tcx: &ctxt, did: DefId, mut f: F) -> bool where
5562-
F: FnMut(&ast::Attribute) -> bool,
5563-
{
5559+
/// Get the attributes of a definition.
5560+
pub fn get_attrs<'tcx>(tcx: &'tcx ctxt, did: DefId)
5561+
-> CowVec<'tcx, ast::Attribute> {
55645562
if is_local(did) {
55655563
let item = tcx.map.expect_item(did.node);
5566-
item.attrs.iter().all(|attr| f(attr))
5564+
Cow::Borrowed(&item.attrs[])
55675565
} else {
5568-
info!("getting foreign attrs");
5569-
let mut cont = true;
5570-
csearch::get_item_attrs(&tcx.sess.cstore, did, |attrs| {
5571-
if cont {
5572-
cont = attrs.iter().all(|attr| f(attr));
5573-
}
5574-
});
5575-
info!("done");
5576-
cont
5566+
Cow::Owned(csearch::get_item_attrs(&tcx.sess.cstore, did))
55775567
}
55785568
}
55795569

55805570
/// Determine whether an item is annotated with an attribute
55815571
pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool {
5582-
let mut found = false;
5583-
each_attr(tcx, did, |item| {
5584-
if item.check_name(attr) {
5585-
found = true;
5586-
false
5587-
} else {
5588-
true
5589-
}
5590-
});
5591-
found
5572+
get_attrs(tcx, did).iter().any(|item| item.check_name(attr))
55925573
}
55935574

55945575
/// Determine whether an item is annotated with `#[repr(packed)]`
@@ -5605,13 +5586,9 @@ pub fn lookup_simd(tcx: &ctxt, did: DefId) -> bool {
56055586
pub fn lookup_repr_hints(tcx: &ctxt, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
56065587
memoized(&tcx.repr_hint_cache, did, |did: DefId| {
56075588
Rc::new(if did.krate == LOCAL_CRATE {
5608-
let mut acc = Vec::new();
5609-
ty::each_attr(tcx, did, |meta| {
5610-
acc.extend(attr::find_repr_attrs(tcx.sess.diagnostic(),
5611-
meta).into_iter());
5612-
true
5613-
});
5614-
acc
5589+
get_attrs(tcx, did).iter().flat_map(|meta| {
5590+
attr::find_repr_attrs(tcx.sess.diagnostic(), meta).into_iter()
5591+
}).collect()
56155592
} else {
56165593
csearch::get_repr_attrs(&tcx.sess.cstore, did)
56175594
})

src/librustc_trans/trans/base.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ fn get_extern_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty<'tcx>,
248248

249249
let f = decl_rust_fn(ccx, fn_ty, name);
250250

251-
csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| {
252-
set_llvm_fn_attrs(ccx, &attrs[], f)
253-
});
251+
let attrs = csearch::get_item_attrs(&ccx.sess().cstore, did);
252+
set_llvm_fn_attrs(ccx, &attrs[], f);
254253

255254
ccx.externs().borrow_mut().insert(name.to_string(), f);
256255
f
@@ -353,12 +352,11 @@ pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId,
353352
// don't do this then linker errors can be generated where the linker
354353
// complains that one object files has a thread local version of the
355354
// symbol and another one doesn't.
356-
ty::each_attr(ccx.tcx(), did, |attr| {
355+
for attr in ty::get_attrs(ccx.tcx(), did).iter() {
357356
if attr.check_name("thread_local") {
358357
llvm::set_thread_local(c, true);
359358
}
360-
true
361-
});
359+
}
362360
ccx.externs().borrow_mut().insert(name.to_string(), c);
363361
return c;
364362
}

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,8 @@ fn try_inline_def(cx: &DocContext, tcx: &ty::ctxt,
126126

127127
pub fn load_attrs(cx: &DocContext, tcx: &ty::ctxt,
128128
did: ast::DefId) -> Vec<clean::Attribute> {
129-
let mut attrs = Vec::new();
130-
csearch::get_item_attrs(&tcx.sess.cstore, did, |v| {
131-
attrs.extend(v.into_iter().map(|a| {
132-
a.clean(cx)
133-
}));
134-
});
135-
attrs
129+
let attrs = csearch::get_item_attrs(&tcx.sess.cstore, did);
130+
attrs.into_iter().map(|a| a.clean(cx)).collect()
136131
}
137132

138133
/// Record an external fully qualified name in the external_paths cache.

0 commit comments

Comments
 (0)