Skip to content

Commit 907ad61

Browse files
committed
Make check_name generic
1 parent 6d062a1 commit 907ad61

File tree

7 files changed

+32
-43
lines changed

7 files changed

+32
-43
lines changed

src/librustc/middle/lib_features.rs

+3-3
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

+1-1
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

+1-1
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

+12-24
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

+12-12
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
);
@@ -1648,25 +1648,25 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
16481648
// check for gated attributes
16491649
self.context.check_attribute(attr, false);
16501650

1651-
if attr.check_name_symbol(symbols::doc) {
1651+
if attr.check_name(symbols::doc) {
16521652
if let Some(content) = attr.meta_item_list() {
1653-
if content.len() == 1 && content[0].check_name_symbol(symbols::cfg) {
1653+
if content.len() == 1 && content[0].check_name(symbols::cfg) {
16541654
gate_feature_post!(&self, doc_cfg, attr.span,
16551655
"#[doc(cfg(...))] is experimental"
16561656
);
1657-
} else if content.iter().any(|c| c.check_name_symbol(symbols::masked)) {
1657+
} else if content.iter().any(|c| c.check_name(symbols::masked)) {
16581658
gate_feature_post!(&self, doc_masked, attr.span,
16591659
"#[doc(masked)] is experimental"
16601660
);
1661-
} else if content.iter().any(|c| c.check_name_symbol(symbols::spotlight)) {
1661+
} else if content.iter().any(|c| c.check_name(symbols::spotlight)) {
16621662
gate_feature_post!(&self, doc_spotlight, attr.span,
16631663
"#[doc(spotlight)] is experimental"
16641664
);
1665-
} else if content.iter().any(|c| c.check_name_symbol(symbols::alias)) {
1665+
} else if content.iter().any(|c| c.check_name(symbols::alias)) {
16661666
gate_feature_post!(&self, doc_alias, attr.span,
16671667
"#[doc(alias = \"...\")] is experimental"
16681668
);
1669-
} else if content.iter().any(|c| c.check_name_symbol(symbols::keyword)) {
1669+
} else if content.iter().any(|c| c.check_name(symbols::keyword)) {
16701670
gate_feature_post!(&self, doc_keyword, attr.span,
16711671
"#[doc(keyword = \"...\")] is experimental"
16721672
);
@@ -1727,7 +1727,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17271727
ast::ItemKind::Struct(..) => {
17281728
for attr in attr::filter_by_name(&i.attrs[..], "repr") {
17291729
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
1730-
if item.check_name_symbol(symbols::simd) {
1730+
if item.check_name(symbols::simd) {
17311731
gate_feature_post!(&self, repr_simd, attr.span,
17321732
"SIMD types are experimental and possibly buggy");
17331733
}
@@ -1738,7 +1738,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17381738
ast::ItemKind::Enum(..) => {
17391739
for attr in attr::filter_by_name(&i.attrs[..], "repr") {
17401740
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
1741-
if item.check_name_symbol(symbols::align) {
1741+
if item.check_name(symbols::align) {
17421742
gate_feature_post!(&self, repr_align_enum, attr.span,
17431743
"`#[repr(align(x))]` on enums is experimental");
17441744
}
@@ -2062,7 +2062,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
20622062
// Process the edition umbrella feature-gates first, to ensure
20632063
// `edition_enabled_features` is completed before it's queried.
20642064
for attr in krate_attrs {
2065-
if !attr.check_name_symbol(symbols::feature) {
2065+
if !attr.check_name(symbols::feature) {
20662066
continue
20672067
}
20682068

@@ -2107,7 +2107,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute],
21072107
}
21082108

21092109
for attr in krate_attrs {
2110-
if !attr.check_name_symbol(symbols::feature) {
2110+
if !attr.check_name(symbols::feature) {
21112111
continue
21122112
}
21132113

@@ -2237,7 +2237,7 @@ fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
22372237
};
22382238
if !allow_features {
22392239
for attr in &krate.attrs {
2240-
if attr.check_name_symbol(symbols::feature) {
2240+
if attr.check_name(symbols::feature) {
22412241
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
22422242
span_err!(span_handler, attr.span, E0554,
22432243
"#![feature] may not be used on the {} release channel",

src/libsyntax_ext/proc_macro_decls.rs

+1-1
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

+2-1
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)