about summary refs log tree commit diff
path: root/src/libsyntax/ext
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-05-24 17:21:20 -0700
committerbors <bors@rust-lang.org>2014-05-24 17:21:20 -0700
commit07563be6ebe081c8f6666a7b6eb68d8e32774f2f (patch)
tree30b4848c49319e66eaf68b86081f6fdf5c99455b /src/libsyntax/ext
parent6304a27b80f3923a8ffc009418c302aa8b06fb93 (diff)
parent334799326486e46b67c5405ba9584a26878988a4 (diff)
downloadrust-07563be6ebe081c8f6666a7b6eb68d8e32774f2f.tar.gz
rust-07563be6ebe081c8f6666a7b6eb68d8e32774f2f.zip
auto merge of #14373 : sfackler/rust/unused-attr, r=huonw
The compiler now tracks which attributes were actually looked at during the compilation process and warns for those that were unused.

Some things of note:

* The tracking is done via thread locals, as it made the implementation more straightforward. Note that this shouldn't hamper any future parallelization as each task can have its own thread local state which can be merged for the lint pass. If there are serious objections to this, I can restructure things to explicitly pass the state around.
* There are a number of attributes that have to be special-cased and globally whitelisted. This happens for four reasons:
  * The `doc` and `automatically_derived` attributes are used by rustdoc, but not by the compiler.
  * The crate-level attributes `license`, `desc` and `comment` aren't currently used by anything.
  * Stability attributes as well as `must_use` are checked only when the tagged item is used, so we can't guarantee that the compiler's looked at them.
  * 12 attributes are used only in trans, which happens after the lint pass.

#14300 is adding infrastructure to track lint state through trans, which this lint should also be able to use to handle the last case. For the other attributes, the right solution would probably involve a specific pass to mark uses that occur in the correct context. For example, a `doc` attribute attached to a match arm should generate a warning, but will not currently.

RFC: 0002-attribute-usage
Diffstat (limited to 'src/libsyntax/ext')
-rw-r--r--src/libsyntax/ext/build.rs2
-rw-r--r--src/libsyntax/ext/deriving/generic/mod.rs3
-rw-r--r--src/libsyntax/ext/expand.rs7
3 files changed, 11 insertions, 1 deletions
diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs
index 3c7415ae0e9..449feb3afbf 100644
--- a/src/libsyntax/ext/build.rs
+++ b/src/libsyntax/ext/build.rs
@@ -12,6 +12,7 @@ use abi;
 use ast::{P, Ident};
 use ast;
 use ast_util;
+use attr;
 use codemap::{Span, respan, Spanned, DUMMY_SP};
 use ext::base::ExtCtxt;
 use ext::quote::rt::*;
@@ -927,6 +928,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
 
     fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute {
         respan(sp, ast::Attribute_ {
+            id: attr::mk_attr_id(),
             style: ast::AttrOuter,
             value: mi,
             is_sugared_doc: false,
diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs
index 0875daddc0f..5f18193437e 100644
--- a/src/libsyntax/ext/deriving/generic/mod.rs
+++ b/src/libsyntax/ext/deriving/generic/mod.rs
@@ -182,6 +182,7 @@ use std::cell::RefCell;
 use ast;
 use ast::{P, EnumDef, Expr, Ident, Generics, StructDef};
 use ast_util;
+use attr;
 use attr::AttrMetaMethods;
 use ext::base::ExtCtxt;
 use ext::build::AstBuilder;
@@ -430,6 +431,8 @@ impl<'a> TraitDef<'a> {
             self.span,
             cx.meta_word(self.span,
                          InternedString::new("automatically_derived")));
+        // Just mark it now since we know that it'll end up used downstream
+        attr::mark_used(&attr);
         let opt_trait_ref = Some(trait_ref);
         let ident = ast_util::impl_pretty_name(&opt_trait_ref, self_type);
         cx.item(
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 989d0a463c3..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 {
@@ -474,7 +477,7 @@ pub fn expand_view_item(vi: &ast::ViewItem,
     match vi.node {
         ast::ViewItemExternCrate(..) => {
             let should_load = vi.attrs.iter().any(|attr| {
-                attr.name().get() == "phase" &&
+                attr.check_name("phase") &&
                     attr.meta_item_list().map_or(false, |phases| {
                         attr::contains_name(phases, "syntax")
                     })
@@ -972,6 +975,7 @@ mod test {
     use super::*;
     use ast;
     use ast::{Attribute_, AttrOuter, MetaWord};
+    use attr;
     use codemap;
     use codemap::Spanned;
     use ext::base::{CrateLoader, MacroCrate};
@@ -1103,6 +1107,7 @@ mod test {
         Spanned {
             span:codemap::DUMMY_SP,
             node: Attribute_ {
+                id: attr::mk_attr_id(),
                 style: AttrOuter,
                 value: @Spanned {
                     node: MetaWord(token::intern_and_get_ident(s)),