Skip to content

Commit

Permalink
Initial implementation of RFC rust-lang#44690
Browse files Browse the repository at this point in the history
  • Loading branch information
repnop committed Oct 17, 2017
1 parent ba4e8d7 commit e188848
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,31 @@ pub enum NestedMetaItemKind {
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct MetaItem {
pub name: Name,
pub name: Name, //MetaItemName,
pub node: MetaItemKind,
pub span: Span,
}

/*#[derive(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialEq)]
pub enum MetaItemName {
/// A single MetaItem name
Single(Name),
/// A collection of MetaItem names
///
/// Used for attribute paths
Namespaced(Vec<Name>),
}*/

/*impl From<MetaItemName> for Name {
fn from(m: MetaItemName) -> Self {
match m {
MetaItemName::Single(n) => n,
MetaItemName::Namespaced(v) => v[0],
}
}
}*/

/// A compile-time attribute item.
///
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
Expand Down
36 changes: 29 additions & 7 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,37 @@ impl Attribute {
}

pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
// TODO: Find a new place to put the whitelist
static WHITELIST: [&str; 2] = ["clippy", "rustfmt"];

if self.path.segments.len() > 1 {
sess.span_diagnostic.span_err(self.path.span, "expected ident, found path");
}
//sess.span_diagnostic.span_err(self.path.span, "expected ident, found path");

Ok(MetaItem {
name: self.path.segments.last().unwrap().identifier.name,
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
span: self.span,
})
let mut is_valid_tool = false;

for wl_name in WHITELIST.iter() {
if *wl_name == self.path.segments[0].identifier.name.as_str() {
is_valid_tool = true;
}
}

if !is_valid_tool {
sess.span_diagnostic.span_err(self.path.span, "Invalid tool name");
}

Ok(MetaItem {
name: Name::intern(&self.path.segments.iter().map(|s| s.identifier.name.as_str().to_string()).collect::<Vec<String>>().join("::")),
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
span: self.span,
})

} else {
Ok(MetaItem {
name: self.path.segments.last().unwrap().identifier.name,
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
span: self.span,
})
}
}

/// Convert self to a normal #[doc="foo"] comment, if it is a
Expand Down

0 comments on commit e188848

Please sign in to comment.