From e0648093d8f8cc4db3b309622b7efd95275a17d3 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 21 May 2014 00:05:45 -0700 Subject: [PATCH] Port more stuff to mark used attributes --- src/librustc/metadata/csearch.rs | 2 +- src/librustc/metadata/decoder.rs | 10 +---- src/librustc/middle/lint.rs | 13 +++---- src/librustc/middle/trans/base.rs | 6 +-- src/librustc/middle/ty.rs | 14 ++++--- src/librustdoc/html/render.rs | 4 +- src/libsyntax/attr.rs | 64 +++++++++++++++++++------------ src/libsyntax/ext/expand.rs | 3 ++ 8 files changed, 63 insertions(+), 53 deletions(-) diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs index f30e24a3151..d2b567395f0 100644 --- a/src/librustc/metadata/csearch.rs +++ b/src/librustc/metadata/csearch.rs @@ -176,7 +176,7 @@ pub fn get_static_methods_if_impl(cstore: &cstore::CStore, pub fn get_item_attrs(cstore: &cstore::CStore, def_id: ast::DefId, - f: |Vec<@ast::MetaItem> |) { + f: |Vec |) { let cdata = cstore.get_crate_data(def_id.krate); decoder::get_item_attrs(&*cdata, def_id.node, f) } diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 77c3d43bc09..54243ea6f1f 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -953,20 +953,14 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd, pub fn get_item_attrs(cdata: Cmd, orig_node_id: ast::NodeId, - f: |Vec<@ast::MetaItem> |) { + f: |Vec|) { // The attributes for a tuple struct are attached to the definition, not the ctor; // we assume that someone passing in a tuple struct ctor is actually wanting to // look at the definition let node_id = get_tuple_struct_definition_if_ctor(cdata, orig_node_id); let node_id = node_id.map(|x| x.node).unwrap_or(orig_node_id); let item = lookup_item(node_id, cdata.data()); - reader::tagged_docs(item, tag_attributes, |attributes| { - reader::tagged_docs(attributes, tag_attribute, |attribute| { - f(get_meta_items(attribute)); - true - }); - true - }); + f(get_attributes(item)); } fn struct_field_family_to_visibility(family: Family) -> ast::Visibility { diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 6b5b98376b6..5f7376396e4 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -64,7 +64,7 @@ use collections::SmallIntMap; use syntax::abi; use syntax::ast_map; use syntax::ast_util::IdVisitingOperation; -use syntax::attr::{AttrMetaMethods, AttributeMethods}; +use syntax::attr::AttrMetaMethods; use syntax::attr; use syntax::codemap::Span; use syntax::parse::token::InternedString; @@ -1148,8 +1148,7 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) { fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) { for attr in attrs.iter() { if !attr::is_used(attr) { - cx.span_lint(UnusedAttribute, attr.span, - format!("unused attribute {}", attr.name()).as_slice()); + cx.span_lint(UnusedAttribute, attr.span, "unused attribute"); } } } @@ -1654,9 +1653,7 @@ fn check_stability(cx: &Context, e: &ast::Expr) { let stability = if ast_util::is_local(id) { // this crate let s = cx.tcx.map.with_attrs(id.node, |attrs| { - attrs.map(|a| { - attr::find_stability(a.iter().map(|a| a.meta())) - }) + attrs.map(|a| attr::find_stability(a.as_slice())) }); match s { Some(s) => s, @@ -1672,9 +1669,9 @@ fn check_stability(cx: &Context, e: &ast::Expr) { let mut s = None; // run through all the attributes and take the first // stability one. - csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |meta_items| { + csearch::get_item_attrs(&cx.tcx.sess.cstore, id, |attrs| { if s.is_none() { - s = attr::find_stability(meta_items.move_iter()) + s = attr::find_stability(attrs.as_slice()) } }); s diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 4b769a5fdae..da28c3008dd 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -227,10 +227,8 @@ fn get_extern_rust_fn(ccx: &CrateContext, fn_ty: ty::t, name: &str, did: ast::De let f = decl_rust_fn(ccx, fn_ty, name); - csearch::get_item_attrs(&ccx.sess().cstore, did, |meta_items| { - set_llvm_fn_attrs(meta_items.iter().map(|&x| { - attr::mk_attr_outer(attr::mk_attr_id(), x) - }).collect::>().as_slice(), f) + csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| { + set_llvm_fn_attrs(attrs.as_slice(), f) }); ccx.externs.borrow_mut().insert(name.to_strbuf(), f); diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 64619b636a3..9176b33331f 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3889,20 +3889,22 @@ pub fn lookup_trait_def(cx: &ctxt, did: ast::DefId) -> Rc { } } -/// Iterate over meta_items of a definition. +/// Iterate over attributes of a definition. // (This should really be an iterator, but that would require csearch and // decoder to use iterators instead of higher-order functions.) -pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool { +pub fn each_attr(tcx: &ctxt, did: DefId, f: |&ast::Attribute| -> bool) -> bool { if is_local(did) { let item = tcx.map.expect_item(did.node); - item.attrs.iter().advance(|attr| f(attr.node.value)) + item.attrs.iter().advance(|attr| f(attr)) } else { + info!("getting foreign attrs"); let mut cont = true; - csearch::get_item_attrs(&tcx.sess.cstore, did, |meta_items| { + csearch::get_item_attrs(&tcx.sess.cstore, did, |attrs| { if cont { - cont = meta_items.iter().advance(|ptrptr| f(*ptrptr)); + cont = attrs.iter().advance(|attr| f(attr)); } }); + info!("done"); cont } } @@ -3911,7 +3913,7 @@ pub fn each_attr(tcx: &ctxt, did: DefId, f: |@ast::MetaItem| -> bool) -> bool { pub fn has_attr(tcx: &ctxt, did: DefId, attr: &str) -> bool { let mut found = false; each_attr(tcx, did, |item| { - if item.name().equiv(&attr) { + if item.check_name(attr) { found = true; false } else { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 8744553955a..b09ac8f94af 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1091,8 +1091,8 @@ impl<'a> fmt::Show for Item<'a> { shortty(self.item), self.item.name.get_ref().as_slice())); // Write stability attributes - match attr::find_stability(self.item.attrs.iter()) { - Some(ref stability) => { + match attr::find_stability_generic(self.item.attrs.iter()) { + Some((ref stability, _)) => { try!(write!(fmt, "{lvl}", lvl = stability.level.to_str(), diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index ac5792febbb..bbd333163b5 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -238,14 +238,14 @@ pub fn contains_name(metas: &[AM], name: &str) -> bool { debug!("attr::contains_name (name={})", name); metas.iter().any(|item| { debug!(" testing: {}", item.name()); - item.name().equiv(&name) + item.check_name(name) }) } pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option { attrs.iter() - .find(|at| at.name().equiv(&name)) + .find(|at| at.check_name(name)) .and_then(|at| at.value_str()) } @@ -253,7 +253,7 @@ pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str) -> Option { items.iter() .rev() - .find(|mi| mi.name().equiv(&name)) + .find(|mi| mi.check_name(name)) .and_then(|i| i.value_str()) } @@ -289,7 +289,7 @@ pub fn sort_meta_items(items: &[@MetaItem]) -> Vec<@MetaItem> { */ pub fn find_linkage_metas(attrs: &[Attribute]) -> Vec<@MetaItem> { let mut result = Vec::new(); - for attr in attrs.iter().filter(|at| at.name().equiv(&("link"))) { + for attr in attrs.iter().filter(|at| at.check_name("link")) { match attr.meta().node { MetaList(_, ref items) => result.push_all(items.as_slice()), _ => () @@ -318,17 +318,21 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr { // FIXME (#2809)---validate the usage of #[inline] and #[inline] attrs.iter().fold(InlineNone, |ia,attr| { match attr.node.value.node { - MetaWord(ref n) if n.equiv(&("inline")) => InlineHint, - MetaList(ref n, ref items) if n.equiv(&("inline")) => { - if contains_name(items.as_slice(), "always") { - InlineAlways - } else if contains_name(items.as_slice(), "never") { - InlineNever - } else { + MetaWord(ref n) if n.equiv(&("inline")) => { + mark_used(attr); InlineHint } - } - _ => ia + MetaList(ref n, ref items) if n.equiv(&("inline")) => { + mark_used(attr); + if contains_name(items.as_slice(), "always") { + InlineAlways + } else if contains_name(items.as_slice(), "never") { + InlineNever + } else { + InlineHint + } + } + _ => ia } }) } @@ -348,7 +352,7 @@ pub fn test_cfg> // this doesn't work. let some_cfg_matches = metas.any(|mi| { debug!("testing name: {}", mi.name()); - if mi.name().equiv(&("cfg")) { // it is a #[cfg()] attribute + if mi.check_name("cfg") { // it is a #[cfg()] attribute debug!("is cfg"); no_cfgs = false; // only #[cfg(...)] ones are understood. @@ -399,11 +403,13 @@ pub enum StabilityLevel { Locked } -/// Find the first stability attribute. `None` if none exists. -pub fn find_stability>(mut metas: It) - -> Option { - for m in metas { - let level = match m.name().get() { +pub fn find_stability_generic<'a, + AM: AttrMetaMethods, + I: Iterator<&'a AM>> + (mut attrs: I) + -> Option<(Stability, &'a AM)> { + for attr in attrs { + let level = match attr.name().get() { "deprecated" => Deprecated, "experimental" => Experimental, "unstable" => Unstable, @@ -413,14 +419,22 @@ pub fn find_stability>(mut metas: It) _ => continue // not a stability level }; - return Some(Stability { + return Some((Stability { level: level, - text: m.value_str() - }); + text: attr.value_str() + }, attr)); } None } +/// Find the first stability attribute. `None` if none exists. +pub fn find_stability(attrs: &[Attribute]) -> Option { + find_stability_generic(attrs.iter()).map(|(s, attr)| { + mark_used(attr); + s + }) +} + pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) { let mut set = HashSet::new(); for meta in metas.iter() { @@ -447,11 +461,13 @@ pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) { * present (before fields, if any) with that type; reprensentation * optimizations which would remove it will not be done. */ -pub fn find_repr_attr(diagnostic: &SpanHandler, attr: @ast::MetaItem, acc: ReprAttr) +pub fn find_repr_attr(diagnostic: &SpanHandler, attr: &Attribute, acc: ReprAttr) -> ReprAttr { let mut acc = acc; - match attr.node { + info!("{}", ::print::pprust::attribute_to_str(attr)); + match attr.node.value.node { ast::MetaList(ref s, ref items) if s.equiv(&("repr")) => { + mark_used(attr); for item in items.iter() { match item.node { ast::MetaWord(ref word) => { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 64b7e1c28c1..658e4bafbe2 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -265,6 +265,8 @@ pub fn expand_item(it: @ast::Item, fld: &mut MacroExpander) match fld.extsbox.find(&intern(mname.get())) { Some(&ItemDecorator(dec_fn)) => { + attr::mark_used(attr); + fld.cx.bt_push(ExpnInfo { call_site: attr.span, callee: NameAndSpan { @@ -336,6 +338,7 @@ fn expand_item_modifiers(mut it: @ast::Item, fld: &mut MacroExpander) match fld.extsbox.find(&intern(mname.get())) { Some(&ItemModifier(dec_fn)) => { + attr::mark_used(attr); fld.cx.bt_push(ExpnInfo { call_site: attr.span, callee: NameAndSpan {