From c305473d3c60d5b4590ef8c715468f718a7aad8f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 20 May 2014 00:07:24 -0700 Subject: Add AttrId to Attribute_ --- src/libsyntax/parse/attr.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 89d1b8f9342..e86dcb3d311 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use attr; use ast; use codemap::{spanned, Spanned, mk_sp, Span}; use parse::common::*; //resolve bug? @@ -39,6 +40,7 @@ impl<'a> ParserAttr for Parser<'a> { } token::DOC_COMMENT(s) => { let attr = ::attr::mk_sugared_doc_attr( + attr::mk_attr_id(), self.id_to_interned_str(s), self.span.lo, self.span.hi @@ -101,6 +103,7 @@ impl<'a> ParserAttr for Parser<'a> { return Spanned { span: span, node: ast::Attribute_ { + id: attr::mk_attr_id(), style: style, value: value, is_sugared_doc: false @@ -132,7 +135,10 @@ impl<'a> ParserAttr for Parser<'a> { // we need to get the position of this token before we bump. let Span { lo, hi, .. } = self.span; self.bump(); - ::attr::mk_sugared_doc_attr(self.id_to_interned_str(s), lo, hi) + ::attr::mk_sugared_doc_attr(attr::mk_attr_id(), + self.id_to_interned_str(s), + lo, + hi) } _ => { break; -- cgit 1.4.1-3-g733a5 From 334799326486e46b67c5405ba9584a26878988a4 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 23 May 2014 08:39:26 -0700 Subject: Changes from feedback --- src/librustc/middle/lint.rs | 69 ++++++++++++++++-------------- src/libsyntax/attr.rs | 19 ++++---- src/libsyntax/ext/build.rs | 8 ++-- src/libsyntax/ext/deriving/clone.rs | 3 +- src/libsyntax/ext/deriving/cmp/eq.rs | 3 +- src/libsyntax/ext/deriving/cmp/ord.rs | 3 +- src/libsyntax/ext/deriving/cmp/totaleq.rs | 5 +-- src/libsyntax/ext/deriving/cmp/totalord.rs | 3 +- src/libsyntax/ext/deriving/default.rs | 3 +- src/libsyntax/ext/deriving/generic/mod.rs | 3 +- src/libsyntax/ext/deriving/hash.rs | 3 +- src/libsyntax/ext/deriving/primitive.rs | 3 +- src/libsyntax/ext/deriving/zero.rs | 3 +- src/libsyntax/ext/format.rs | 7 +-- src/libsyntax/parse/attr.rs | 8 ++-- 15 files changed, 70 insertions(+), 73 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 25a0f151090..2b1e28548f9 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -1146,39 +1146,46 @@ fn check_attrs_usage(cx: &Context, attrs: &[ast::Attribute]) { } fn check_unused_attribute(cx: &Context, attrs: &[ast::Attribute]) { - for attr in attrs.iter() { - // whitelist docs since rustdoc looks at them - attr.check_name("automatically_derived"); - attr.check_name("doc"); - - // these are processed in trans, which happens after the lint pass - attr.check_name("address_insignificant"); - attr.check_name("cold"); - attr.check_name("inline"); - attr.check_name("link"); - attr.check_name("link_name"); - attr.check_name("link_section"); - attr.check_name("no_builtins"); - attr.check_name("no_mangle"); - attr.check_name("no_split_stack"); - attr.check_name("packed"); - attr.check_name("static_assert"); - attr.check_name("thread_local"); + static ATTRIBUTE_WHITELIST: &'static [&'static str] = &'static [ + // FIXME: #14408 whitelist docs since rustdoc looks at them + "doc", + + // FIXME: #14406 these are processed in trans, which happens after the + // lint pass + "address_insignificant", + "cold", + "inline", + "link", + "link_name", + "link_section", + "no_builtins", + "no_mangle", + "no_split_stack", + "packed", + "static_assert", + "thread_local", // not used anywhere (!?) but apparently we want to keep them around - attr.check_name("comment"); - attr.check_name("desc"); - attr.check_name("license"); - - // these are only looked at on-demand so we can't guarantee they'll have - // already been checked - attr.check_name("deprecated"); - attr.check_name("experimental"); - attr.check_name("frozen"); - attr.check_name("locked"); - attr.check_name("must_use"); - attr.check_name("stable"); - attr.check_name("unstable"); + "comment", + "desc", + "license", + + // FIXME: #14407 these are only looked at on-demand so we can't + // guarantee they'll have already been checked + "deprecated", + "experimental", + "frozen", + "locked", + "must_use", + "stable", + "unstable", + ]; + for attr in attrs.iter() { + for &name in ATTRIBUTE_WHITELIST.iter() { + if attr.check_name(name) { + break; + } + } if !attr::is_used(attr) { cx.span_lint(UnusedAttribute, attr.span, "unused attribute"); diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 1d43ac898f1..527e851ae35 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -21,17 +21,20 @@ use parse::token; use crateid::CrateId; use collections::HashSet; +use collections::bitv::BitvSet; -local_data_key!(used_attrs: HashSet) +local_data_key!(used_attrs: BitvSet) pub fn mark_used(attr: &Attribute) { - let mut used = used_attrs.replace(None).unwrap_or_else(|| HashSet::new()); - used.insert(attr.node.id); + let mut used = used_attrs.replace(None).unwrap_or_else(|| BitvSet::new()); + let AttrId(id) = attr.node.id; + used.insert(id); used_attrs.replace(Some(used)); } pub fn is_used(attr: &Attribute) -> bool { - used_attrs.get().map_or(false, |used| used.contains(&attr.node.id)) + let AttrId(id) = attr.node.id; + used_attrs.get().map_or(false, |used| used.contains(&id)) } pub trait AttrMetaMethods { @@ -60,12 +63,11 @@ pub trait AttrMetaMethods { impl AttrMetaMethods for Attribute { fn check_name(&self, name: &str) -> bool { - if name == self.name().get() { + let matches = name == self.name().get(); + if matches { mark_used(self); - true - } else { - false } + matches } fn name(&self) -> InternedString { self.meta().name() } fn value_str(&self) -> Option { @@ -465,7 +467,6 @@ pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[@MetaItem]) { pub fn find_repr_attr(diagnostic: &SpanHandler, attr: &Attribute, acc: ReprAttr) -> ReprAttr { let mut acc = acc; - 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); diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 44c177d19ca..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::*; @@ -231,7 +232,7 @@ pub trait AstBuilder { generics: Generics) -> @ast::Item; fn item_ty(&self, span: Span, name: Ident, ty: P) -> @ast::Item; - fn attribute(&self, id: AttrId, sp: Span, mi: @ast::MetaItem) -> ast::Attribute; + fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute; fn meta_word(&self, sp: Span, w: InternedString) -> @ast::MetaItem; fn meta_list(&self, @@ -925,10 +926,9 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.item_ty_poly(span, name, ty, ast_util::empty_generics()) } - fn attribute(&self, id: ast::AttrId, sp: Span, mi: @ast::MetaItem) - -> ast::Attribute { + fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute { respan(sp, ast::Attribute_ { - id: id, + id: attr::mk_attr_id(), style: ast::AttrOuter, value: mi, is_sugared_doc: false, diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index 73bfe9c27b6..89c94891b33 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use attr; use ast::{MetaItem, Item, Expr}; use codemap::Span; use ext::base::ExtCtxt; @@ -22,7 +21,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index 31d6fcb9d6f..92b3788c247 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -9,7 +9,6 @@ // except according to those terms. use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -35,7 +34,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt, macro_rules! md ( ($name:expr, $f:ident) => { { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); MethodDef { name: $name, generics: LifetimeBounds::empty(), diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index 3d79de4feb1..dd2f90cfa5f 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -10,7 +10,6 @@ use ast; use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -25,7 +24,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, macro_rules! md ( ($name:expr, $op:expr, $equal:expr) => { { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); MethodDef { name: $name, generics: LifetimeBounds::empty(), diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index 42bbf8e415a..b76caccffec 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -9,7 +9,6 @@ // except according to those terms. use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -38,8 +37,8 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt, let inline = cx.meta_word(span, InternedString::new("inline")); let hidden = cx.meta_word(span, InternedString::new("hidden")); let doc = cx.meta_list(span, InternedString::new("doc"), vec!(hidden)); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline), - cx.attribute(attr::mk_attr_id(), span, doc)); + let attrs = vec!(cx.attribute(span, inline), + cx.attribute(span, doc)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs index 6413bdab344..3ca4f9e2862 100644 --- a/src/libsyntax/ext/deriving/cmp/totalord.rs +++ b/src/libsyntax/ext/deriving/cmp/totalord.rs @@ -10,7 +10,6 @@ use ast; use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -25,7 +24,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs index a0499a2cc1e..c225906ed2b 100644 --- a/src/libsyntax/ext/deriving/default.rs +++ b/src/libsyntax/ext/deriving/default.rs @@ -9,7 +9,6 @@ // except according to those terms. use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -22,7 +21,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 28595fecd89..5f18193437e 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -428,10 +428,11 @@ impl<'a> TraitDef<'a> { self_ty_params.into_vec()), None); let attr = cx.attribute( - attr::mk_attr_id(), 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/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index 8b368968f49..3e6b8d522d4 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -10,7 +10,6 @@ use ast; use ast::{MetaItem, Item, Expr, MutMutable}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -38,7 +37,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, Path::new(vec!("std", "hash", "sip", "SipState"))) }; let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let hash_trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs index e3621b51c4d..5066a395b41 100644 --- a/src/libsyntax/ext/deriving/primitive.rs +++ b/src/libsyntax/ext/deriving/primitive.rs @@ -10,7 +10,6 @@ use ast::{MetaItem, Item, Expr}; use ast; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -23,7 +22,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs index c60cdab9099..449851dd3ea 100644 --- a/src/libsyntax/ext/deriving/zero.rs +++ b/src/libsyntax/ext/deriving/zero.rs @@ -9,7 +9,6 @@ // except according to those terms. use ast::{MetaItem, Item, Expr}; -use attr; use codemap::Span; use ext::base::ExtCtxt; use ext::build::AstBuilder; @@ -22,7 +21,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt, item: @Item, push: |@Item|) { let inline = cx.meta_word(span, InternedString::new("inline")); - let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline)); + let attrs = vec!(cx.attribute(span, inline)); let trait_def = TraitDef { span: span, attributes: Vec::new(), diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index c3b3c4eed57..ad4b798cfe5 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -10,7 +10,6 @@ use ast; use ast::P; -use attr; use codemap::{Span, respan}; use ext::base::*; use ext::base; @@ -383,8 +382,7 @@ impl<'a, 'b> Context<'a, 'b> { .meta_word(self.fmtsp, InternedString::new( "address_insignificant")); - let unnamed = self.ecx.attribute(attr::mk_attr_id(), self.fmtsp, - unnamed); + let unnamed = self.ecx.attribute(self.fmtsp, unnamed); // Do not warn format string as dead code let dead_code = self.ecx.meta_word(self.fmtsp, @@ -392,8 +390,7 @@ impl<'a, 'b> Context<'a, 'b> { let allow_dead_code = self.ecx.meta_list(self.fmtsp, InternedString::new("allow"), vec!(dead_code)); - let allow_dead_code = self.ecx.attribute(attr::mk_attr_id(), self.fmtsp, - allow_dead_code); + let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code); return vec!(unnamed, allow_dead_code); } diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index e86dcb3d311..9dcc0877fa4 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -135,10 +135,10 @@ impl<'a> ParserAttr for Parser<'a> { // we need to get the position of this token before we bump. let Span { lo, hi, .. } = self.span; self.bump(); - ::attr::mk_sugared_doc_attr(attr::mk_attr_id(), - self.id_to_interned_str(s), - lo, - hi) + attr::mk_sugared_doc_attr(attr::mk_attr_id(), + self.id_to_interned_str(s), + lo, + hi) } _ => { break; -- cgit 1.4.1-3-g733a5