Skip to content

Commit

Permalink
Refactor away AttrNestedMetaItemMethods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Aug 25, 2016
1 parent 8250a26 commit 4eb08bb
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/check_attr.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/context.rs
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mir/mod.rs
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/assert_dep_graph.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/dirty_clean.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/creader.rs
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/macro_import.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_plugin/load.rs
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/assert_module_sources.rs
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/clean/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -542,7 +542,7 @@ impl Clean<Attribute> 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)
}
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/test.rs
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/visit_ast.rs
Expand Up @@ -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;
Expand Down
74 changes: 33 additions & 41 deletions src/libsyntax/attr.rs
Expand Up @@ -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`.
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<InternedString> {
pub fn name(&self) -> Option<InternedString> {
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
/// 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())
}

/// 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() {
Some(meta_item)
} else {
Expand All @@ -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<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 {
Expand Down Expand Up @@ -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);
items.iter().any(|item| {
debug!(" testing: {:?}", item.name());
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/config.rs
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/feature_gate.rs
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/test.rs
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_ext/deriving/mod.rs
Expand Up @@ -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;
Expand Down

0 comments on commit 4eb08bb

Please sign in to comment.