Skip to content

Commit c305473

Browse files
committed
Add AttrId to Attribute_
1 parent 6304a27 commit c305473

File tree

23 files changed

+94
-30
lines changed

23 files changed

+94
-30
lines changed

src/librustc/back/svh.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ impl Svh {
9191
// types and then use hash_content. But, since all crate
9292
// attributes should appear near beginning of the file, it is
9393
// not such a big deal to be sensitive to their spans for now.
94-
krate.attrs.hash(&mut state);
94+
//
95+
// We hash only the MetaItems instead of the entire Attribute
96+
// to avoid hashing the AttrId
97+
for attr in krate.attrs.iter() {
98+
attr.node.value.hash(&mut state);
99+
}
95100

96101
let hash = state.result();
97102
return Svh {

src/librustc/front/std_inject.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
7878
with_version("std"),
7979
ast::DUMMY_NODE_ID),
8080
attrs: vec!(
81-
attr::mk_attr_outer(attr::mk_list_item(
81+
attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_list_item(
8282
InternedString::new("phase"),
8383
vec!(
8484
attr::mk_word_item(InternedString::new("syntax")),
@@ -110,7 +110,8 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
110110
// Add it during the prelude injection instead.
111111

112112
// Add #![feature(phase)] here, because we use #[phase] on extern crate std.
113-
let feat_phase_attr = attr::mk_attr_inner(attr::mk_list_item(
113+
let feat_phase_attr = attr::mk_attr_inner(attr::mk_attr_id(),
114+
attr::mk_list_item(
114115
InternedString::new("feature"),
115116
vec![attr::mk_word_item(InternedString::new("phase"))],
116117
));
@@ -138,15 +139,17 @@ impl<'a> fold::Folder for PreludeInjector<'a> {
138139
// This must happen here and not in StandardLibraryInjector because this
139140
// fold happens second.
140141

141-
let no_std_attr = attr::mk_attr_inner(attr::mk_word_item(InternedString::new("no_std")));
142+
let no_std_attr = attr::mk_attr_inner(attr::mk_attr_id(),
143+
attr::mk_word_item(InternedString::new("no_std")));
142144
krate.attrs.push(no_std_attr);
143145

144146
if !no_prelude(krate.attrs.as_slice()) {
145147
// only add `use std::prelude::*;` if there wasn't a
146148
// `#![no_implicit_prelude]` at the crate level.
147149

148150
// fold_mod() will insert glob path.
149-
let globs_attr = attr::mk_attr_inner(attr::mk_list_item(
151+
let globs_attr = attr::mk_attr_inner(attr::mk_attr_id(),
152+
attr::mk_list_item(
150153
InternedString::new("feature"),
151154
vec!(
152155
attr::mk_word_item(InternedString::new("globs")),

src/librustc/front/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::Item {
341341
// This attribute tells resolve to let us call unexported functions
342342
let resolve_unexported_str = InternedString::new("!resolve_unexported");
343343
let resolve_unexported_attr =
344-
attr::mk_attr_inner(attr::mk_word_item(resolve_unexported_str));
344+
attr::mk_attr_inner(attr::mk_attr_id(),
345+
attr::mk_word_item(resolve_unexported_str));
345346

346347
let item = ast::Item {
347348
ident: token::str_to_ident("__test"),

src/librustc/metadata/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ fn get_attributes(md: ebml::Doc) -> Vec<ast::Attribute> {
10561056
attrs.push(
10571057
codemap::Spanned {
10581058
node: ast::Attribute_ {
1059+
id: attr::mk_attr_id(),
10591060
style: ast::AttrOuter,
10601061
value: meta_item,
10611062
is_sugared_doc: false,

src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ fn synthesize_crate_attrs(ecx: &EncodeContext,
14361436
fn synthesize_crateid_attr(ecx: &EncodeContext) -> Attribute {
14371437
assert!(!ecx.link_meta.crateid.name.is_empty());
14381438

1439-
attr::mk_attr_inner(
1439+
attr::mk_attr_inner(attr::mk_attr_id(),
14401440
attr::mk_name_value_item_str(
14411441
InternedString::new("crate_id"),
14421442
token::intern_and_get_ident(ecx.link_meta

src/librustc/middle/trans/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ fn get_extern_rust_fn(ccx: &CrateContext, fn_ty: ty::t, name: &str, did: ast::De
228228
let f = decl_rust_fn(ccx, fn_ty, name);
229229

230230
csearch::get_item_attrs(&ccx.sess().cstore, did, |meta_items| {
231-
set_llvm_fn_attrs(meta_items.iter().map(|&x| attr::mk_attr_outer(x))
232-
.collect::<Vec<_>>().as_slice(), f)
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)
233234
});
234235

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

src/libsyntax/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,13 @@ pub enum AttrStyle {
10241024
AttrInner,
10251025
}
10261026

1027+
#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
1028+
pub struct AttrId(pub uint);
1029+
10271030
// doc-comments are promoted to attributes that have is_sugared_doc = true
10281031
#[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
10291032
pub struct Attribute_ {
1033+
pub id: AttrId,
10301034
pub style: AttrStyle,
10311035
pub value: @MetaItem,
10321036
pub is_sugared_doc: bool,

src/libsyntax/attr.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Functions dealing with attributes and meta items
1212

1313
use ast;
14-
use ast::{Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
14+
use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
1515
use codemap::{Span, Spanned, spanned, dummy_spanned};
1616
use codemap::BytePos;
1717
use diagnostic::SpanHandler;
@@ -22,6 +22,18 @@ use crateid::CrateId;
2222

2323
use collections::HashSet;
2424

25+
local_data_key!(used_attrs: HashSet<AttrId>)
26+
27+
pub fn mark_used(attr: &Attribute) {
28+
let mut used = used_attrs.replace(None).unwrap_or_else(|| HashSet::new());
29+
used.insert(attr.node.id);
30+
used_attrs.replace(Some(used));
31+
}
32+
33+
pub fn is_used(attr: &Attribute) -> bool {
34+
used_attrs.get().map_or(false, |used| used.contains(&attr.node.id))
35+
}
36+
2537
pub trait AttrMetaMethods {
2638
// This could be changed to `fn check_name(&self, name: InternedString) ->
2739
// bool` which would facilitate a side table recording which
@@ -127,9 +139,9 @@ impl AttributeMethods for Attribute {
127139
token::intern_and_get_ident(strip_doc_comment_decoration(
128140
comment.get()).as_slice()));
129141
if self.node.style == ast::AttrOuter {
130-
mk_attr_outer(meta)
142+
mk_attr_outer(self.node.id, meta)
131143
} else {
132-
mk_attr_inner(meta)
144+
mk_attr_inner(self.node.id, meta)
133145
}
134146
} else {
135147
*self
@@ -158,29 +170,41 @@ pub fn mk_word_item(name: InternedString) -> @MetaItem {
158170
@dummy_spanned(MetaWord(name))
159171
}
160172

173+
local_data_key!(next_attr_id: uint)
174+
175+
pub fn mk_attr_id() -> AttrId {
176+
let id = next_attr_id.replace(None).unwrap_or(0);
177+
next_attr_id.replace(Some(id + 1));
178+
AttrId(id)
179+
}
180+
161181
/// Returns an inner attribute with the given value.
162-
pub fn mk_attr_inner(item: @MetaItem) -> Attribute {
182+
pub fn mk_attr_inner(id: AttrId, item: @MetaItem) -> Attribute {
163183
dummy_spanned(Attribute_ {
184+
id: id,
164185
style: ast::AttrInner,
165186
value: item,
166187
is_sugared_doc: false,
167188
})
168189
}
169190

170191
/// Returns an outer attribute with the given value.
171-
pub fn mk_attr_outer(item: @MetaItem) -> Attribute {
192+
pub fn mk_attr_outer(id: AttrId, item: @MetaItem) -> Attribute {
172193
dummy_spanned(Attribute_ {
194+
id: id,
173195
style: ast::AttrOuter,
174196
value: item,
175197
is_sugared_doc: false,
176198
})
177199
}
178200

179-
pub fn mk_sugared_doc_attr(text: InternedString, lo: BytePos, hi: BytePos)
201+
pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos,
202+
hi: BytePos)
180203
-> Attribute {
181204
let style = doc_comment_style(text.get());
182205
let lit = spanned(lo, hi, ast::LitStr(text, ast::CookedStr));
183206
let attr = Attribute_ {
207+
id: id,
184208
style: style,
185209
value: @spanned(lo, hi, MetaNameValue(InternedString::new("doc"),
186210
lit)),

src/libsyntax/ext/build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub trait AstBuilder {
231231
generics: Generics) -> @ast::Item;
232232
fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> @ast::Item;
233233

234-
fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute;
234+
fn attribute(&self, id: AttrId, sp: Span, mi: @ast::MetaItem) -> ast::Attribute;
235235

236236
fn meta_word(&self, sp: Span, w: InternedString) -> @ast::MetaItem;
237237
fn meta_list(&self,
@@ -925,8 +925,10 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
925925
self.item_ty_poly(span, name, ty, ast_util::empty_generics())
926926
}
927927

928-
fn attribute(&self, sp: Span, mi: @ast::MetaItem) -> ast::Attribute {
928+
fn attribute(&self, id: ast::AttrId, sp: Span, mi: @ast::MetaItem)
929+
-> ast::Attribute {
929930
respan(sp, ast::Attribute_ {
931+
id: id,
930932
style: ast::AttrOuter,
931933
value: mi,
932934
is_sugared_doc: false,

src/libsyntax/ext/deriving/clone.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use attr;
1112
use ast::{MetaItem, Item, Expr};
1213
use codemap::Span;
1314
use ext::base::ExtCtxt;
@@ -21,7 +22,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
2122
item: @Item,
2223
push: |@Item|) {
2324
let inline = cx.meta_word(span, InternedString::new("inline"));
24-
let attrs = vec!(cx.attribute(span, inline));
25+
let attrs = vec!(cx.attribute(attr::mk_attr_id(), span, inline));
2526
let trait_def = TraitDef {
2627
span: span,
2728
attributes: Vec::new(),

0 commit comments

Comments
 (0)