Skip to content

Commit 9217fe0

Browse files
committed
Auto merge of #59850 - Zoxc:symbols-attrs, r=petrochenkov
Preallocate BUILTIN_ATTRIBUTES symbols Builds on #59655 r? @petrochenkov
2 parents fcf850f + cf3a256 commit 9217fe0

File tree

11 files changed

+837
-605
lines changed

11 files changed

+837
-605
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3356,9 +3356,11 @@ name = "syntax"
33563356
version = "0.0.0"
33573357
dependencies = [
33583358
"bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
3359+
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
33593360
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
33603361
"rustc_data_structures 0.0.0",
33613362
"rustc_errors 0.0.0",
3363+
"rustc_macros 0.1.0",
33623364
"rustc_target 0.0.0",
33633365
"scoped-tls 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
33643366
"serialize 0.0.0",

src/librustc_lint/builtin.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use syntax::edition::Edition;
4242
use syntax::feature_gate::{AttributeGate, AttributeTemplate, AttributeType};
4343
use syntax::feature_gate::{Stability, deprecated_attributes};
4444
use syntax_pos::{BytePos, Span, SyntaxContext};
45-
use syntax::symbol::keywords;
45+
use syntax::symbol::{Symbol, keywords};
4646
use syntax::errors::{Applicability, DiagnosticBuilder};
4747
use syntax::print::pprust::expr_to_string;
4848
use syntax::visit::FnKind;
@@ -653,7 +653,7 @@ impl EarlyLintPass for AnonymousParameters {
653653
pub struct DeprecatedAttr {
654654
// This is not free to compute, so we want to keep it around, rather than
655655
// compute it for every attribute.
656-
depr_attrs: Vec<&'static (&'static str, AttributeType, AttributeTemplate, AttributeGate)>,
656+
depr_attrs: Vec<&'static (Symbol, AttributeType, AttributeTemplate, AttributeGate)>,
657657
}
658658

659659
impl_lint_pass!(DeprecatedAttr => []);
@@ -668,9 +668,8 @@ impl DeprecatedAttr {
668668

669669
impl EarlyLintPass for DeprecatedAttr {
670670
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
671-
let name = attr.name_or_empty();
672671
for &&(n, _, _, ref g) in &self.depr_attrs {
673-
if name == n {
672+
if attr.ident().map(|ident| ident.name) == Some(n) {
674673
if let &AttributeGate::Gated(Stability::Deprecated(link, suggestion),
675674
ref name,
676675
ref reason,

src/librustc_lint/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ macro_rules! late_lint_passes {
118118
UnusedBrokenConst: UnusedBrokenConst,
119119

120120
// Uses attr::is_used which is untracked, can't be an incremental module pass.
121-
UnusedAttributes: UnusedAttributes,
121+
UnusedAttributes: UnusedAttributes::new(),
122122

123123
// Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
124124
UnstableFeatures: UnstableFeatures,

src/librustc_lint/unused.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ use rustc::hir::def_id::DefId;
33
use rustc::lint;
44
use rustc::ty;
55
use rustc::ty::adjustment;
6+
use rustc_data_structures::fx::FxHashMap;
67
use lint::{LateContext, EarlyContext, LintContext, LintArray};
78
use lint::{LintPass, EarlyLintPass, LateLintPass};
89

910
use syntax::ast;
1011
use syntax::attr;
1112
use syntax::errors::Applicability;
12-
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
13+
use syntax::feature_gate::{AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1314
use syntax::print::pprust;
1415
use syntax::symbol::keywords;
16+
use syntax::symbol::Symbol;
1517
use syntax::util::parser;
1618
use syntax_pos::Span;
1719

@@ -210,17 +212,32 @@ declare_lint! {
210212
"detects attributes that were not used by the compiler"
211213
}
212214

213-
declare_lint_pass!(UnusedAttributes => [UNUSED_ATTRIBUTES]);
215+
#[derive(Copy, Clone)]
216+
pub struct UnusedAttributes {
217+
builtin_attributes: &'static FxHashMap<Symbol, &'static BuiltinAttribute>,
218+
}
219+
220+
impl UnusedAttributes {
221+
pub fn new() -> Self {
222+
UnusedAttributes {
223+
builtin_attributes: &*BUILTIN_ATTRIBUTE_MAP,
224+
}
225+
}
226+
}
227+
228+
impl_lint_pass!(UnusedAttributes => [UNUSED_ATTRIBUTES]);
214229

215230
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
216231
fn check_attribute(&mut self, cx: &LateContext<'_, '_>, attr: &ast::Attribute) {
217232
debug!("checking attribute: {:?}", attr);
218-
// Note that check_name() marks the attribute as used if it matches.
219-
for &(name, ty, ..) in BUILTIN_ATTRIBUTES {
233+
234+
let attr_info = attr.ident().and_then(|ident| self.builtin_attributes.get(&ident.name));
235+
236+
if let Some(&&(name, ty, ..)) = attr_info {
220237
match ty {
221-
AttributeType::Whitelisted if attr.check_name(name) => {
238+
AttributeType::Whitelisted => {
222239
debug!("{:?} is Whitelisted", name);
223-
break;
240+
return;
224241
}
225242
_ => (),
226243
}
@@ -239,11 +256,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
239256
debug!("Emitting warning for: {:?}", attr);
240257
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
241258
// Is it a builtin attribute that must be used at the crate level?
242-
let known_crate = BUILTIN_ATTRIBUTES.iter()
243-
.find(|&&(builtin, ty, ..)| {
244-
name == builtin && ty == AttributeType::CrateLevel
245-
})
246-
.is_some();
259+
let known_crate = attr_info.map(|&&(_, ty, ..)| {
260+
ty == AttributeType::CrateLevel
261+
}).unwrap_or(false);
247262

248263
// Has a plugin registered this attribute as one that must be used at
249264
// the crate level?

src/librustc_resolve/macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ impl<'a> Resolver<'a> {
360360

361361
let attr_candidates = BUILTIN_ATTRIBUTES
362362
.iter()
363-
.filter_map(|(name, _, _, gate)| {
364-
if name.starts_with("rustc_") && !features.rustc_attrs {
363+
.filter_map(|&(name, _, _, ref gate)| {
364+
if name.as_str().starts_with("rustc_") && !features.rustc_attrs {
365365
return None;
366366
}
367367

@@ -376,7 +376,6 @@ impl<'a> Resolver<'a> {
376376
_ => None,
377377
}
378378
})
379-
.map(|name| Symbol::intern(name))
380379
.chain(
381380
// Add built-in macro attributes as well.
382381
self.builtin_macros.iter().filter_map(|(name, binding)| {

src/libsyntax/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ bitflags = "1.0"
1414
serialize = { path = "../libserialize" }
1515
log = "0.4"
1616
scoped-tls = "1.0"
17+
lazy_static = "1.0.0"
1718
syntax_pos = { path = "../libsyntax_pos" }
1819
errors = { path = "../librustc_errors", package = "rustc_errors" }
1920
rustc_data_structures = { path = "../librustc_data_structures" }
21+
rustc_macros = { path = "../librustc_macros" }
2022
rustc_target = { path = "../librustc_target" }
2123
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }

0 commit comments

Comments
 (0)