From 4eb08bb2ab43f0ad80071469771381a4dd03603d Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Tue, 23 Aug 2016 03:21:17 +0000 Subject: [PATCH] Refactor away `AttrNestedMetaItemMethods`. --- src/librustc/hir/check_attr.rs | 2 +- src/librustc/lint/context.rs | 2 +- src/librustc_borrowck/borrowck/mir/mod.rs | 2 +- src/librustc_incremental/assert_dep_graph.rs | 2 +- .../persist/dirty_clean.rs | 2 +- src/librustc_lint/builtin.rs | 2 +- src/librustc_metadata/creader.rs | 2 +- src/librustc_metadata/macro_import.rs | 2 +- src/librustc_plugin/load.rs | 2 +- src/librustc_trans/assert_module_sources.rs | 2 +- src/librustdoc/clean/mod.rs | 4 +- src/librustdoc/test.rs | 1 - src/librustdoc/visit_ast.rs | 2 +- src/libsyntax/attr.rs | 74 +++++++++---------- src/libsyntax/config.rs | 2 +- src/libsyntax/feature_gate.rs | 2 +- src/libsyntax/test.rs | 2 +- src/libsyntax_ext/deriving/mod.rs | 2 +- 18 files changed, 50 insertions(+), 59 deletions(-) diff --git a/src/librustc/hir/check_attr.rs b/src/librustc/hir/check_attr.rs index 350b9fd88f6..42f293577b3 100644 --- a/src/librustc/hir/check_attr.rs +++ b/src/librustc/hir/check_attr.rs @@ -11,7 +11,7 @@ use session::Session; use syntax::ast; -use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; +use syntax::attr::AttrMetaMethods; use syntax::visit; use syntax::visit::Visitor; diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index bccd217352b..ed812471594 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -38,7 +38,7 @@ use util::nodemap::FnvHashMap; use std::cmp; use std::default::Default as StdDefault; use std::mem; -use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::{self, AttrMetaMethods}; use syntax::parse::token::InternedString; use syntax::ast; use syntax_pos::Span; diff --git a/src/librustc_borrowck/borrowck/mir/mod.rs b/src/librustc_borrowck/borrowck/mir/mod.rs index 55c6a4de9df..3285cecb79c 100644 --- a/src/librustc_borrowck/borrowck/mir/mod.rs +++ b/src/librustc_borrowck/borrowck/mir/mod.rs @@ -11,7 +11,7 @@ use borrowck::BorrowckCtxt; use syntax::ast::{self, MetaItem}; -use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::AttrMetaMethods; use syntax::ptr::P; use syntax_pos::{Span, DUMMY_SP}; diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 482b351481c..7ab61d494b5 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -56,7 +56,7 @@ use std::env; use std::fs::File; use std::io::Write; use syntax::ast; -use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; +use syntax::attr::AttrMetaMethods; use syntax::parse::token::InternedString; use syntax_pos::Span; diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index f0092ce04d1..0a87c083318 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -32,7 +32,7 @@ use rustc::hir::def_id::DefId; use rustc::hir::intravisit::Visitor; use rustc_data_structures::fnv::FnvHashSet; use syntax::ast::{self, Attribute, NestedMetaItem}; -use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; +use syntax::attr::AttrMetaMethods; use syntax::parse::token::InternedString; use rustc::ty::TyCtxt; diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 9a4eec2d05b..57afcc47d67 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -44,7 +44,7 @@ use lint::{LintPass, LateLintPass}; use std::collections::HashSet; use syntax::{ast}; -use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods, AttributeMethods}; +use syntax::attr::{self, AttrMetaMethods, AttributeMethods}; use syntax_pos::{Span}; use rustc::hir::{self, PatKind}; diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 4dc3d04c4a2..91b7acff03f 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -35,7 +35,7 @@ use syntax::ast; use syntax::abi::Abi; use syntax::codemap; use syntax::parse; -use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::{self, AttrMetaMethods}; use syntax::parse::token::InternedString; use syntax::visit; use syntax_pos::{self, Span, mk_sp, Pos}; diff --git a/src/librustc_metadata/macro_import.rs b/src/librustc_metadata/macro_import.rs index ec0c9f455cd..2482a53fe48 100644 --- a/src/librustc_metadata/macro_import.rs +++ b/src/librustc_metadata/macro_import.rs @@ -18,7 +18,7 @@ use rustc::session::Session; use std::collections::{HashSet, HashMap}; use syntax::parse::token; use syntax::ast; -use syntax::attr::{self, AttrNestedMetaItemMethods, AttrMetaMethods}; +use syntax::attr::{self, AttrMetaMethods}; use syntax::ext; use syntax_pos::Span; diff --git a/src/librustc_plugin/load.rs b/src/librustc_plugin/load.rs index 5a8d2e58c55..895645a76cb 100644 --- a/src/librustc_plugin/load.rs +++ b/src/librustc_plugin/load.rs @@ -20,7 +20,7 @@ use std::env; use std::mem; use std::path::PathBuf; use syntax::ast; -use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::{AttrMetaMethods}; use syntax_pos::{Span, COMMAND_LINE_SP}; /// Pointer to a registrar function. diff --git a/src/librustc_trans/assert_module_sources.rs b/src/librustc_trans/assert_module_sources.rs index e2633c82976..e0532e7476f 100644 --- a/src/librustc_trans/assert_module_sources.rs +++ b/src/librustc_trans/assert_module_sources.rs @@ -29,7 +29,7 @@ use rustc::ty::TyCtxt; use syntax::ast; -use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::AttrMetaMethods; use syntax::parse::token::InternedString; use {ModuleSource, ModuleTranslation}; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 02fa073dd55..79c0bfaaa34 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -26,7 +26,7 @@ pub use self::Visibility::*; use syntax::abi::Abi; use syntax::ast; use syntax::attr; -use syntax::attr::{AttributeMethods, AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::{AttributeMethods, AttrMetaMethods}; use syntax::codemap::Spanned; use syntax::parse::token::{self, InternedString, keywords}; use syntax::ptr::P; @@ -542,7 +542,7 @@ impl Clean for ast::Attribute { } // This is a rough approximation that gets us what we want. -impl attr::AttrNestedMetaItemMethods for Attribute { +impl Attribute { fn check_name(&self, name: &str) -> bool { self.name().map_or(false, |mi_name| &*mi_name == name) } diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 23a047f922f..caf05a947d3 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -141,7 +141,6 @@ pub fn run(input: &str, // Look for #![doc(test(no_crate_inject))], used by crates in the std facade fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions { use syntax::attr::AttrMetaMethods; - use syntax::attr::AttrNestedMetaItemMethods; use syntax::print::pprust; let mut opts = TestOptions { diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 4e3a81b1bae..d3397a04b3a 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -17,7 +17,7 @@ use std::mem; use syntax::abi; use syntax::ast; use syntax::attr; -use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; +use syntax::attr::AttrMetaMethods; use syntax_pos::Span; use rustc::hir::map as hir_map; diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 4897425f2c0..f1a820ce1d4 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -81,32 +81,47 @@ pub fn is_used(attr: &Attribute) -> bool { }) } -pub trait AttrNestedMetaItemMethods { +impl NestedMetaItem { + /// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem. + pub fn meta_item(&self) -> Option<&P> { + match self.node { + NestedMetaItemKind::MetaItem(ref item) => Some(&item), + _ => None + } + } + + /// Returns the Lit if self is a NestedMetaItemKind::Literal. + pub fn literal(&self) -> Option<&Lit> { + match self.node { + NestedMetaItemKind::Literal(ref lit) => Some(&lit), + _ => None + } + } + + /// Returns the Span for `self`. + pub fn span(&self) -> Span { + self.span + } + /// Returns true if this list item is a MetaItem with a name of `name`. - fn check_name(&self, name: &str) -> bool { + pub fn check_name(&self, name: &str) -> bool { self.meta_item().map_or(false, |meta_item| meta_item.check_name(name)) } /// Returns the name of the meta item, e.g. `foo` in `#[foo]`, /// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem - fn name(&self) -> Option { + pub fn name(&self) -> Option { self.meta_item().and_then(|meta_item| Some(meta_item.name())) } - /// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem. - fn meta_item(&self) -> Option<&P>; - - /// Returns the Lit if self is a NestedMetaItemKind::Literal. - fn literal(&self) -> Option<&Lit>; - /// Gets the string value if self is a MetaItem and the MetaItem is a /// MetaItemKind::NameValue variant containing a string, otherwise None. - fn value_str(&self) -> Option { + pub fn value_str(&self) -> Option { self.meta_item().and_then(|meta_item| meta_item.value_str()) } /// Returns a MetaItem if self is a MetaItem with Kind Word. - fn word(&self) -> Option<&P> { + pub fn word(&self) -> Option<&P> { self.meta_item().and_then(|meta_item| if meta_item.is_word() { Some(meta_item) } else { @@ -115,57 +130,34 @@ pub trait AttrNestedMetaItemMethods { } /// Gets a list of inner meta items from a list MetaItem type. - fn meta_item_list(&self) -> Option<&[NestedMetaItem]> { + pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> { self.meta_item().and_then(|meta_item| meta_item.meta_item_list()) } /// Returns `true` if the variant is MetaItem. - fn is_meta_item(&self) -> bool { + pub fn is_meta_item(&self) -> bool { self.meta_item().is_some() } /// Returns `true` if the variant is Literal. - fn is_literal(&self) -> bool { + pub fn is_literal(&self) -> bool { self.literal().is_some() } /// Returns `true` if self is a MetaItem and the meta item is a word. - fn is_word(&self) -> bool { + pub fn is_word(&self) -> bool { self.word().is_some() } /// Returns `true` if self is a MetaItem and the meta item is a ValueString. - fn is_value_str(&self) -> bool { + pub fn is_value_str(&self) -> bool { self.value_str().is_some() } /// Returns `true` if self is a MetaItem and the meta item is a list. - fn is_meta_item_list(&self) -> bool { + pub fn is_meta_item_list(&self) -> bool { self.meta_item_list().is_some() } - - /// Returns the Span for `self`. - fn span(&self) -> Span; -} - -impl AttrNestedMetaItemMethods for NestedMetaItem { - fn meta_item(&self) -> Option<&P> { - match self.node { - NestedMetaItemKind::MetaItem(ref item) => Some(&item), - _ => None - } - } - - fn literal(&self) -> Option<&Lit> { - match self.node { - NestedMetaItemKind::Literal(ref lit) => Some(&lit), - _ => None - } - } - - fn span(&self) -> Span { - self.span - } } pub trait AttrMetaMethods { @@ -431,7 +423,7 @@ pub fn contains(haystack: &[P], needle: &MetaItem) -> bool { }) } -pub fn list_contains_name(items: &[AM], name: &str) -> bool { +pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool { debug!("attr::list_contains_name (name={})", name); items.iter().any(|item| { debug!(" testing: {:?}", item.name()); diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 4663143f4b1..ed05dc243b3 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use attr::{AttrMetaMethods, AttrNestedMetaItemMethods, HasAttrs}; +use attr::{AttrMetaMethods, HasAttrs}; use feature_gate::{emit_feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue}; use fold::Folder; use {fold, attr}; diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index df1d5c4d9ca..d2aa9a4cb6b 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -27,7 +27,7 @@ use self::AttributeGate::*; use abi::Abi; use ast::{self, NodeId, PatKind}; -use attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; +use attr::{self, AttrMetaMethods}; use codemap::CodeMap; use syntax_pos::Span; use errors::Handler; diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index ce917f248e1..0a14eae7d35 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -19,7 +19,7 @@ use std::iter; use std::slice; use std::mem; use std::vec; -use attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; +use attr::{self, AttrMetaMethods}; use syntax_pos::{self, DUMMY_SP, NO_EXPANSION, Span, FileMap, BytePos}; use std::rc::Rc; diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index ffc1bfd6db8..fae17c0465b 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -11,7 +11,7 @@ //! The compiler code necessary to implement the `#[derive]` extensions. use syntax::ast::{self, MetaItem}; -use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; +use syntax::attr::AttrMetaMethods; use syntax::ext::base::{Annotatable, ExtCtxt, SyntaxEnv}; use syntax::ext::base::{MultiDecorator, MultiItemDecorator, MultiModifier}; use syntax::ext::build::AstBuilder;