Skip to content

Commit e064809

Browse files
committed
Port more stuff to mark used attributes
1 parent 50181ad commit e064809

File tree

8 files changed

+63
-53
lines changed

8 files changed

+63
-53
lines changed

src/librustc/metadata/csearch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn get_static_methods_if_impl(cstore: &cstore::CStore,
176176

177177
pub fn get_item_attrs(cstore: &cstore::CStore,
178178
def_id: ast::DefId,
179-
f: |Vec<@ast::MetaItem> |) {
179+
f: |Vec<ast::Attribute> |) {
180180
let cdata = cstore.get_crate_data(def_id.krate);
181181
decoder::get_item_attrs(&*cdata, def_id.node, f)
182182
}

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -953,20 +953,14 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
953953

954954
pub fn get_item_attrs(cdata: Cmd,
955955
orig_node_id: ast::NodeId,
956-
f: |Vec<@ast::MetaItem> |) {
956+
f: |Vec<ast::Attribute>|) {
957957
// The attributes for a tuple struct are attached to the definition, not the ctor;
958958
// we assume that someone passing in a tuple struct ctor is actually wanting to
959959
// look at the definition
960960
let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id);
961961
let node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id);
962962
let item = lookup_item(node_id, cdata.data());
963-
reader::tagged_docs(item, tag_attributes, |attributes| {
964-
reader::tagged_docs(attributes, tag_attribute, |attribute| {
965-
f(get_meta_items(attribute));
966-
true
967-
});
968-
true
969-
});
963+
f(get_attributes(item));
970964
}
971965

972966
fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {

src/librustc/middle/lint.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use collections::SmallIntMap;
6464
use syntax::abi;
6565
use syntax::ast_map;
6666
use syntax::ast_util::IdVisitingOperation;
67-
use syntax::attr::{AttrMetaMethods, AttributeMethods};
67+
use syntax::attr::AttrMetaMethods;
6868
use syntax::attr;
6969
use syntax::codemap::Span;
7070
use syntax::parse::token::InternedString;
@@ -1148,8 +1148,7 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) {
11481148
fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) {
11491149
for attr in attrs.iter() {
11501150
if !attr::is_used(attr) {
1151-
cx.span_lint(UnusedAttribute, attr.span,
1152-
format!("unused attribute {}", attr.name()).as_slice());
1151+
cx.span_lint(UnusedAttribute, attr.span, "unused attribute");
11531152
}
11541153
}
11551154
}
@@ -1654,9 +1653,7 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
16541653
let stability = if ast_util::is_local(id) {
16551654
// this crate
16561655
let s = cx.tcx.map.with_attrs(id.node, |attrs| {
1657-
attrs.map(|a| {
1658-
attr::find_stability(a.iter().map(|a| a.meta()))
1659-
})
1656+
attrs.map(|a| attr::find_stability(a.as_slice()))
16601657
});
16611658
match s {
16621659
Some(s) => s,
@@ -1672,9 +1669,9 @@ fn check_stability(cx: &Context, e: &ast::Expr) {
16721669
let mut s = None;
16731670
// run through all the attributes and take the first
16741671
// stability one.
1675-
csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |meta_items| {
1672+
csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |attrs| {
16761673
if s.is_none() {
1677-
s = attr::find_stability(meta_items.move_iter())
1674+
s = attr::find_stability(attrs.as_slice())
16781675
}
16791676
});
16801677
s

src/librustc/middle/trans/base.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,8 @@ fn get_extern_rust_fn(ccx: &CrateContext, fn_ty: ty::t, name: &str, did: ast::De
227227

228228
let f = decl_rust_fn(ccx, fn_ty, name);
229229

230-
csearch::get_item_attrs(&ccx.sess().cstore, did, |meta_items| {
231-
set_llvm_fn_attrs(meta_items.iter().map(|&x| {
232-
attr::mk_attr_outer(attr::mk_attr_id(), x)
233-
}).collect::<Vec<_>>().as_slice(), f)
230+
csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| {
231+
set_llvm_fn_attrs(attrs.as_slice(), f)
234232
});
235233

236234
ccx.externs.borrow_mut().insert(name.to_strbuf(), f);

src/librustc/middle/ty.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,20 +3889,22 @@ pub fn lookup_trait_def(cx: &ctxt, did: ast::DefId) -> Rc<ty::TraitDef> {
38893889
}
38903890
}
38913891

3892-
/// Iterate over meta_items of a definition.
3892+
/// Iterate over attributes of a definition.
38933893
// (This should really be an iterator, but that would require csearch and
38943894
// decoder to use iterators instead of higher-order functions.)
3895-
pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool {
3895+
pub fn each_attr(tcx: &ctxt, did: DefId, f: |&ast::Attribute| -> bool) -> bool {
38963896
if is_local(did) {
38973897
let item = tcx.map.expect_item(did.node);
3898-
item.attrs.iter().advance(|attr| f(attr.node.value))
3898+
item.attrs.iter().advance(|attr| f(attr))
38993899
} else {
3900+
info!("getting foreign attrs");
39003901
let mut cont = true;
3901-
csearch::get_item_attrs(&tcx.sess.cstore, did, |meta_items| {
3902+
csearch::get_item_attrs(&tcx.sess.cstore, did, |attrs| {
39023903
if cont {
3903-
cont = meta_items.iter().advance(|ptrptr| f(*ptrptr));
3904+
cont = attrs.iter().advance(|attr| f(attr));
39043905
}
39053906
});
3907+
info!("done");
39063908
cont
39073909
}
39083910
}
@@ -3911,7 +3913,7 @@ pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool {
39113913
pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool {
39123914
let mut found = false;
39133915
each_attr(tcx, did, |item| {
3914-
if item.name().equiv(&attr) {
3916+
if item.check_name(attr) {
39153917
found = true;
39163918
false
39173919
} else {

src/librustdoc/html/render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,8 @@ impl<'a> fmt::Show for Item<'a> {
10911091
shortty(self.item), self.item.name.get_ref().as_slice()));
10921092

10931093
// Write stability attributes
1094-
match attr::find_stability(self.item.attrs.iter()) {
1095-
Some(ref stability) => {
1094+
match attr::find_stability_generic(self.item.attrs.iter()) {
1095+
Some((ref stability, _)) => {
10961096
try!(write!(fmt,
10971097
"<a class='stability {lvl}' title='{reason}'>{lvl}</a>",
10981098
lvl = stability.level.to_str(),

src/libsyntax/attr.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,22 @@ pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool {
238238
debug!("attr::contains_name (name={})", name);
239239
metas.iter().any(|item| {
240240
debug!(" testing: {}", item.name());
241-
item.name().equiv(&name)
241+
item.check_name(name)
242242
})
243243
}
244244

245245
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
246246
-> Option<InternedString> {
247247
attrs.iter()
248-
.find(|at| at.name().equiv(&name))
248+
.find(|at| at.check_name(name))
249249
.and_then(|at| at.value_str())
250250
}
251251

252252
pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str)
253253
-> Option<InternedString> {
254254
items.iter()
255255
.rev()
256-
.find(|mi| mi.name().equiv(&name))
256+
.find(|mi| mi.check_name(name))
257257
.and_then(|i| i.value_str())
258258
}
259259

@@ -289,7 +289,7 @@ pub fn sort_meta_items(items: &[@MetaItem]) -> Vec<@MetaItem> {
289289
*/
290290
pub fn find_linkage_metas(attrs: &[Attribute]) -> Vec<@MetaItem> {
291291
let mut result = Vec::new();
292-
for attr in attrs.iter().filter(|at| at.name().equiv(&("link"))) {
292+
for attr in attrs.iter().filter(|at| at.check_name("link")) {
293293
match attr.meta().node {
294294
MetaList(_, ref items) => result.push_all(items.as_slice()),
295295
_ => ()
@@ -318,17 +318,21 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
318318
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
319319
attrs.iter().fold(InlineNone, |ia,attr| {
320320
match attr.node.value.node {
321-
MetaWord(ref n) if n.equiv(&("inline")) => InlineHint,
322-
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
323-
if contains_name(items.as_slice(), "always") {
324-
InlineAlways
325-
} else if contains_name(items.as_slice(), "never") {
326-
InlineNever
327-
} else {
321+
MetaWord(ref n) if n.equiv(&("inline")) => {
322+
mark_used(attr);
328323
InlineHint
329324
}
330-
}
331-
_ => ia
325+
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
326+
mark_used(attr);
327+
if contains_name(items.as_slice(), "always") {
328+
InlineAlways
329+
} else if contains_name(items.as_slice(), "never") {
330+
InlineNever
331+
} else {
332+
InlineHint
333+
}
334+
}
335+
_ => ia
332336
}
333337
})
334338
}
@@ -348,7 +352,7 @@ pub fn test_cfg<AM: AttrMetaMethods, It: Iterator<AM>>
348352
// this doesn't work.
349353
let some_cfg_matches = metas.any(|mi| {
350354
debug!("testing name: {}", mi.name());
351-
if mi.name().equiv(&("cfg")) { // it is a #[cfg()] attribute
355+
if mi.check_name("cfg") { // it is a #[cfg()] attribute
352356
debug!("is cfg");
353357
no_cfgs = false;
354358
// only #[cfg(...)] ones are understood.
@@ -399,11 +403,13 @@ pub enum StabilityLevel {
399403
Locked
400404
}
401405

402-
/// Find the first stability attribute. `None` if none exists.
403-
pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
404-
-> Option<Stability> {
405-
for m in metas {
406-
let level = match m.name().get() {
406+
pub fn find_stability_generic<'a,
407+
AM: AttrMetaMethods,
408+
I: Iterator<&'a AM>>
409+
(mut attrs: I)
410+
-> Option<(Stability, &'a AM)> {
411+
for attr in attrs {
412+
let level = match attr.name().get() {
407413
"deprecated" => Deprecated,
408414
"experimental" => Experimental,
409415
"unstable" => Unstable,
@@ -413,14 +419,22 @@ pub fn find_stability<AM: AttrMetaMethods, It: Iterator<AM>>(mut metas: It)
413419
_ => continue // not a stability level
414420
};
415421

416-
return Some(Stability {
422+
return Some((Stability {
417423
level: level,
418-
text: m.value_str()
419-
});
424+
text: attr.value_str()
425+
}, attr));
420426
}
421427
None
422428
}
423429

430+
/// Find the first stability attribute. `None` if none exists.
431+
pub fn find_stability(attrs: &[Attribute]) -> Option<Stability> {
432+
find_stability_generic(attrs.iter()).map(|(s, attr)| {
433+
mark_used(attr);
434+
s
435+
})
436+
}
437+
424438
pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) {
425439
let mut set = HashSet::new();
426440
for meta in metas.iter() {
@@ -447,11 +461,13 @@ pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) {
447461
* present (before fields, if any) with that type; reprensentation
448462
* optimizations which would remove it will not be done.
449463
*/
450-
pub fn find_repr_attr(diagnostic: &SpanHandler, attr: @ast::MetaItem, acc: ReprAttr)
464+
pub fn find_repr_attr(diagnostic: &SpanHandler, attr: &Attribute, acc: ReprAttr)
451465
-> ReprAttr {
452466
let mut acc = acc;
453-
match attr.node {
467+
info!("{}", ::print::pprust::attribute_to_str(attr));
468+
match attr.node.value.node {
454469
ast::MetaList(ref s, ref items) if s.equiv(&("repr")) => {
470+
mark_used(attr);
455471
for item in items.iter() {
456472
match item.node {
457473
ast::MetaWord(ref word) => {

src/libsyntax/ext/expand.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ pub fn expand_item(it: @ast::Item, fld: &mut MacroExpander)
265265

266266
match fld.extsbox.find(&intern(mname.get())) {
267267
Some(&ItemDecorator(dec_fn)) => {
268+
attr::mark_used(attr);
269+
268270
fld.cx.bt_push(ExpnInfo {
269271
call_site: attr.span,
270272
callee: NameAndSpan {
@@ -336,6 +338,7 @@ fn expand_item_modifiers(mut it: @ast::Item, fld: &mut MacroExpander)
336338

337339
match fld.extsbox.find(&intern(mname.get())) {
338340
Some(&ItemModifier(dec_fn)) => {
341+
attr::mark_used(attr);
339342
fld.cx.bt_push(ExpnInfo {
340343
call_site: attr.span,
341344
callee: NameAndSpan {

0 commit comments

Comments
 (0)