Skip to content

Commit d377cf5

Browse files
Rename KNOWN_ATTRS to BUILT_ATTRS, and create KNOWN_ATTRS
KNOWN_ATTRIBUTES should really be named BUILT_ATTRIBUTES, while KNOWN_ATTRIBUTES should be used to mark attributes as known, similar to USED_ATTRIBUTES.
1 parent cae6ab1 commit d377cf5

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/librustc_lint/unused.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
1919

2020
use syntax::ast;
2121
use syntax::attr;
22-
use syntax::feature_gate::{KNOWN_ATTRIBUTES, AttributeType};
22+
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
2323
use syntax::parse::token::keywords;
2424
use syntax::ptr::P;
2525
use syntax_pos::Span;
@@ -245,7 +245,7 @@ impl LateLintPass for UnusedAttributes {
245245
debug!("checking attribute: {:?}", attr);
246246

247247
// Note that check_name() marks the attribute as used if it matches.
248-
for &(ref name, ty, _) in KNOWN_ATTRIBUTES {
248+
for &(ref name, ty, _) in BUILTIN_ATTRIBUTES {
249249
match ty {
250250
AttributeType::Whitelisted if attr.check_name(name) => {
251251
debug!("{:?} is Whitelisted", name);
@@ -267,7 +267,7 @@ impl LateLintPass for UnusedAttributes {
267267
debug!("Emitting warning for: {:?}", attr);
268268
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
269269
// Is it a builtin attribute that must be used at the crate level?
270-
let known_crate = KNOWN_ATTRIBUTES.iter()
270+
let known_crate = BUILTIN_ATTRIBUTES.iter()
271271
.find(|&&(name, ty, _)| attr.name() == name && ty == AttributeType::CrateLevel)
272272
.is_some();
273273

src/libsyntax/attr.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use std::cell::{RefCell, Cell};
3232
use std::collections::HashSet;
3333

3434
thread_local! {
35-
static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new())
35+
static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
36+
static KNOWN_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
3637
}
3738

3839
enum AttrError {
@@ -81,6 +82,29 @@ pub fn is_used(attr: &Attribute) -> bool {
8182
})
8283
}
8384

85+
pub fn mark_known(attr: &Attribute) {
86+
debug!("Marking {:?} as known.", attr);
87+
let AttrId(id) = attr.node.id;
88+
KNOWN_ATTRS.with(|slot| {
89+
let idx = (id / 64) as usize;
90+
let shift = id % 64;
91+
if slot.borrow().len() <= idx {
92+
slot.borrow_mut().resize(idx + 1, 0);
93+
}
94+
slot.borrow_mut()[idx] |= 1 << shift;
95+
});
96+
}
97+
98+
pub fn is_known(attr: &Attribute) -> bool {
99+
let AttrId(id) = attr.node.id;
100+
KNOWN_ATTRS.with(|slot| {
101+
let idx = (id / 64) as usize;
102+
let shift = id % 64;
103+
slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
104+
.unwrap_or(false)
105+
})
106+
}
107+
84108
impl NestedMetaItem {
85109
/// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem.
86110
pub fn meta_item(&self) -> Option<&P<MetaItem>> {

src/libsyntax/feature_gate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,11 @@ macro_rules! cfg_fn {
417417
}
418418

419419
pub fn deprecated_attributes() -> Vec<&'static (&'static str, AttributeType, AttributeGate)> {
420-
KNOWN_ATTRIBUTES.iter().filter(|a| a.2.is_deprecated()).collect()
420+
BUILTIN_ATTRIBUTES.iter().filter(|a| a.2.is_deprecated()).collect()
421421
}
422422

423423
// Attributes that have a special meaning to rustc or rustdoc
424-
pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGate)] = &[
424+
pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGate)] = &[
425425
// Normal attributes
426426

427427
("warn", Normal, Ungated),
@@ -790,12 +790,12 @@ impl<'a> Context<'a> {
790790
fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
791791
debug!("check_attribute(attr = {:?})", attr);
792792
let name = &*attr.name();
793-
for &(n, ty, ref gateage) in KNOWN_ATTRIBUTES {
793+
for &(n, ty, ref gateage) in BUILTIN_ATTRIBUTES {
794794
if n == name {
795795
if let &Gated(_, ref name, ref desc, ref has_feature) = gateage {
796796
gate_feature_fn!(self, has_feature, attr.span, name, desc);
797797
}
798-
debug!("check_attribute: {:?} is known, {:?}, {:?}", name, ty, gateage);
798+
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", name, ty, gateage);
799799
return;
800800
}
801801
}
@@ -815,6 +815,8 @@ impl<'a> Context<'a> {
815815
are reserved for internal compiler diagnostics");
816816
} else if name.starts_with("derive_") {
817817
gate_feature!(self, custom_derive, attr.span, EXPLAIN_DERIVE_UNDERSCORE);
818+
} else if attr::is_known(attr) {
819+
debug!("check_attribute: {:?} is known", name);
818820
} else {
819821
// Only run the custom attribute lint during regular
820822
// feature gate checking. Macro gating runs

0 commit comments

Comments
 (0)