]> git.lizzy.rs Git - rust.git/commitdiff
Make check_name generic
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Fri, 5 Apr 2019 01:05:30 +0000 (03:05 +0200)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Mon, 15 Apr 2019 05:23:02 +0000 (07:23 +0200)
src/librustc/middle/lib_features.rs
src/librustc_incremental/persist/dirty_clean.rs
src/librustc_lint/unused.rs
src/libsyntax/attr/mod.rs
src/libsyntax/feature_gate.rs
src/libsyntax_ext/proc_macro_decls.rs
src/libsyntax_pos/symbol.rs

index ee8dd9e58b5ff9b2babee032a9575bfad06aeac1..e79ef8bfc6fb43f0a31af6cd8003d4d8140a5318 100644 (file)
@@ -8,7 +8,7 @@
 use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
 use syntax::symbol::Symbol;
 use syntax::ast::{Attribute, MetaItem, MetaItemKind};
-use syntax_pos::Span;
+use syntax_pos::{Span, symbols};
 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
 use rustc_macros::HashStable;
 use errors::DiagnosticId;
@@ -51,12 +51,12 @@ fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LibFeatureCollector<'a, 'tcx> {
     }
 
     fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
-        let stab_attrs = vec!["stable", "unstable", "rustc_const_unstable"];
+        let stab_attrs = [symbols::stable, symbols::unstable, symbols::rustc_const_unstable];
 
         // Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
         // `#[rustc_const_unstable (..)]`).
         if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| {
-            attr.check_name(stab_attr)
+            attr.check_name(**stab_attr)
         }) {
             let meta_item = attr.meta();
             if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta_item {
index 5c4fd9dba7af47dc9a92c961bf0443a7b7411224..6f5b411946c2d8d3408beb1200a29979178c659a 100644 (file)
@@ -599,7 +599,7 @@ impl<'a, 'tcx> FindAllAttrs<'a, 'tcx> {
 
     fn is_active_attr(&mut self, attr: &Attribute) -> bool {
         for attr_name in &self.attr_names {
-            if attr.check_name(attr_name) && check_config(self.tcx, attr) {
+            if attr.check_name(*attr_name) && check_config(self.tcx, attr) {
                 return true;
             }
         }
index d41d97f58bcbe0aea788c1acf62ad2110439ee57..2aee21abb58e5ba8dda0dac129141c4d243a1b39 100644 (file)
@@ -228,7 +228,7 @@ fn check_attribute(&mut self, cx: &LateContext<'_, '_>, attr: &ast::Attribute) {
 
         let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
         for &(ref name, ty) in plugin_attributes.iter() {
-            if ty == AttributeType::Whitelisted && attr.check_name(&name) {
+            if ty == AttributeType::Whitelisted && attr.check_name(&**name) {
                 debug!("{:?} (plugin attr) is whitelisted with ty {:?}", name, ty);
                 break;
             }
index f34bbc9f35dad6dc0eddc0e7ec9d415db88af1c0..e00f91e39528018e898756e2c7bd97f1899ccad4 100644 (file)
@@ -81,15 +81,13 @@ pub fn literal(&self) -> Option<&Lit> {
     }
 
     /// Returns `true` if this list item is a MetaItem with a name of `name`.
-    pub fn check_name(&self, name: &str) -> bool {
+    pub fn check_name<T>(&self, name: T) -> bool
+    where
+        Path: PartialEq<T>,
+    {
         self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
     }
 
-    /// Returns `true` if this list item is a MetaItem with a name of `name`.
-    pub fn check_name_symbol(&self, name: Symbol) -> bool {
-        self.meta_item().map_or(false, |meta_item| meta_item.check_name_symbol(name))
-    }
-
     /// For a single-segment meta-item returns its name, otherwise returns `None`.
     pub fn ident(&self) -> Option<Ident> {
         self.meta_item().and_then(|meta_item| meta_item.ident())
@@ -156,19 +154,10 @@ impl Attribute {
     /// attribute is marked as used.
     ///
     /// To check the attribute name without marking it used, use the `path` field directly.
-    pub fn check_name(&self, name: &str) -> bool {
-        let matches = self.path == name;
-        if matches {
-            mark_used(self);
-        }
-        matches
-    }
-
-    /// Returns `true` if the attribute's path matches the argument. If it matches, then the
-    /// attribute is marked as used.
-    ///
-    /// To check the attribute name without marking it used, use the `path` field directly.
-    pub fn check_name_symbol(&self, name: Symbol) -> bool {
+    pub fn check_name<T>(&self, name: T) -> bool
+    where
+        Path: PartialEq<T>,
+    {
         let matches = self.path == name;
         if matches {
             mark_used(self);
@@ -261,11 +250,10 @@ pub fn is_word(&self) -> bool {
         }
     }
 
-    pub fn check_name(&self, name: &str) -> bool {
-        self.path == name
-    }
-
-    pub fn check_name_symbol(&self, name: Symbol) -> bool {
+    pub fn check_name<T>(&self, name: T) -> bool
+    where
+        Path: PartialEq<T>,
+    {
         self.path == name
     }
 
index b3e5d808a476d4000701d52be41e7c8efec88041..ba4380416fb2a5fc524ef173b08030fd47cee671 100644 (file)
@@ -1366,7 +1366,7 @@ fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
                     }
                 } else if n == "doc" {
                     if let Some(content) = attr.meta_item_list() {
-                        if content.iter().any(|c| c.check_name_symbol(symbols::include)) {
+                        if content.iter().any(|c| c.check_name(symbols::include)) {
                             gate_feature!(self, external_doc, attr.span,
                                 "#[doc(include = \"...\")] is experimental"
                             );
@@ -1667,25 +1667,25 @@ fn visit_attribute(&mut self, attr: &ast::Attribute) {
         // check for gated attributes
         self.context.check_attribute(attr, false);
 
-        if attr.check_name_symbol(symbols::doc) {
+        if attr.check_name(symbols::doc) {
             if let Some(content) = attr.meta_item_list() {
-                if content.len() == 1 && content[0].check_name_symbol(symbols::cfg) {
+                if content.len() == 1 && content[0].check_name(symbols::cfg) {
                     gate_feature_post!(&self, doc_cfg, attr.span,
                         "#[doc(cfg(...))] is experimental"
                     );
-                } else if content.iter().any(|c| c.check_name_symbol(symbols::masked)) {
+                } else if content.iter().any(|c| c.check_name(symbols::masked)) {
                     gate_feature_post!(&self, doc_masked, attr.span,
                         "#[doc(masked)] is experimental"
                     );
-                } else if content.iter().any(|c| c.check_name_symbol(symbols::spotlight)) {
+                } else if content.iter().any(|c| c.check_name(symbols::spotlight)) {
                     gate_feature_post!(&self, doc_spotlight, attr.span,
                         "#[doc(spotlight)] is experimental"
                     );
-                } else if content.iter().any(|c| c.check_name_symbol(symbols::alias)) {
+                } else if content.iter().any(|c| c.check_name(symbols::alias)) {
                     gate_feature_post!(&self, doc_alias, attr.span,
                         "#[doc(alias = \"...\")] is experimental"
                     );
-                } else if content.iter().any(|c| c.check_name_symbol(symbols::keyword)) {
+                } else if content.iter().any(|c| c.check_name(symbols::keyword)) {
                     gate_feature_post!(&self, doc_keyword, attr.span,
                         "#[doc(keyword = \"...\")] is experimental"
                     );
@@ -1748,7 +1748,7 @@ fn visit_item(&mut self, i: &'a ast::Item) {
             ast::ItemKind::Struct(..) => {
                 for attr in attr::filter_by_name(&i.attrs[..], "repr") {
                     for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
-                        if item.check_name_symbol(symbols::simd) {
+                        if item.check_name(symbols::simd) {
                             gate_feature_post!(&self, repr_simd, attr.span,
                                                "SIMD types are experimental and possibly buggy");
                         }
@@ -1759,7 +1759,7 @@ fn visit_item(&mut self, i: &'a ast::Item) {
             ast::ItemKind::Enum(..) => {
                 for attr in attr::filter_by_name(&i.attrs[..], "repr") {
                     for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
-                        if item.check_name_symbol(symbols::align) {
+                        if item.check_name(symbols::align) {
                             gate_feature_post!(&self, repr_align_enum, attr.span,
                                                "`#[repr(align(x))]` on enums is experimental");
                         }
@@ -2083,7 +2083,7 @@ fn feature_removed(span_handler: &Handler, span: Span, reason: Option<&str>) {
     // Process the edition umbrella feature-gates first, to ensure
     // `edition_enabled_features` is completed before it's queried.
     for attr in krate_attrs {
-        if !attr.check_name_symbol(symbols::feature) {
+        if !attr.check_name(symbols::feature) {
             continue
         }
 
@@ -2128,7 +2128,7 @@ fn feature_removed(span_handler: &Handler, span: Span, reason: Option<&str>) {
     }
 
     for attr in krate_attrs {
-        if !attr.check_name_symbol(symbols::feature) {
+        if !attr.check_name(symbols::feature) {
             continue
         }
 
@@ -2258,7 +2258,7 @@ fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
     };
     if !allow_features {
         for attr in &krate.attrs {
-            if attr.check_name_symbol(symbols::feature) {
+            if attr.check_name(symbols::feature) {
                 let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
                 span_err!(span_handler, attr.span, E0554,
                           "#![feature] may not be used on the {} release channel",
index 22fee902aea262546694649e6edb2532858cc31b..f0390ba3d40cb0830a3fb2acc517f91a2fc0df4c 100644 (file)
@@ -87,7 +87,7 @@ pub fn modify(sess: &ParseSess,
 }
 
 pub fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
-    PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind))
+    PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(*kind))
 }
 
 impl<'a> CollectProcMacros<'a> {
index 8c195fb45ef7b751fccbe12e936857874c260d6d..83cb6f4e86b0ea31ec05c97abfa29b0612b1dbc9 100644 (file)
 
     // Other symbols that can be referred to with syntax_pos::symbols::*
     Other {
-        doc, cfg, masked, spotlight, alias, keyword, feature, include, simd, align,
+        doc, cfg, masked, spotlight, alias, keyword, feature, include, simd, align, stable,
+        unstable, rustc_const_unstable,
     }
 }