1
Fork 0

Refactor away AttrNestedMetaItemMethods.

This commit is contained in:
Jeffrey Seyfried 2016-08-23 03:21:17 +00:00
parent 8250a26b5b
commit 4eb08bb2ab
18 changed files with 50 additions and 59 deletions

View file

@ -11,7 +11,7 @@
use session::Session; use session::Session;
use syntax::ast; use syntax::ast;
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; use syntax::attr::AttrMetaMethods;
use syntax::visit; use syntax::visit;
use syntax::visit::Visitor; use syntax::visit::Visitor;

View file

@ -38,7 +38,7 @@ use util::nodemap::FnvHashMap;
use std::cmp; use std::cmp;
use std::default::Default as StdDefault; use std::default::Default as StdDefault;
use std::mem; use std::mem;
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::{self, AttrMetaMethods};
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use syntax::ast; use syntax::ast;
use syntax_pos::Span; use syntax_pos::Span;

View file

@ -11,7 +11,7 @@
use borrowck::BorrowckCtxt; use borrowck::BorrowckCtxt;
use syntax::ast::{self, MetaItem}; use syntax::ast::{self, MetaItem};
use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::AttrMetaMethods;
use syntax::ptr::P; use syntax::ptr::P;
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};

View file

@ -56,7 +56,7 @@ use std::env;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use syntax::ast; use syntax::ast;
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; use syntax::attr::AttrMetaMethods;
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use syntax_pos::Span; use syntax_pos::Span;

View file

@ -32,7 +32,7 @@ use rustc::hir::def_id::DefId;
use rustc::hir::intravisit::Visitor; use rustc::hir::intravisit::Visitor;
use rustc_data_structures::fnv::FnvHashSet; use rustc_data_structures::fnv::FnvHashSet;
use syntax::ast::{self, Attribute, NestedMetaItem}; use syntax::ast::{self, Attribute, NestedMetaItem};
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods}; use syntax::attr::AttrMetaMethods;
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;

View file

@ -44,7 +44,7 @@ use lint::{LintPass, LateLintPass};
use std::collections::HashSet; use std::collections::HashSet;
use syntax::{ast}; use syntax::{ast};
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods, AttributeMethods}; use syntax::attr::{self, AttrMetaMethods, AttributeMethods};
use syntax_pos::{Span}; use syntax_pos::{Span};
use rustc::hir::{self, PatKind}; use rustc::hir::{self, PatKind};

View file

@ -35,7 +35,7 @@ use syntax::ast;
use syntax::abi::Abi; use syntax::abi::Abi;
use syntax::codemap; use syntax::codemap;
use syntax::parse; use syntax::parse;
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::{self, AttrMetaMethods};
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use syntax::visit; use syntax::visit;
use syntax_pos::{self, Span, mk_sp, Pos}; use syntax_pos::{self, Span, mk_sp, Pos};

View file

@ -18,7 +18,7 @@ use rustc::session::Session;
use std::collections::{HashSet, HashMap}; use std::collections::{HashSet, HashMap};
use syntax::parse::token; use syntax::parse::token;
use syntax::ast; use syntax::ast;
use syntax::attr::{self, AttrNestedMetaItemMethods, AttrMetaMethods}; use syntax::attr::{self, AttrMetaMethods};
use syntax::ext; use syntax::ext;
use syntax_pos::Span; use syntax_pos::Span;

View file

@ -20,7 +20,7 @@ use std::env;
use std::mem; use std::mem;
use std::path::PathBuf; use std::path::PathBuf;
use syntax::ast; use syntax::ast;
use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::{AttrMetaMethods};
use syntax_pos::{Span, COMMAND_LINE_SP}; use syntax_pos::{Span, COMMAND_LINE_SP};
/// Pointer to a registrar function. /// Pointer to a registrar function.

View file

@ -29,7 +29,7 @@
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use syntax::ast; use syntax::ast;
use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::AttrMetaMethods;
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use {ModuleSource, ModuleTranslation}; use {ModuleSource, ModuleTranslation};

View file

@ -26,7 +26,7 @@ pub use self::Visibility::*;
use syntax::abi::Abi; use syntax::abi::Abi;
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use syntax::attr::{AttributeMethods, AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::{AttributeMethods, AttrMetaMethods};
use syntax::codemap::Spanned; use syntax::codemap::Spanned;
use syntax::parse::token::{self, InternedString, keywords}; use syntax::parse::token::{self, InternedString, keywords};
use syntax::ptr::P; use syntax::ptr::P;
@ -542,7 +542,7 @@ impl Clean<Attribute> for ast::Attribute {
} }
// This is a rough approximation that gets us what we want. // 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 { fn check_name(&self, name: &str) -> bool {
self.name().map_or(false, |mi_name| &*mi_name == name) self.name().map_or(false, |mi_name| &*mi_name == name)
} }

View file

@ -141,7 +141,6 @@ pub fn run(input: &str,
// Look for #![doc(test(no_crate_inject))], used by crates in the std facade // Look for #![doc(test(no_crate_inject))], used by crates in the std facade
fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions { fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
use syntax::attr::AttrMetaMethods; use syntax::attr::AttrMetaMethods;
use syntax::attr::AttrNestedMetaItemMethods;
use syntax::print::pprust; use syntax::print::pprust;
let mut opts = TestOptions { let mut opts = TestOptions {

View file

@ -17,7 +17,7 @@ use std::mem;
use syntax::abi; use syntax::abi;
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods}; use syntax::attr::AttrMetaMethods;
use syntax_pos::Span; use syntax_pos::Span;
use rustc::hir::map as hir_map; use rustc::hir::map as hir_map;

View file

@ -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<MetaItem>> {
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`. /// 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)) 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]`, /// Returns the name of the meta item, e.g. `foo` in `#[foo]`,
/// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem /// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem
fn name(&self) -> Option<InternedString> { pub fn name(&self) -> Option<InternedString> {
self.meta_item().and_then(|meta_item| Some(meta_item.name())) 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<MetaItem>>;
/// 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 /// Gets the string value if self is a MetaItem and the MetaItem is a
/// MetaItemKind::NameValue variant containing a string, otherwise None. /// MetaItemKind::NameValue variant containing a string, otherwise None.
fn value_str(&self) -> Option<InternedString> { pub fn value_str(&self) -> Option<InternedString> {
self.meta_item().and_then(|meta_item| meta_item.value_str()) self.meta_item().and_then(|meta_item| meta_item.value_str())
} }
/// Returns a MetaItem if self is a MetaItem with Kind Word. /// Returns a MetaItem if self is a MetaItem with Kind Word.
fn word(&self) -> Option<&P<MetaItem>> { pub fn word(&self) -> Option<&P<MetaItem>> {
self.meta_item().and_then(|meta_item| if meta_item.is_word() { self.meta_item().and_then(|meta_item| if meta_item.is_word() {
Some(meta_item) Some(meta_item)
} else { } else {
@ -115,57 +130,34 @@ pub trait AttrNestedMetaItemMethods {
} }
/// Gets a list of inner meta items from a list MetaItem type. /// 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()) self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
} }
/// Returns `true` if the variant is MetaItem. /// 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() self.meta_item().is_some()
} }
/// Returns `true` if the variant is Literal. /// Returns `true` if the variant is Literal.
fn is_literal(&self) -> bool { pub fn is_literal(&self) -> bool {
self.literal().is_some() self.literal().is_some()
} }
/// Returns `true` if self is a MetaItem and the meta item is a word. /// 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() self.word().is_some()
} }
/// Returns `true` if self is a MetaItem and the meta item is a ValueString. /// 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() self.value_str().is_some()
} }
/// Returns `true` if self is a MetaItem and the meta item is a list. /// 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() 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<MetaItem>> {
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 { pub trait AttrMetaMethods {
@ -431,7 +423,7 @@ pub fn contains(haystack: &[P<MetaItem>], needle: &MetaItem) -> bool {
}) })
} }
pub fn list_contains_name<AM: AttrNestedMetaItemMethods>(items: &[AM], name: &str) -> bool { pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool {
debug!("attr::list_contains_name (name={})", name); debug!("attr::list_contains_name (name={})", name);
items.iter().any(|item| { items.iter().any(|item| {
debug!(" testing: {:?}", item.name()); debug!(" testing: {:?}", item.name());

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // 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 feature_gate::{emit_feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue};
use fold::Folder; use fold::Folder;
use {fold, attr}; use {fold, attr};

View file

@ -27,7 +27,7 @@ use self::AttributeGate::*;
use abi::Abi; use abi::Abi;
use ast::{self, NodeId, PatKind}; use ast::{self, NodeId, PatKind};
use attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; use attr::{self, AttrMetaMethods};
use codemap::CodeMap; use codemap::CodeMap;
use syntax_pos::Span; use syntax_pos::Span;
use errors::Handler; use errors::Handler;

View file

@ -19,7 +19,7 @@ use std::iter;
use std::slice; use std::slice;
use std::mem; use std::mem;
use std::vec; use std::vec;
use attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods}; use attr::{self, AttrMetaMethods};
use syntax_pos::{self, DUMMY_SP, NO_EXPANSION, Span, FileMap, BytePos}; use syntax_pos::{self, DUMMY_SP, NO_EXPANSION, Span, FileMap, BytePos};
use std::rc::Rc; use std::rc::Rc;

View file

@ -11,7 +11,7 @@
//! The compiler code necessary to implement the `#[derive]` extensions. //! The compiler code necessary to implement the `#[derive]` extensions.
use syntax::ast::{self, MetaItem}; 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::{Annotatable, ExtCtxt, SyntaxEnv};
use syntax::ext::base::{MultiDecorator, MultiItemDecorator, MultiModifier}; use syntax::ext::base::{MultiDecorator, MultiItemDecorator, MultiModifier};
use syntax::ext::build::AstBuilder; use syntax::ext::build::AstBuilder;