Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
// Functions dealing with attributes and meta items
|
2011-06-28 15:25:20 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::StabilityLevel::*;
|
|
|
|
pub use self::ReprAttr::*;
|
|
|
|
pub use self::IntType::*;
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2016-08-19 18:58:14 -07:00
|
|
|
use ast::{AttrId, Attribute, Attribute_};
|
|
|
|
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
|
|
|
|
use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
|
|
|
|
use codemap::{respan, spanned, dummy_spanned};
|
2016-07-15 13:13:17 -07:00
|
|
|
use syntax_pos::{Span, BytePos, DUMMY_SP};
|
2015-12-14 11:17:55 +13:00
|
|
|
use errors::Handler;
|
2016-06-11 01:37:24 +00:00
|
|
|
use feature_gate::{Features, GatedCfg};
|
2014-05-21 16:57:31 -07:00
|
|
|
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
2015-12-12 03:29:35 +00:00
|
|
|
use parse::token::InternedString;
|
2016-06-11 01:37:24 +00:00
|
|
|
use parse::{ParseSess, token};
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2016-06-18 04:01:57 +00:00
|
|
|
use util::ThinVec;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
use std::cell::{RefCell, Cell};
|
|
|
|
use std::collections::HashSet;
|
2012-03-02 13:14:10 -08:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
thread_local! {
|
2016-11-08 08:30:26 +10:30
|
|
|
static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
|
|
|
|
static KNOWN_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
|
2015-08-11 17:27:05 -07:00
|
|
|
}
|
2014-05-20 00:07:24 -07:00
|
|
|
|
2016-06-28 19:40:40 +02:00
|
|
|
enum AttrError {
|
|
|
|
MultipleItem(InternedString),
|
|
|
|
UnknownMetaItem(InternedString),
|
|
|
|
MissingSince,
|
|
|
|
MissingFeature,
|
|
|
|
MultipleStabilityLevels,
|
2016-08-19 18:58:14 -07:00
|
|
|
UnsupportedLiteral
|
2016-06-28 19:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
|
|
|
|
match error {
|
|
|
|
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
|
|
|
|
"multiple '{}' items", item),
|
|
|
|
AttrError::UnknownMetaItem(item) => span_err!(diag, span, E0541,
|
|
|
|
"unknown meta item '{}'", item),
|
|
|
|
AttrError::MissingSince => span_err!(diag, span, E0542, "missing 'since'"),
|
|
|
|
AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
|
|
|
|
AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
|
|
|
|
"multiple stability levels"),
|
2016-08-19 18:58:14 -07:00
|
|
|
AttrError::UnsupportedLiteral => span_err!(diag, span, E0565, "unsupported literal"),
|
2016-06-28 19:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:07:24 -07:00
|
|
|
pub fn mark_used(attr: &Attribute) {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("Marking {:?} as used.", attr);
|
2014-05-23 08:39:26 -07:00
|
|
|
let AttrId(id) = attr.node.id;
|
2015-08-11 17:27:05 -07:00
|
|
|
USED_ATTRS.with(|slot| {
|
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
|
|
|
if slot.borrow().len() <= idx {
|
|
|
|
slot.borrow_mut().resize(idx + 1, 0);
|
|
|
|
}
|
|
|
|
slot.borrow_mut()[idx] |= 1 << shift;
|
|
|
|
});
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_used(attr: &Attribute) -> bool {
|
2014-05-23 08:39:26 -07:00
|
|
|
let AttrId(id) = attr.node.id;
|
2015-08-11 17:27:05 -07:00
|
|
|
USED_ATTRS.with(|slot| {
|
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
|
|
|
slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
2016-11-08 08:30:26 +10:30
|
|
|
pub fn mark_known(attr: &Attribute) {
|
|
|
|
debug!("Marking {:?} as known.", attr);
|
|
|
|
let AttrId(id) = attr.node.id;
|
|
|
|
KNOWN_ATTRS.with(|slot| {
|
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
|
|
|
if slot.borrow().len() <= idx {
|
|
|
|
slot.borrow_mut().resize(idx + 1, 0);
|
|
|
|
}
|
|
|
|
slot.borrow_mut()[idx] |= 1 << shift;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_known(attr: &Attribute) -> bool {
|
|
|
|
let AttrId(id) = attr.node.id;
|
|
|
|
KNOWN_ATTRS.with(|slot| {
|
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
|
|
|
slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
/// Returns true if this list item is a MetaItem with a name of `name`.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
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
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn name(&self) -> Option<InternedString> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| Some(meta_item.name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the string value if self is a MetaItem and the MetaItem is a
|
|
|
|
/// MetaItemKind::NameValue variant containing a string, otherwise None.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn value_str(&self) -> Option<InternedString> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| meta_item.value_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a MetaItem if self is a MetaItem with Kind Word.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn word(&self) -> Option<&P<MetaItem>> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| if meta_item.is_word() {
|
|
|
|
Some(meta_item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a list of inner meta items from a list MetaItem type.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the variant is MetaItem.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_meta_item(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the variant is Literal.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_literal(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.literal().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if self is a MetaItem and the meta item is a word.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.word().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if self is a MetaItem and the meta item is a ValueString.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if self is a MetaItem and the meta item is a list.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item_list().is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl Attribute {
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2015-02-18 14:48:57 -05:00
|
|
|
let matches = name == &self.name()[..];
|
2014-05-23 08:39:26 -07:00
|
|
|
if matches {
|
2014-05-20 22:07:42 -07:00
|
|
|
mark_used(self);
|
|
|
|
}
|
2014-05-23 08:39:26 -07:00
|
|
|
matches
|
2014-05-20 22:07:42 -07:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn name(&self) -> InternedString { self.meta().name() }
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn value_str(&self) -> Option<InternedString> {
|
2014-01-10 14:02:36 -08:00
|
|
|
self.meta().value_str()
|
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2016-07-15 13:13:17 -07:00
|
|
|
self.meta().meta_item_list()
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_word(&self) -> bool { self.meta().is_word() }
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span { self.meta().span }
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
|
|
|
self.meta_item_list().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if the attribute is a Value String.
|
|
|
|
pub fn is_value_str(&self) -> bool {
|
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
2011-06-28 11:24:24 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl MetaItem {
|
|
|
|
pub fn name(&self) -> InternedString {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-02-09 12:05:20 +01:00
|
|
|
MetaItemKind::Word(ref n) => (*n).clone(),
|
|
|
|
MetaItemKind::NameValue(ref n, _) => (*n).clone(),
|
|
|
|
MetaItemKind::List(ref n, _) => (*n).clone(),
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn value_str(&self) -> Option<InternedString> {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-02-09 12:05:20 +01:00
|
|
|
MetaItemKind::NameValue(_, ref v) => {
|
2013-07-19 21:51:37 +10:00
|
|
|
match v.node {
|
2016-02-08 17:06:20 +01:00
|
|
|
ast::LitKind::Str(ref s, _) => Some((*s).clone()),
|
2013-07-19 21:51:37 +10:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-02-09 12:05:20 +01:00
|
|
|
MetaItemKind::List(_, ref l) => Some(&l[..]),
|
2013-07-19 21:51:37 +10:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2016-07-15 13:13:17 -07:00
|
|
|
match self.node {
|
|
|
|
MetaItemKind::Word(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn span(&self) -> Span { self.span }
|
|
|
|
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
|
|
|
name == &self.name()[..]
|
|
|
|
}
|
2012-01-26 16:23:34 -08:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
|
|
|
self.meta_item_list().is_some()
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:39:04 +00:00
|
|
|
impl Attribute {
|
2013-07-19 21:51:37 +10:00
|
|
|
/// Extract the MetaItem from inside this Attribute.
|
2016-08-23 03:39:04 +00:00
|
|
|
pub fn meta(&self) -> &MetaItem {
|
2016-02-08 23:55:55 +01:00
|
|
|
&self.node.value
|
2011-06-28 11:24:24 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
/// Convert self to a normal #[doc="foo"] comment, if it is a
|
|
|
|
/// comment like `///` or `/** */`. (Returns self unchanged for
|
|
|
|
/// non-sugared doc attributes.)
|
2016-08-23 03:39:04 +00:00
|
|
|
pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
|
2014-12-08 13:28:32 -05:00
|
|
|
F: FnOnce(&Attribute) -> T,
|
|
|
|
{
|
2013-07-19 21:51:37 +10:00
|
|
|
if self.node.is_sugared_doc {
|
2013-08-04 01:59:24 +02:00
|
|
|
let comment = self.value_str().unwrap();
|
2014-01-10 14:02:36 -08:00
|
|
|
let meta = mk_name_value_item_str(
|
|
|
|
InternedString::new("doc"),
|
2015-01-07 11:58:31 -05:00
|
|
|
token::intern_and_get_ident(&strip_doc_comment_decoration(
|
2015-02-18 14:48:57 -05:00
|
|
|
&comment)));
|
2015-10-01 18:03:34 +02:00
|
|
|
if self.node.style == ast::AttrStyle::Outer {
|
2014-09-13 19:06:01 +03:00
|
|
|
f(&mk_attr_outer(self.node.id, meta))
|
2014-05-20 21:25:42 -07:00
|
|
|
} else {
|
2014-09-13 19:06:01 +03:00
|
|
|
f(&mk_attr_inner(self.node.id, meta))
|
2014-05-20 21:25:42 -07:00
|
|
|
}
|
2013-07-19 21:51:37 +10:00
|
|
|
} else {
|
2014-09-13 19:06:01 +03:00
|
|
|
f(self)
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
2011-07-05 17:01:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
/* Constructors */
|
2011-10-29 17:35:45 -07:00
|
|
|
|
2014-01-10 14:02:36 -08:00
|
|
|
pub fn mk_name_value_item_str(name: InternedString, value: InternedString)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> P<MetaItem> {
|
2016-02-09 18:01:08 +01:00
|
|
|
let value_lit = dummy_spanned(ast::LitKind::Str(value, ast::StrStyle::Cooked));
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_name_value_item(DUMMY_SP, name, value_lit)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2011-06-28 12:53:59 -07:00
|
|
|
|
2014-01-08 10:35:15 -08:00
|
|
|
pub fn mk_name_value_item(name: InternedString, value: ast::Lit)
|
2014-09-13 19:06:01 +03:00
|
|
|
-> P<MetaItem> {
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_name_value_item(DUMMY_SP, name, value)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
pub fn mk_list_item(name: InternedString, items: Vec<NestedMetaItem>) -> P<MetaItem> {
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_list_item(DUMMY_SP, name, items)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
pub fn mk_list_word_item(name: InternedString) -> ast::NestedMetaItem {
|
|
|
|
dummy_spanned(NestedMetaItemKind::MetaItem(mk_spanned_word_item(DUMMY_SP, name)))
|
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn mk_word_item(name: InternedString) -> P<MetaItem> {
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_word_item(DUMMY_SP, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_spanned_name_value_item(sp: Span, name: InternedString, value: ast::Lit)
|
|
|
|
-> P<MetaItem> {
|
2016-07-17 21:45:06 -07:00
|
|
|
P(respan(sp, MetaItemKind::NameValue(name, value)))
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
pub fn mk_spanned_list_item(sp: Span, name: InternedString, items: Vec<NestedMetaItem>)
|
2016-07-15 13:13:17 -07:00
|
|
|
-> P<MetaItem> {
|
|
|
|
P(respan(sp, MetaItemKind::List(name, items)))
|
2011-06-28 12:53:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-15 13:13:17 -07:00
|
|
|
pub fn mk_spanned_word_item(sp: Span, name: InternedString) -> P<MetaItem> {
|
2016-07-17 21:45:06 -07:00
|
|
|
P(respan(sp, MetaItemKind::Word(name)))
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-17 23:33:05 +00:00
|
|
|
thread_local! { static NEXT_ATTR_ID: Cell<usize> = Cell::new(0) }
|
2014-05-20 00:07:24 -07:00
|
|
|
|
|
|
|
pub fn mk_attr_id() -> AttrId {
|
2014-11-14 14:20:57 -08:00
|
|
|
let id = NEXT_ATTR_ID.with(|slot| {
|
|
|
|
let r = slot.get();
|
|
|
|
slot.set(r + 1);
|
|
|
|
r
|
|
|
|
});
|
2014-05-20 00:07:24 -07:00
|
|
|
AttrId(id)
|
|
|
|
}
|
|
|
|
|
2014-05-20 21:25:42 -07:00
|
|
|
/// Returns an inner attribute with the given value.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn mk_attr_inner(id: AttrId, item: P<MetaItem>) -> Attribute {
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_attr_inner(DUMMY_SP, id, item)
|
2011-06-28 12:53:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-15 13:13:17 -07:00
|
|
|
/// Returns an innter attribute with the given value and span.
|
|
|
|
pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: P<MetaItem>) -> Attribute {
|
|
|
|
respan(sp,
|
|
|
|
Attribute_ {
|
|
|
|
id: id,
|
|
|
|
style: ast::AttrStyle::Inner,
|
|
|
|
value: item,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 21:25:42 -07:00
|
|
|
/// Returns an outer attribute with the given value.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn mk_attr_outer(id: AttrId, item: P<MetaItem>) -> Attribute {
|
2016-07-15 13:13:17 -07:00
|
|
|
mk_spanned_attr_outer(DUMMY_SP, id, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an outer attribute with the given value and span.
|
|
|
|
pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: P<MetaItem>) -> Attribute {
|
|
|
|
respan(sp,
|
|
|
|
Attribute_ {
|
|
|
|
id: id,
|
|
|
|
style: ast::AttrStyle::Outer,
|
|
|
|
value: item,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_doc_attr_outer(id: AttrId, item: P<MetaItem>, is_sugared_doc: bool) -> Attribute {
|
2014-05-20 21:25:42 -07:00
|
|
|
dummy_spanned(Attribute_ {
|
2014-05-20 00:07:24 -07:00
|
|
|
id: id,
|
2015-10-01 18:03:34 +02:00
|
|
|
style: ast::AttrStyle::Outer,
|
2014-05-20 21:25:42 -07:00
|
|
|
value: item,
|
2016-07-15 13:13:17 -07:00
|
|
|
is_sugared_doc: is_sugared_doc,
|
2014-05-20 21:25:42 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:07:24 -07:00
|
|
|
pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos,
|
|
|
|
hi: BytePos)
|
2014-01-10 14:02:36 -08:00
|
|
|
-> Attribute {
|
2015-02-04 21:48:12 +01:00
|
|
|
let style = doc_comment_style(&text);
|
2016-02-09 18:01:08 +01:00
|
|
|
let lit = spanned(lo, hi, ast::LitKind::Str(text, ast::StrStyle::Cooked));
|
2013-07-19 21:51:37 +10:00
|
|
|
let attr = Attribute_ {
|
2014-05-20 00:07:24 -07:00
|
|
|
id: id,
|
2013-07-19 21:51:37 +10:00
|
|
|
style: style,
|
2016-02-09 12:05:20 +01:00
|
|
|
value: P(spanned(lo, hi, MetaItemKind::NameValue(InternedString::new("doc"), lit))),
|
2013-07-19 21:51:37 +10:00
|
|
|
is_sugared_doc: true
|
|
|
|
};
|
|
|
|
spanned(lo, hi, attr)
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
/* Searching */
|
|
|
|
/// Check if `needle` occurs in `haystack` by a structural
|
|
|
|
/// comparison. This is slightly subtle, and relies on ignoring the
|
|
|
|
/// span included in the `==` comparison a plain MetaItem.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn contains(haystack: &[P<MetaItem>], needle: &MetaItem) -> bool {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("attr::contains (name={})", needle.name());
|
2013-11-20 16:23:04 -08:00
|
|
|
haystack.iter().any(|item| {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(" testing: {}", item.name());
|
2013-07-19 21:51:37 +10:00
|
|
|
item.node == needle.node
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("attr::list_contains_name (name={})", name);
|
|
|
|
items.iter().any(|item| {
|
|
|
|
debug!(" testing: {:?}", item.name());
|
|
|
|
item.check_name(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn contains_name(attrs: &[Attribute], name: &str) -> bool {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("attr::contains_name (name={})", name);
|
2016-08-23 03:54:53 +00:00
|
|
|
attrs.iter().any(|item| {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(" testing: {}", item.name());
|
2014-05-21 00:05:45 -07:00
|
|
|
item.check_name(name)
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
|
2014-01-10 14:02:36 -08:00
|
|
|
-> Option<InternedString> {
|
2013-07-19 21:51:37 +10:00
|
|
|
attrs.iter()
|
2014-05-21 00:05:45 -07:00
|
|
|
.find(|at| at.check_name(name))
|
2013-09-11 12:52:17 -07:00
|
|
|
.and_then(|at| at.value_str())
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn last_meta_item_value_str_by_name(items: &[P<MetaItem>], name: &str)
|
2014-01-10 14:02:36 -08:00
|
|
|
-> Option<InternedString> {
|
Deprecate the rev_iter pattern in all places where a DoubleEndedIterator is provided (everywhere but treemap)
This commit deprecates rev_iter, mut_rev_iter, move_rev_iter everywhere (except treemap) and also
deprecates related functions like rsplit, rev_components, and rev_str_components. In every case,
these functions can be replaced with the non-reversed form followed by a call to .rev(). To make this
more concrete, a translation table for all functional changes necessary follows:
* container.rev_iter() -> container.iter().rev()
* container.mut_rev_iter() -> container.mut_iter().rev()
* container.move_rev_iter() -> container.move_iter().rev()
* sliceorstr.rsplit(sep) -> sliceorstr.split(sep).rev()
* path.rev_components() -> path.components().rev()
* path.rev_str_components() -> path.str_components().rev()
In terms of the type system, this change also deprecates any specialized reversed iterator types (except
in treemap), opting instead to use Rev directly if any type annotations are needed. However, since
methods directly returning reversed iterators are now discouraged, the need for such annotations should
be small. However, in those cases, the general pattern for conversion is to take whatever follows Rev in
the original reversed name and surround it with Rev<>:
* RevComponents<'a> -> Rev<Components<'a>>
* RevStrComponents<'a> -> Rev<StrComponents<'a>>
* RevItems<'a, T> -> Rev<Items<'a, T>>
* etc.
The reasoning behind this change is that it makes the standard API much simpler without reducing readability,
performance, or power. The presence of functions such as rev_iter adds more boilerplate code to libraries
(all of which simply call .iter().rev()), clutters up the documentation, and only helps code by saving two
characters. Additionally, the numerous type synonyms that were used to make the type signatures look nice
like RevItems add even more boilerplate and clutter up the docs even more. With this change, all that cruft
goes away.
[breaking-change]
2014-04-20 23:59:12 -05:00
|
|
|
items.iter()
|
|
|
|
.rev()
|
2014-05-21 00:05:45 -07:00
|
|
|
.find(|mi| mi.check_name(name))
|
2014-01-08 10:35:15 -08:00
|
|
|
.and_then(|i| i.value_str())
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Higher-level applications */
|
|
|
|
|
2014-06-06 13:21:18 -07:00
|
|
|
pub fn find_crate_name(attrs: &[Attribute]) -> Option<InternedString> {
|
|
|
|
first_attr_value_str_by_name(attrs, "crate_name")
|
2013-12-09 14:56:53 -07:00
|
|
|
}
|
|
|
|
|
2015-02-27 13:34:33 +02:00
|
|
|
/// Find the value of #[export_name=*] attribute and check its validity.
|
2016-01-15 14:43:14 -05:00
|
|
|
pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<InternedString> {
|
2015-02-27 13:34:33 +02:00
|
|
|
attrs.iter().fold(None, |ia,attr| {
|
|
|
|
if attr.check_name("export_name") {
|
|
|
|
if let s@Some(_) = attr.value_str() {
|
|
|
|
s
|
|
|
|
} else {
|
2016-07-11 23:27:27 +02:00
|
|
|
struct_span_err!(diag, attr.span, E0558,
|
2016-06-28 19:40:40 +02:00
|
|
|
"export_name attribute has invalid format")
|
2016-09-02 02:21:53 +05:30
|
|
|
.span_label(attr.span,
|
|
|
|
&format!("did you mean #[export_name=\"*\"]?"))
|
|
|
|
.emit();
|
2015-02-27 13:34:33 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ia
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:43:14 -05:00
|
|
|
pub fn contains_extern_indicator(diag: &Handler, attrs: &[Attribute]) -> bool {
|
2015-11-07 12:22:04 -05:00
|
|
|
contains_name(attrs, "no_mangle") ||
|
2016-01-15 14:43:14 -05:00
|
|
|
find_export_name_attr(diag, attrs).is_some()
|
2015-11-07 12:22:04 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:59 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2013-07-19 21:51:37 +10:00
|
|
|
pub enum InlineAttr {
|
2015-02-28 13:56:32 +01:00
|
|
|
None,
|
|
|
|
Hint,
|
|
|
|
Always,
|
|
|
|
Never,
|
2012-01-16 21:12:08 -08:00
|
|
|
}
|
|
|
|
|
2014-08-01 12:27:12 -07:00
|
|
|
/// Determine what `#[inline]` attribute is present in `attrs`, if any.
|
2015-12-14 11:17:55 +13:00
|
|
|
pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr {
|
2015-02-28 13:56:32 +01:00
|
|
|
attrs.iter().fold(InlineAttr::None, |ia,attr| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match attr.node.value.node {
|
2016-04-26 16:27:10 +02:00
|
|
|
MetaItemKind::Word(ref n) if n == "inline" => {
|
2014-05-21 00:05:45 -07:00
|
|
|
mark_used(attr);
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::Hint
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2016-04-26 16:27:10 +02:00
|
|
|
MetaItemKind::List(ref n, ref items) if n == "inline" => {
|
2014-05-21 00:05:45 -07:00
|
|
|
mark_used(attr);
|
2015-02-18 20:30:17 +02:00
|
|
|
if items.len() != 1 {
|
2016-06-28 19:40:40 +02:00
|
|
|
diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::None
|
2016-08-19 18:58:14 -07:00
|
|
|
} else if list_contains_name(&items[..], "always") {
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::Always
|
2016-08-19 18:58:14 -07:00
|
|
|
} else if list_contains_name(&items[..], "never") {
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::Never
|
2014-05-21 00:05:45 -07:00
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
diagnostic.map(|d| {
|
2016-08-19 18:58:14 -07:00
|
|
|
span_err!(d, items[0].span, E0535, "invalid argument");
|
2016-06-28 19:40:40 +02:00
|
|
|
});
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::None
|
2014-05-21 00:05:45 -07:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 16:34:47 +02:00
|
|
|
_ => ia,
|
2012-01-16 21:12:08 -08:00
|
|
|
}
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-01-16 21:12:08 -08:00
|
|
|
}
|
|
|
|
|
2014-08-01 12:27:12 -07:00
|
|
|
/// True if `#[inline]` or `#[inline(always)]` is present in `attrs`.
|
|
|
|
pub fn requests_inline(attrs: &[Attribute]) -> bool {
|
2015-02-18 20:30:17 +02:00
|
|
|
match find_inline_attr(None, attrs) {
|
2015-02-28 13:56:32 +01:00
|
|
|
InlineAttr::Hint | InlineAttr::Always => true,
|
|
|
|
InlineAttr::None | InlineAttr::Never => false,
|
2014-08-01 12:27:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 20:22:57 -07:00
|
|
|
/// Tests if a cfg-pattern matches the cfg set
|
2016-10-27 06:36:56 +00:00
|
|
|
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
|
2014-09-24 20:22:57 -07:00
|
|
|
match cfg.node {
|
2016-08-19 18:58:14 -07:00
|
|
|
ast::MetaItemKind::List(ref pred, ref mis) => {
|
|
|
|
for mi in mis.iter() {
|
|
|
|
if !mi.is_meta_item() {
|
|
|
|
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The unwraps below may look dangerous, but we've already asserted
|
|
|
|
// that they won't fail with the loop above.
|
|
|
|
match &pred[..] {
|
|
|
|
"any" => mis.iter().any(|mi| {
|
2016-10-27 06:36:56 +00:00
|
|
|
cfg_matches(mi.meta_item().unwrap(), sess, features)
|
2016-08-19 18:58:14 -07:00
|
|
|
}),
|
|
|
|
"all" => mis.iter().all(|mi| {
|
2016-10-27 06:36:56 +00:00
|
|
|
cfg_matches(mi.meta_item().unwrap(), sess, features)
|
2016-08-19 18:58:14 -07:00
|
|
|
}),
|
|
|
|
"not" => {
|
|
|
|
if mis.len() != 1 {
|
|
|
|
span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:36:56 +00:00
|
|
|
!cfg_matches(mis[0].meta_item().unwrap(), sess, features)
|
2016-08-19 18:58:14 -07:00
|
|
|
},
|
|
|
|
p => {
|
|
|
|
span_err!(sess.span_diagnostic, cfg.span, E0537, "invalid predicate `{}`", p);
|
|
|
|
false
|
|
|
|
}
|
2014-09-24 20:22:57 -07:00
|
|
|
}
|
|
|
|
},
|
2016-02-09 12:05:20 +01:00
|
|
|
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
|
|
|
|
gated_cfg.check_and_emit(sess, feats);
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
2016-10-27 06:36:56 +00:00
|
|
|
contains(&sess.config, cfg)
|
2015-07-13 17:10:44 -07:00
|
|
|
}
|
2014-09-24 20:22:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 16:11:20 +03:00
|
|
|
/// Represents the #[stable], #[unstable] and #[rustc_deprecated] attributes.
|
2015-05-26 00:41:27 +03:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, PartialEq, Eq, Hash)]
|
2013-08-31 17:13:57 +10:00
|
|
|
pub struct Stability {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub level: StabilityLevel,
|
2015-01-12 18:40:19 -08:00
|
|
|
pub feature: InternedString,
|
2015-12-04 19:34:28 +03:00
|
|
|
pub rustc_depr: Option<RustcDeprecation>,
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The available stability levels.
|
2015-10-13 06:01:31 +03:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
2013-08-31 17:13:57 +10:00
|
|
|
pub enum StabilityLevel {
|
2015-10-13 06:01:31 +03:00
|
|
|
// Reason for the current stability level and the relevant rust-lang issue
|
|
|
|
Unstable { reason: Option<InternedString>, issue: u32 },
|
|
|
|
Stable { since: InternedString },
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
2015-12-04 19:34:28 +03:00
|
|
|
pub struct RustcDeprecation {
|
2015-10-13 06:01:31 +03:00
|
|
|
pub since: InternedString,
|
|
|
|
pub reason: InternedString,
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
|
2015-12-04 19:34:28 +03:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub struct Deprecation {
|
|
|
|
pub since: Option<InternedString>,
|
|
|
|
pub note: Option<InternedString>,
|
|
|
|
}
|
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
impl StabilityLevel {
|
|
|
|
pub fn is_unstable(&self) -> bool { if let Unstable {..} = *self { true } else { false }}
|
|
|
|
pub fn is_stable(&self) -> bool { if let Stable {..} = *self { true } else { false }}
|
|
|
|
}
|
2015-01-22 12:33:46 -08:00
|
|
|
|
2015-12-14 11:17:55 +13:00
|
|
|
fn find_stability_generic<'a, I>(diagnostic: &Handler,
|
2015-10-13 06:01:31 +03:00
|
|
|
attrs_iter: I,
|
|
|
|
item_sp: Span)
|
|
|
|
-> Option<Stability>
|
|
|
|
where I: Iterator<Item = &'a Attribute>
|
|
|
|
{
|
2015-01-22 12:33:46 -08:00
|
|
|
let mut stab: Option<Stability> = None;
|
2015-12-04 19:34:28 +03:00
|
|
|
let mut rustc_depr: Option<RustcDeprecation> = None;
|
2015-01-22 12:33:46 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
'outer: for attr in attrs_iter {
|
2015-01-22 12:33:46 -08:00
|
|
|
let tag = attr.name();
|
2015-10-13 06:01:31 +03:00
|
|
|
let tag = &*tag;
|
2015-11-20 16:11:20 +03:00
|
|
|
if tag != "rustc_deprecated" && tag != "unstable" && tag != "stable" {
|
2015-01-22 12:33:46 -08:00
|
|
|
continue // not a stability level
|
|
|
|
}
|
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
mark_used(attr);
|
|
|
|
|
|
|
|
if let Some(metas) = attr.meta_item_list() {
|
|
|
|
let get = |meta: &MetaItem, item: &mut Option<InternedString>| {
|
|
|
|
if item.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
|
2015-10-13 06:01:31 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if let Some(v) = meta.value_str() {
|
|
|
|
*item = Some(v);
|
|
|
|
true
|
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
|
2015-10-13 06:01:31 +03:00
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match tag {
|
2015-11-20 16:11:20 +03:00
|
|
|
"rustc_deprecated" => {
|
2015-12-04 19:34:28 +03:00
|
|
|
if rustc_depr.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, item_sp, E0540,
|
|
|
|
"multiple rustc_deprecated attributes");
|
2015-10-13 06:01:31 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut since = None;
|
|
|
|
let mut reason = None;
|
|
|
|
for meta in metas {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let Some(mi) = meta.meta_item() {
|
|
|
|
match &*mi.name() {
|
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
"reason" => if !get(mi, &mut reason) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, mi.span,
|
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
|
|
|
continue 'outer
|
|
|
|
}
|
2015-01-12 18:40:19 -08:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
|
|
|
|
continue 'outer
|
2015-01-12 18:40:19 -08:00
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
match (since, reason) {
|
|
|
|
(Some(since), Some(reason)) => {
|
2015-12-04 19:34:28 +03:00
|
|
|
rustc_depr = Some(RustcDeprecation {
|
2015-10-13 06:01:31 +03:00
|
|
|
since: since,
|
|
|
|
reason: reason,
|
|
|
|
})
|
2015-01-12 18:40:19 -08:00
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
(None, _) => {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
2015-07-02 21:00:59 -07:00
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
_ => {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, attr.span(), E0543, "missing 'reason'");
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
2015-01-12 18:40:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
"unstable" => {
|
|
|
|
if stab.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
|
2015-10-13 06:01:31 +03:00
|
|
|
break
|
|
|
|
}
|
2015-01-12 18:40:19 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
let mut feature = None;
|
|
|
|
let mut reason = None;
|
|
|
|
let mut issue = None;
|
|
|
|
for meta in metas {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let Some(mi) = meta.meta_item() {
|
|
|
|
match &*mi.name() {
|
|
|
|
"feature" => if !get(mi, &mut feature) { continue 'outer },
|
|
|
|
"reason" => if !get(mi, &mut reason) { continue 'outer },
|
|
|
|
"issue" => if !get(mi, &mut issue) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, meta.span,
|
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
|
|
|
continue 'outer
|
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
|
|
|
|
continue 'outer
|
2015-10-13 06:01:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-01-12 18:40:19 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
match (feature, reason, issue) {
|
|
|
|
(Some(feature), reason, Some(issue)) => {
|
|
|
|
stab = Some(Stability {
|
|
|
|
level: Unstable {
|
|
|
|
reason: reason,
|
|
|
|
issue: {
|
|
|
|
if let Ok(issue) = issue.parse() {
|
|
|
|
issue
|
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, attr.span(), E0545,
|
|
|
|
"incorrect 'issue'");
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
feature: feature,
|
2015-12-04 19:34:28 +03:00
|
|
|
rustc_depr: None,
|
2015-10-13 06:01:31 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(None, _, _) => {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, attr.span(), E0547, "missing 'issue'");
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"stable" => {
|
|
|
|
if stab.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
|
2015-10-13 06:01:31 +03:00
|
|
|
break
|
|
|
|
}
|
2015-01-12 18:40:19 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
let mut feature = None;
|
|
|
|
let mut since = None;
|
|
|
|
for meta in metas {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
|
|
|
|
match &*mi.name() {
|
|
|
|
"feature" => if !get(mi, &mut feature) { continue 'outer },
|
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, meta.span,
|
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
|
|
|
continue 'outer
|
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
|
|
|
|
continue 'outer
|
2015-10-13 06:01:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 12:33:46 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
match (feature, since) {
|
|
|
|
(Some(feature), Some(since)) => {
|
|
|
|
stab = Some(Stability {
|
|
|
|
level: Stable {
|
|
|
|
since: since,
|
|
|
|
},
|
|
|
|
feature: feature,
|
2015-12-04 19:34:28 +03:00
|
|
|
rustc_depr: None,
|
2015-10-13 06:01:31 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(None, _) => {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 12:33:46 -08:00
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2015-10-13 06:01:31 +03:00
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, attr.span(), E0548, "incorrect stability attribute type");
|
2015-10-13 06:01:31 +03:00
|
|
|
continue
|
2015-01-22 12:33:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the deprecation info into the stability info
|
2015-12-04 19:34:28 +03:00
|
|
|
if let Some(rustc_depr) = rustc_depr {
|
2015-10-13 06:01:31 +03:00
|
|
|
if let Some(ref mut stab) = stab {
|
2015-12-04 19:34:28 +03:00
|
|
|
stab.rustc_depr = Some(rustc_depr);
|
2015-10-13 06:01:31 +03:00
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, item_sp, E0549,
|
|
|
|
"rustc_deprecated attribute must be paired with \
|
|
|
|
either stable or unstable attribute");
|
2015-01-22 12:33:46 -08:00
|
|
|
}
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
2015-01-22 12:33:46 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
stab
|
2013-08-31 17:13:57 +10:00
|
|
|
}
|
|
|
|
|
2015-12-14 11:17:55 +13:00
|
|
|
fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
|
|
|
|
attrs_iter: I,
|
|
|
|
item_sp: Span)
|
|
|
|
-> Option<Deprecation>
|
2015-12-04 19:34:28 +03:00
|
|
|
where I: Iterator<Item = &'a Attribute>
|
|
|
|
{
|
|
|
|
let mut depr: Option<Deprecation> = None;
|
|
|
|
|
|
|
|
'outer: for attr in attrs_iter {
|
|
|
|
if attr.name() != "deprecated" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mark_used(attr);
|
|
|
|
|
|
|
|
if depr.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
|
2015-12-04 19:34:28 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
depr = if let Some(metas) = attr.meta_item_list() {
|
|
|
|
let get = |meta: &MetaItem, item: &mut Option<InternedString>| {
|
|
|
|
if item.is_some() {
|
2016-06-28 19:40:40 +02:00
|
|
|
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
|
2015-12-04 19:34:28 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if let Some(v) = meta.value_str() {
|
|
|
|
*item = Some(v);
|
|
|
|
true
|
|
|
|
} else {
|
2016-06-28 19:40:40 +02:00
|
|
|
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
|
2015-12-04 19:34:28 +03:00
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut since = None;
|
|
|
|
let mut note = None;
|
|
|
|
for meta in metas {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
|
|
|
|
match &*mi.name() {
|
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
"note" => if !get(mi, &mut note) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, meta.span,
|
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
|
|
|
continue 'outer
|
|
|
|
}
|
2015-12-04 19:34:28 +03:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
|
|
|
|
continue 'outer
|
2015-12-04 19:34:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Deprecation {since: since, note: note})
|
|
|
|
} else {
|
|
|
|
Some(Deprecation{since: None, note: None})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
depr
|
|
|
|
}
|
|
|
|
|
2014-05-21 00:05:45 -07:00
|
|
|
/// Find the first stability attribute. `None` if none exists.
|
2015-12-14 11:17:55 +13:00
|
|
|
pub fn find_stability(diagnostic: &Handler, attrs: &[Attribute],
|
2015-01-22 12:33:46 -08:00
|
|
|
item_sp: Span) -> Option<Stability> {
|
2015-10-13 06:01:31 +03:00
|
|
|
find_stability_generic(diagnostic, attrs.iter(), item_sp)
|
2014-05-21 00:05:45 -07:00
|
|
|
}
|
|
|
|
|
2015-12-04 19:34:28 +03:00
|
|
|
/// Find the deprecation attribute. `None` if none exists.
|
2015-12-14 11:17:55 +13:00
|
|
|
pub fn find_deprecation(diagnostic: &Handler, attrs: &[Attribute],
|
|
|
|
item_sp: Span) -> Option<Deprecation> {
|
2015-12-04 19:34:28 +03:00
|
|
|
find_deprecation_generic(diagnostic, attrs.iter(), item_sp)
|
|
|
|
}
|
|
|
|
|
2015-12-14 11:17:55 +13:00
|
|
|
pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
|
2013-04-03 09:28:36 -04:00
|
|
|
let mut set = HashSet::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for meta in metas {
|
2013-07-19 21:51:37 +10:00
|
|
|
let name = meta.name();
|
2012-06-07 20:12:05 -07:00
|
|
|
|
2014-01-08 10:35:15 -08:00
|
|
|
if !set.insert(name.clone()) {
|
2015-03-28 21:58:51 +00:00
|
|
|
panic!(diagnostic.span_fatal(meta.span,
|
2016-06-28 19:40:40 +02:00
|
|
|
&format!("duplicate meta item `{}`", name)));
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2012-01-16 21:12:08 -08:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 18:08:45 -04:00
|
|
|
|
|
|
|
|
2014-05-26 23:56:52 -07:00
|
|
|
/// Parse #[repr(...)] forms.
|
2014-06-09 13:12:30 -07:00
|
|
|
///
|
|
|
|
/// Valid repr contents: any of the primitive integral type names (see
|
2014-05-26 23:56:52 -07:00
|
|
|
/// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
|
|
|
|
/// the same discriminant size that the corresponding C enum would or C
|
|
|
|
/// structure layout, and `packed` to remove padding.
|
2015-12-14 11:17:55 +13:00
|
|
|
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
|
2014-05-26 23:56:52 -07:00
|
|
|
let mut acc = Vec::new();
|
2014-05-21 00:05:45 -07:00
|
|
|
match attr.node.value.node {
|
2016-04-26 16:27:10 +02:00
|
|
|
ast::MetaItemKind::List(ref s, ref items) if s == "repr" => {
|
2014-05-21 00:05:45 -07:00
|
|
|
mark_used(attr);
|
2015-01-31 12:20:46 -05:00
|
|
|
for item in items {
|
2016-08-19 18:58:14 -07:00
|
|
|
if !item.is_meta_item() {
|
|
|
|
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral);
|
|
|
|
continue
|
|
|
|
}
|
2014-05-26 23:56:52 -07:00
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
if let Some(mi) = item.word() {
|
|
|
|
let word = &*mi.name();
|
|
|
|
let hint = match word {
|
|
|
|
// Can't use "extern" because it's not a lexical identifier.
|
|
|
|
"C" => Some(ReprExtern),
|
|
|
|
"packed" => Some(ReprPacked),
|
|
|
|
"simd" => Some(ReprSimd),
|
|
|
|
_ => match int_type_of_word(word) {
|
2016-08-31 14:00:29 +03:00
|
|
|
Some(ity) => Some(ReprInt(ity)),
|
2016-08-19 18:58:14 -07:00
|
|
|
None => {
|
|
|
|
// Not a word we recognize
|
|
|
|
span_err!(diagnostic, item.span, E0552,
|
|
|
|
"unrecognized representation hint");
|
|
|
|
None
|
|
|
|
}
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
};
|
|
|
|
|
2016-07-03 15:24:27 +12:00
|
|
|
if let Some(h) = hint {
|
|
|
|
acc.push(h);
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, item.span, E0553,
|
|
|
|
"unrecognized enum representation hint");
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not a "repr" hint: ignore.
|
|
|
|
_ => { }
|
|
|
|
}
|
2014-03-16 20:56:24 +02:00
|
|
|
acc
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn int_type_of_word(s: &str) -> Option<IntType> {
|
|
|
|
match s {
|
2016-02-08 16:20:57 +01:00
|
|
|
"i8" => Some(SignedInt(ast::IntTy::I8)),
|
|
|
|
"u8" => Some(UnsignedInt(ast::UintTy::U8)),
|
|
|
|
"i16" => Some(SignedInt(ast::IntTy::I16)),
|
|
|
|
"u16" => Some(UnsignedInt(ast::UintTy::U16)),
|
|
|
|
"i32" => Some(SignedInt(ast::IntTy::I32)),
|
|
|
|
"u32" => Some(UnsignedInt(ast::UintTy::U32)),
|
|
|
|
"i64" => Some(SignedInt(ast::IntTy::I64)),
|
|
|
|
"u64" => Some(UnsignedInt(ast::UintTy::U64)),
|
|
|
|
"isize" => Some(SignedInt(ast::IntTy::Is)),
|
|
|
|
"usize" => Some(UnsignedInt(ast::UintTy::Us)),
|
2013-05-24 18:08:45 -04:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:59 -04:00
|
|
|
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
|
2013-05-24 18:08:45 -04:00
|
|
|
pub enum ReprAttr {
|
|
|
|
ReprAny,
|
2016-08-31 14:00:29 +03:00
|
|
|
ReprInt(IntType),
|
2014-05-26 23:56:52 -07:00
|
|
|
ReprExtern,
|
|
|
|
ReprPacked,
|
2015-07-13 11:35:00 -07:00
|
|
|
ReprSimd,
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
|
2013-06-02 13:03:35 -07:00
|
|
|
impl ReprAttr {
|
|
|
|
pub fn is_ffi_safe(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
ReprAny => false,
|
2016-08-31 14:00:29 +03:00
|
|
|
ReprInt(ity) => ity.is_ffi_safe(),
|
2014-05-26 23:56:52 -07:00
|
|
|
ReprExtern => true,
|
2015-07-13 11:35:00 -07:00
|
|
|
ReprPacked => false,
|
|
|
|
ReprSimd => true,
|
2013-06-02 13:03:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:59 -04:00
|
|
|
#[derive(Eq, Hash, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
|
2013-05-24 18:08:45 -04:00
|
|
|
pub enum IntType {
|
2014-01-09 15:05:33 +02:00
|
|
|
SignedInt(ast::IntTy),
|
|
|
|
UnsignedInt(ast::UintTy)
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntType {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_signed(self) -> bool {
|
|
|
|
match self {
|
2013-11-28 12:22:53 -08:00
|
|
|
SignedInt(..) => true,
|
|
|
|
UnsignedInt(..) => false
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-02 13:03:35 -07:00
|
|
|
fn is_ffi_safe(self) -> bool {
|
|
|
|
match self {
|
2016-02-08 16:20:57 +01:00
|
|
|
SignedInt(ast::IntTy::I8) | UnsignedInt(ast::UintTy::U8) |
|
|
|
|
SignedInt(ast::IntTy::I16) | UnsignedInt(ast::UintTy::U16) |
|
|
|
|
SignedInt(ast::IntTy::I32) | UnsignedInt(ast::UintTy::U32) |
|
|
|
|
SignedInt(ast::IntTy::I64) | UnsignedInt(ast::UintTy::U64) => true,
|
|
|
|
SignedInt(ast::IntTy::Is) | UnsignedInt(ast::UintTy::Us) => false
|
2013-06-02 13:03:35 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
2015-11-03 17:39:51 +01:00
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
pub trait HasAttrs: Sized {
|
|
|
|
fn attrs(&self) -> &[ast::Attribute];
|
|
|
|
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasAttrs for Vec<Attribute> {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
&self
|
|
|
|
}
|
|
|
|
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
|
|
|
|
f(self)
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
impl HasAttrs for ThinVec<Attribute> {
|
2016-05-18 07:25:44 +00:00
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2016-06-18 04:01:57 +00:00
|
|
|
&self
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
|
2016-06-18 04:01:57 +00:00
|
|
|
f(self.into()).into()
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl<T: HasAttrs + 'static> HasAttrs for P<T> {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
(**self).attrs()
|
|
|
|
}
|
|
|
|
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
|
|
|
|
self.map(|t| t.map_attrs(f))
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for StmtKind {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
match *self {
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Local(ref local) => local.attrs(),
|
2016-06-14 06:48:24 +00:00
|
|
|
StmtKind::Item(..) => &[],
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
|
|
|
|
StmtKind::Mac(ref mac) => {
|
|
|
|
let (_, _, ref attrs) = **mac;
|
|
|
|
attrs.attrs()
|
|
|
|
}
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
|
|
|
|
match self {
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
|
2016-06-14 06:48:24 +00:00
|
|
|
StmtKind::Item(..) => self,
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Expr(expr) => StmtKind::Expr(expr.map_attrs(f)),
|
|
|
|
StmtKind::Semi(expr) => StmtKind::Semi(expr.map_attrs(f)),
|
|
|
|
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, style, attrs)| {
|
|
|
|
(mac, style, attrs.map_attrs(f))
|
|
|
|
})),
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! derive_has_attrs_from_field {
|
|
|
|
($($ty:path),*) => { derive_has_attrs_from_field!($($ty: .attrs),*); };
|
|
|
|
($($ty:path : $(.$field:ident)*),*) => { $(
|
|
|
|
impl HasAttrs for $ty {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
self $(.$field)* .attrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_attrs<F>(mut self, f: F) -> Self
|
|
|
|
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
|
|
|
|
{
|
|
|
|
self $(.$field)* = self $(.$field)* .map_attrs(f);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
|
|
|
derive_has_attrs_from_field! {
|
|
|
|
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:30:01 +00:00
|
|
|
derive_has_attrs_from_field! { Stmt: .node, ast::Variant: .node.attrs }
|