Skip to content

Commit d1fcdd9

Browse files
committed
Make check_name generic
1 parent 37d325e commit d1fcdd9

File tree

7 files changed

+32
-43
lines changed

7 files changed

+32
-43
lines changed

src/librustc/middle/lib_features.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ty::TyCtxt;
88
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
99
use syntax::symbol::Symbol;
1010
use syntax::ast::{Attribute, MetaItem, MetaItemKind};
11-
use syntax_pos::Span;
11+
use syntax_pos::{Span, symbols};
1212
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1313
use rustc_macros::HashStable;
1414
use errors::DiagnosticId;
@@ -51,12 +51,12 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
5151
}
5252

5353
fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
54-
let stab_attrs = vec!["stable", "unstable", "rustc_const_unstable"];
54+
let stab_attrs = [symbols::stable, symbols::unstable, symbols::rustc_const_unstable];
5555

5656
// Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
5757
// `#[rustc_const_unstable (..)]`).
5858
if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
59-
attr.check_name(stab_attr)
59+
attr.check_name(**stab_attr)
6060
}) {
6161
let meta_item = attr.meta();
6262
if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {

src/librustc_incremental/persist/dirty_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a, 'tcx> FindAllAttrs<'a, 'tcx> {
599599

600600
fn is_active_attr(&mut self, attr: &Attribute) -> bool {
601601
for attr_name in &self.attr_names {
602-
if attr.check_name(attr_name) && check_config(self.tcx, attr) {
602+
if attr.check_name(*attr_name) && check_config(self.tcx, attr) {
603603
return true;
604604
}
605605
}

src/librustc_lint/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
228228

229229
let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
230230
for &(ref name, ty) in plugin_attributes.iter() {
231-
if ty == AttributeType::Whitelisted && attr.check_name(&name) {
231+
if ty == AttributeType::Whitelisted && attr.check_name(&**name) {
232232
debug!("{:?} (plugin attr) is whitelisted with ty {:?}", name, ty);
233233
break;
234234
}

src/libsyntax/attr/mod.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ impl NestedMetaItem {
8181
}
8282

8383
/// Returns `true` if this list item is a MetaItem with a name of `name`.
84-
pub fn check_name(&self, name: &str) -> bool {
84+
pub fn check_name<T>(&self, name: T) -> bool
85+
where
86+
Path: PartialEq<T>,
87+
{
8588
self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
8689
}
8790

88-
/// Returns `true` if this list item is a MetaItem with a name of `name`.
89-
pub fn check_name_symbol(&self, name: Symbol) -> bool {
90-
self.meta_item().map_or(false, |meta_item| meta_item.check_name_symbol(name))
91-
}
92-
9391
/// For a single-segment meta-item returns its name, otherwise returns `None`.
9492
pub fn ident(&self) -> Option<Ident> {
9593
self.meta_item().and_then(|meta_item| meta_item.ident())
@@ -156,19 +154,10 @@ impl Attribute {
156154
/// attribute is marked as used.
157155
///
158156
/// To check the attribute name without marking it used, use the `path` field directly.
159-
pub fn check_name(&self, name: &str) -> bool {
160-
let matches = self.path == name;
161-
if matches {
162-
mark_used(self);
163-
}
164-
matches
165-
}
166-
167-
/// Returns `true` if the attribute's path matches the argument. If it matches, then the
168-
/// attribute is marked as used.
169-
///
170-
/// To check the attribute name without marking it used, use the `path` field directly.
171-
pub fn check_name_symbol(&self, name: Symbol) -> bool {
157+
pub fn check_name<T>(&self, name: T) -> bool
158+
where
159+
Path: PartialEq<T>,
160+
{
172161
let matches = self.path == name;
173162
if matches {
174163
mark_used(self);
@@ -261,11 +250,10 @@ impl MetaItem {
261250
}
262251
}
263252

264-
pub fn check_name(&self, name: &str) -> bool {
265-
self.path == name
266-
}
267-
268-
pub fn check_name_symbol(&self, name: Symbol) -> bool {
253+
pub fn check_name<T>(&self, name: T) -> bool
254+
where
255+
Path: PartialEq<T>,
256+
{
269257
self.path == name
270258
}
271259

src/libsyntax/feature_gate.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl<'a> Context<'a> {
13661366
}
13671367
} else if n == "doc" {
13681368
if let Some(content) = attr.meta_item_list() {
1369-
if content.iter().any(|c| c.check_name_symbol(symbols::include)) {
1369+
if content.iter().any(|c| c.check_name(symbols::include)) {
13701370
gate_feature!(self, external_doc, attr.span,
13711371
"#[doc(include = \"...\")] is experimental"
13721372
);
@@ -1667,25 +1667,25 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
16671667
// check for gated attributes
16681668
self.context.check_attribute(attr, false);
16691669

1670-
if attr.check_name_symbol(symbols::doc) {
1670+
if attr.check_name(symbols::doc) {
16711671
if let Some(content) = attr.meta_item_list() {
1672-
if content.len() == 1 && content[0].check_name_symbol(symbols::cfg) {
1672+
if content.len() == 1 && content[0].check_name(symbols::cfg) {
16731673
gate_feature_post!(&self, doc_cfg, attr.span,
16741674
"#[doc(cfg(...))] is experimental"
16751675
);
1676-
} else if content.iter().any(|c| c.check_name_symbol(symbols::masked)) {
1676+
} else if content.iter().any(|c| c.check_name(symbols::masked)) {
16771677
gate_feature_post!(&self, doc_masked, attr.span,
16781678
"#[doc(masked)] is experimental"
16791679
);
1680-
} else if content.iter().any(|c| c.check_name_symbol(symbols::spotlight)) {
1680+
} else if content.iter().any(|c| c.check_name(symbols::spotlight)) {
16811681
gate_feature_post!(&self, doc_spotlight, attr.span,
16821682
"#[doc(spotlight)] is experimental"
16831683
);
1684-
} else if content.iter().any(|c| c.check_name_symbol(symbols::alias)) {
1684+
} else if content.iter().any(|c| c.check_name(symbols::alias)) {
16851685
gate_feature_post!(&self, doc_alias, attr.span,
16861686
"#[doc(alias = \"...\")] is experimental"
16871687
);
1688-
} else if content.iter().any(|c| c.check_name_symbol(symbols::keyword)) {
1688+
} else if content.iter().any(|c| c.check_name(symbols::keyword)) {
16891689
gate_feature_post!(&self, doc_keyword, attr.span,
16901690
"#[doc(keyword = \"...\")] is experimental"
16911691
);
@@ -1748,7 +1748,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17481748
ast::ItemKind::Struct(..) => {
17491749
for attr in attr::filter_by_name(&i.attrs[..], "repr") {
17501750
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
1751-
if item.check_name_symbol(symbols::simd) {
1751+
if item.check_name(symbols::simd) {
17521752
gate_feature_post!(&self, repr_simd, attr.span,
17531753
"SIMD types are experimental and possibly buggy");
17541754
}
@@ -1759,7 +1759,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17591759
ast::ItemKind::Enum(..) => {
17601760
for attr in attr::filter_by_name(&i.attrs[..], "repr") {
17611761
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
1762-
if item.check_name_symbol(symbols::align) {
1762+
if item.check_name(symbols::align) {
17631763
gate_feature_post!(&self, repr_align_enum, attr.span,
17641764
"`#[repr(align(x))]` on enums is experimental");
17651765
}
@@ -2083,7 +2083,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
20832083
// Process the edition umbrella feature-gates first, to ensure
20842084
// `edition_enabled_features` is completed before it's queried.
20852085
for attr in krate_attrs {
2086-
if !attr.check_name_symbol(symbols::feature) {
2086+
if !attr.check_name(symbols::feature) {
20872087
continue
20882088
}
20892089

@@ -2128,7 +2128,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
21282128
}
21292129

21302130
for attr in krate_attrs {
2131-
if !attr.check_name_symbol(symbols::feature) {
2131+
if !attr.check_name(symbols::feature) {
21322132
continue
21332133
}
21342134

@@ -2258,7 +2258,7 @@ fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
22582258
};
22592259
if !allow_features {
22602260
for attr in &krate.attrs {
2261-
if attr.check_name_symbol(symbols::feature) {
2261+
if attr.check_name(symbols::feature) {
22622262
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
22632263
span_err!(span_handler, attr.span, E0554,
22642264
"#![feature] may not be used on the {} release channel",

src/libsyntax_ext/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn modify(sess: &ParseSess,
8787
}
8888

8989
pub fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
90-
PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind))
90+
PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(*kind))
9191
}
9292

9393
impl<'a> CollectProcMacros<'a> {

src/libsyntax_pos/symbol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ symbols! {
100100

101101
// Other symbols that can be referred to with syntax_pos::symbols::*
102102
Other {
103-
doc, cfg, masked, spotlight, alias, keyword, feature, include, simd, align,
103+
doc, cfg, masked, spotlight, alias, keyword, feature, include, simd, align, stable,
104+
unstable, rustc_const_unstable,
104105
}
105106
}
106107

0 commit comments

Comments
 (0)