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;
|
2017-03-03 09:23:59 +00:00
|
|
|
use ast::{AttrId, Attribute, Name, Ident};
|
2016-08-19 18:58:14 -07:00
|
|
|
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
|
2017-03-08 23:13:35 +00:00
|
|
|
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind};
|
2017-03-15 00:22:48 +00:00
|
|
|
use codemap::{Spanned, respan, dummy_spanned};
|
2018-03-24 21:17:27 +03:00
|
|
|
use syntax_pos::Span;
|
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};
|
2017-03-03 09:23:59 +00:00
|
|
|
use parse::parser::Parser;
|
|
|
|
use parse::{self, ParseSess, PResult};
|
|
|
|
use parse::token::{self, Token};
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2016-11-16 10:52:37 +00:00
|
|
|
use symbol::Symbol;
|
2017-03-03 09:23:59 +00:00
|
|
|
use tokenstream::{TokenStream, TokenTree, Delimited};
|
2016-06-18 04:01:57 +00:00
|
|
|
use util::ThinVec;
|
2018-03-07 02:44:10 +01:00
|
|
|
use GLOBALS;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
use std::iter;
|
2012-03-02 13:14:10 -08:00
|
|
|
|
2016-06-28 19:40:40 +02:00
|
|
|
enum AttrError {
|
2016-11-15 04:34:52 +00:00
|
|
|
MultipleItem(Name),
|
|
|
|
UnknownMetaItem(Name),
|
2016-06-28 19:40:40 +02:00
|
|
|
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);
|
2016-11-14 12:00:25 +00:00
|
|
|
let AttrId(id) = attr.id;
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
|
|
|
let mut slot = globals.used_attrs.lock();
|
2015-08-11 17:27:05 -07:00
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
2018-03-07 02:44:10 +01:00
|
|
|
if slot.len() <= idx {
|
|
|
|
slot.resize(idx + 1, 0);
|
2015-08-11 17:27:05 -07:00
|
|
|
}
|
2018-03-07 02:44:10 +01:00
|
|
|
slot[idx] |= 1 << shift;
|
2015-08-11 17:27:05 -07:00
|
|
|
});
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_used(attr: &Attribute) -> bool {
|
2016-11-14 12:00:25 +00:00
|
|
|
let AttrId(id) = attr.id;
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
|
|
|
let slot = globals.used_attrs.lock();
|
2015-08-11 17:27:05 -07:00
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
2018-03-07 02:44:10 +01:00
|
|
|
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
|
2015-08-11 17:27:05 -07:00
|
|
|
.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);
|
2016-11-14 12:00:25 +00:00
|
|
|
let AttrId(id) = attr.id;
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
|
|
|
let mut slot = globals.known_attrs.lock();
|
2016-11-08 08:30:26 +10:30
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
2018-03-07 02:44:10 +01:00
|
|
|
if slot.len() <= idx {
|
|
|
|
slot.resize(idx + 1, 0);
|
2016-11-08 08:30:26 +10:30
|
|
|
}
|
2018-03-07 02:44:10 +01:00
|
|
|
slot[idx] |= 1 << shift;
|
2016-11-08 08:30:26 +10:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_known(attr: &Attribute) -> bool {
|
2016-11-14 12:00:25 +00:00
|
|
|
let AttrId(id) = attr.id;
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
|
|
|
let slot = globals.known_attrs.lock();
|
2016-11-08 08:30:26 +10:30
|
|
|
let idx = (id / 64) as usize;
|
|
|
|
let shift = id % 64;
|
2018-03-07 02:44:10 +01:00
|
|
|
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
|
2016-11-08 08:30:26 +10:30
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:28:55 +09:00
|
|
|
const RUST_KNOWN_TOOL: &[&str] = &["clippy", "rustfmt"];
|
|
|
|
|
|
|
|
pub fn is_known_tool(attr: &Attribute) -> bool {
|
2018-02-04 15:36:26 +09:00
|
|
|
let tool_name =
|
|
|
|
attr.path.segments.iter().next().expect("empty path in attribute").identifier.name;
|
|
|
|
RUST_KNOWN_TOOL.contains(&tool_name.as_str().as_ref())
|
2018-01-30 19:28:55 +09:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
impl NestedMetaItem {
|
|
|
|
/// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn meta_item(&self) -> Option<&MetaItem> {
|
2016-08-23 03:21:17 +00:00
|
|
|
match self.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
NestedMetaItemKind::MetaItem(ref item) => Some(item),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Lit if self is a NestedMetaItemKind::Literal.
|
|
|
|
pub fn literal(&self) -> Option<&Lit> {
|
|
|
|
match self.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
NestedMetaItemKind::Literal(ref lit) => Some(lit),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => 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-11-15 04:34:52 +00:00
|
|
|
pub fn name(&self) -> Option<Name> {
|
2018-01-30 14:30:39 +09:00
|
|
|
self.meta_item().and_then(|meta_item| Some(meta_item.name()))
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the string value if self is a MetaItem and the MetaItem is a
|
|
|
|
/// MetaItemKind::NameValue variant containing a string, otherwise None.
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| meta_item.value_str())
|
|
|
|
}
|
|
|
|
|
2017-01-15 09:49:29 +11:00
|
|
|
/// Returns a name and single literal value tuple of the MetaItem.
|
|
|
|
pub fn name_value_literal(&self) -> Option<(Name, &Lit)> {
|
|
|
|
self.meta_item().and_then(
|
|
|
|
|meta_item| meta_item.meta_item_list().and_then(
|
|
|
|
|meta_item_list| {
|
|
|
|
if meta_item_list.len() == 1 {
|
|
|
|
let nested_item = &meta_item_list[0];
|
|
|
|
if nested_item.is_literal() {
|
2018-01-30 14:30:39 +09:00
|
|
|
Some((meta_item.name(), nested_item.literal().unwrap()))
|
2017-01-15 09:49:29 +11:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}}))
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
/// Returns a MetaItem if self is a MetaItem with Kind Word.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn word(&self) -> Option<&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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 14:30:39 +09:00
|
|
|
fn name_from_path(path: &ast::Path) -> Name {
|
2018-02-04 15:36:26 +09:00
|
|
|
path.segments.last().expect("empty path in attribute").identifier.name
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl Attribute {
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2017-03-03 09:23:59 +00:00
|
|
|
let matches = self.path == 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
|
|
|
|
2018-02-04 15:36:26 +09:00
|
|
|
/// Returns the **last** segment of the name of this attribute.
|
|
|
|
/// E.g. `foo` for `#[foo]`, `skip` for `#[rustfmt::skip]`.
|
2018-01-30 14:53:01 +09:00
|
|
|
pub fn name(&self) -> Name {
|
|
|
|
name_from_path(&self.path)
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2017-03-03 09:23:59 +00:00
|
|
|
self.meta().and_then(|meta| meta.value_str())
|
2014-01-10 14:02:36 -08:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
|
|
|
|
match self.meta() {
|
|
|
|
Some(MetaItem { node: MetaItemKind::List(list), .. }) => Some(list),
|
|
|
|
_ => None
|
|
|
|
}
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
|
|
|
self.path.segments.len() == 1 && self.tokens.is_empty()
|
|
|
|
}
|
2016-08-23 03:54:53 +00:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.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()
|
|
|
|
}
|
2018-01-30 19:28:55 +09:00
|
|
|
|
|
|
|
pub fn is_scoped(&self) -> bool {
|
|
|
|
self.path.segments.len() > 1
|
|
|
|
}
|
2011-06-28 11:24:24 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl MetaItem {
|
2018-01-30 14:30:39 +09:00
|
|
|
pub fn name(&self) -> Name {
|
|
|
|
name_from_path(&self.name)
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::NameValue(ref v) => {
|
2013-07-19 21:51:37 +10:00
|
|
|
match v.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Str(ref s, _) => Some(*s),
|
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-11-15 07:37:10 +00: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 {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::Word => true,
|
2016-07-15 13:13:17 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn span(&self) -> Span { self.span }
|
|
|
|
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2018-01-30 14:30:39 +09:00
|
|
|
self.name() == name
|
2016-08-23 03:54:53 +00:00
|
|
|
}
|
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.
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn meta(&self) -> Option<MetaItem> {
|
|
|
|
let mut tokens = self.tokens.trees().peekable();
|
|
|
|
Some(MetaItem {
|
2018-01-30 14:30:39 +09:00
|
|
|
name: self.path.clone(),
|
2017-03-03 09:23:59 +00:00
|
|
|
node: if let Some(node) = MetaItemKind::from_tokens(&mut tokens) {
|
|
|
|
if tokens.peek().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
node
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
},
|
|
|
|
span: self.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
{
|
2017-05-18 10:37:24 +12:00
|
|
|
let mut parser = Parser::new(sess, self.tokens.clone(), None, false, false);
|
2017-03-08 23:13:35 +00:00
|
|
|
let result = f(&mut parser)?;
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
parser.unexpected()?;
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_list<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, Vec<T>>
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
{
|
|
|
|
if self.tokens.is_empty() {
|
|
|
|
return Ok(Vec::new());
|
|
|
|
}
|
|
|
|
self.parse(sess, |parser| {
|
|
|
|
parser.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let mut list = Vec::new();
|
|
|
|
while !parser.eat(&token::CloseDelim(token::Paren)) {
|
|
|
|
list.push(f(parser)?);
|
|
|
|
if !parser.eat(&token::Comma) {
|
|
|
|
parser.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
|
|
|
|
Ok(MetaItem {
|
2018-01-30 14:30:39 +09:00
|
|
|
name: self.path.clone(),
|
2017-03-08 23:13:35 +00:00
|
|
|
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
|
2017-03-03 09:23:59 +00:00
|
|
|
span: self.span,
|
|
|
|
})
|
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,
|
|
|
|
{
|
2016-11-14 12:00:25 +00:00
|
|
|
if self.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(
|
2018-03-24 21:17:27 +03:00
|
|
|
Ident::from_str("doc"),
|
|
|
|
dummy_spanned(Symbol::intern(&strip_doc_comment_decoration(&comment.as_str()))));
|
2017-09-23 11:58:06 -05:00
|
|
|
let mut attr = if self.style == ast::AttrStyle::Outer {
|
|
|
|
mk_attr_outer(self.span, self.id, meta)
|
2014-05-20 21:25:42 -07:00
|
|
|
} else {
|
2017-09-23 11:58:06 -05:00
|
|
|
mk_attr_inner(self.span, self.id, meta)
|
|
|
|
};
|
|
|
|
attr.is_sugared_doc = true;
|
|
|
|
f(&attr)
|
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
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_name_value_item_str(ident: Ident, value: Spanned<Symbol>) -> MetaItem {
|
|
|
|
let value = respan(value.span, LitKind::Str(value.node, ast::StrStyle::Cooked));
|
|
|
|
mk_name_value_item(ident.span.to(value.span), ident, value)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2011-06-28 12:53:59 -07:00
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_name_value_item(span: Span, ident: Ident, value: ast::Lit) -> MetaItem {
|
|
|
|
MetaItem { ident, span, node: MetaItemKind::NameValue(value) }
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_list_item(span: Span, ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
|
|
|
|
MetaItem { ident, span, node: MetaItemKind::List(items) }
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_word_item(ident: Ident) -> MetaItem {
|
|
|
|
MetaItem { ident, span: ident.span, node: MetaItemKind::Word }
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
2018-01-30 14:30:39 +09:00
|
|
|
|
|
|
|
pub fn mk_word_item(name: Name) -> MetaItem {
|
|
|
|
mk_spanned_word_item(DUMMY_SP, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mk_spanned_meta_item {
|
|
|
|
($sp:ident, $name:ident, $node:expr) => {
|
|
|
|
MetaItem {
|
|
|
|
span: $sp,
|
|
|
|
name: ast::Path::from_ident($sp, ast::Ident::with_empty_ctxt($name)),
|
|
|
|
node: $node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> MetaItem {
|
|
|
|
mk_spanned_meta_item!(sp, name, MetaItemKind::NameValue(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> MetaItem {
|
|
|
|
mk_spanned_meta_item!(sp, name, MetaItemKind::List(items))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_spanned_word_item(sp: Span, name: Name) -> MetaItem {
|
|
|
|
mk_spanned_meta_item!(sp, name, MetaItemKind::Word)
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
pub fn mk_attr_id() -> AttrId {
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0);
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
assert!(id != ::std::usize::MAX);
|
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.
|
2017-01-17 23:54:51 +01:00
|
|
|
pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute {
|
|
|
|
mk_spanned_attr_inner(span, id, item)
|
2011-06-28 12:53:59 -07:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:16:18 +02:00
|
|
|
/// Returns an inner attribute with the given value and span.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
2016-11-14 12:00:25 +00:00
|
|
|
style: ast::AttrStyle::Inner,
|
2018-01-30 14:30:39 +09:00
|
|
|
path: item.name,
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: item.node.tokens(item.span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: false,
|
|
|
|
span: sp,
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 21:25:42 -07:00
|
|
|
/// Returns an outer attribute with the given value.
|
2017-01-17 23:54:51 +01:00
|
|
|
pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute {
|
|
|
|
mk_spanned_attr_outer(span, id, item)
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an outer attribute with the given value and span.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
2016-11-14 12:00:25 +00:00
|
|
|
style: ast::AttrStyle::Outer,
|
2018-01-30 14:30:39 +09:00
|
|
|
path: item.name,
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: item.node.tokens(item.span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: false,
|
|
|
|
span: sp,
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-03-15 00:22:48 +00:00
|
|
|
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
|
2016-11-16 10:52:37 +00:00
|
|
|
let style = doc_comment_style(&text.as_str());
|
2017-03-15 00:22:48 +00:00
|
|
|
let lit = respan(span, LitKind::Str(text, ast::StrStyle::Cooked));
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
|
|
|
style,
|
2018-03-24 21:17:27 +03:00
|
|
|
path: ast::Path::from_ident(Ident::from_str("doc").with_span_pos(span)),
|
2017-03-15 00:22:48 +00:00
|
|
|
tokens: MetaItemKind::NameValue(lit).tokens(span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: true,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-11-14 12:00:25 +00:00
|
|
|
}
|
2011-07-13 18:13:19 -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
|
|
|
items.iter().any(|item| {
|
|
|
|
item.check_name(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn contains_name(attrs: &[Attribute], name: &str) -> bool {
|
|
|
|
attrs.iter().any(|item| {
|
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
|
|
|
}
|
|
|
|
|
2017-08-26 18:00:33 -07:00
|
|
|
pub fn find_by_name<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a Attribute> {
|
|
|
|
attrs.iter().find(|attr| attr.check_name(name))
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option<Symbol> {
|
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
|
|
|
}
|
|
|
|
|
2017-09-20 20:42:49 +02:00
|
|
|
/// Check if `attrs` contains an attribute like `#![feature(feature_name)]`.
|
|
|
|
/// This will not perform any "sanity checks" on the form of the attributes.
|
|
|
|
pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
|
|
|
|
attrs.iter().any(|item| {
|
|
|
|
item.check_name("feature") &&
|
|
|
|
item.meta_item_list().map(|list| {
|
|
|
|
list.iter().any(|mi| {
|
2018-01-30 14:30:39 +09:00
|
|
|
mi.word().map(|w| w.name() == feature_name)
|
2017-09-20 20:42:49 +02:00
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
}).unwrap_or(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-04-15 01:07:47 -07:00
|
|
|
/* Higher-level applications */
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
|
2014-06-06 13:21:18 -07:00
|
|
|
first_attr_value_str_by_name(attrs, "crate_name")
|
2013-12-09 14:56:53 -07:00
|
|
|
}
|
|
|
|
|
2018-01-30 22:39:23 -05:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, RustcEncodable, RustcDecodable)]
|
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
|
|
|
}
|
|
|
|
|
2018-02-20 13:49:54 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub enum UnwindAttr {
|
|
|
|
Allowed,
|
|
|
|
Aborts,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine what `#[unwind]` attribute is present in `attrs`, if any.
|
|
|
|
pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Option<UnwindAttr> {
|
|
|
|
let syntax_error = |attr: &Attribute| {
|
|
|
|
mark_used(attr);
|
|
|
|
diagnostic.map(|d| {
|
|
|
|
span_err!(d, attr.span, E0633, "malformed `#[unwind]` attribute");
|
|
|
|
});
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
attrs.iter().fold(None, |ia, attr| {
|
|
|
|
if attr.path != "unwind" {
|
|
|
|
return ia;
|
|
|
|
}
|
|
|
|
let meta = match attr.meta() {
|
|
|
|
Some(meta) => meta.node,
|
|
|
|
None => return ia,
|
|
|
|
};
|
|
|
|
match meta {
|
|
|
|
MetaItemKind::Word => {
|
|
|
|
syntax_error(attr)
|
|
|
|
}
|
|
|
|
MetaItemKind::List(ref items) => {
|
|
|
|
mark_used(attr);
|
|
|
|
if items.len() != 1 {
|
|
|
|
syntax_error(attr)
|
|
|
|
} else if list_contains_name(&items[..], "allowed") {
|
|
|
|
Some(UnwindAttr::Allowed)
|
|
|
|
} else if list_contains_name(&items[..], "aborts") {
|
|
|
|
Some(UnwindAttr::Aborts)
|
|
|
|
} else {
|
|
|
|
syntax_error(attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ia,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-08-30 23:40:43 +03:00
|
|
|
eval_condition(cfg, sess, &mut |cfg| {
|
|
|
|
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
|
|
|
|
gated_cfg.check_and_emit(sess, feats);
|
|
|
|
}
|
2018-01-30 14:30:39 +09:00
|
|
|
sess.config.contains(&(cfg.name(), cfg.value_str()))
|
2017-08-30 23:40:43 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
|
|
|
|
/// evaluate individual items.
|
|
|
|
pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
|
|
|
|
-> bool
|
|
|
|
where F: FnMut(&ast::MetaItem) -> bool
|
|
|
|
{
|
2014-09-24 20:22:57 -07:00
|
|
|
match cfg.node {
|
2016-11-15 07:37:10 +00:00
|
|
|
ast::MetaItemKind::List(ref mis) => {
|
2016-08-19 18:58:14 -07:00
|
|
|
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.
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*cfg.name().as_str() {
|
2016-08-19 18:58:14 -07:00
|
|
|
"any" => mis.iter().any(|mi| {
|
2017-08-30 23:40:43 +03:00
|
|
|
eval_condition(mi.meta_item().unwrap(), sess, eval)
|
2016-08-19 18:58:14 -07:00
|
|
|
}),
|
|
|
|
"all" => mis.iter().all(|mi| {
|
2017-08-30 23:40:43 +03:00
|
|
|
eval_condition(mi.meta_item().unwrap(), sess, eval)
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-30 23:40:43 +03:00
|
|
|
!eval_condition(mis[0].meta_item().unwrap(), sess, eval)
|
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-11-15 07:37:10 +00:00
|
|
|
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
|
2017-08-30 23:40:43 +03:00
|
|
|
eval(cfg)
|
2015-07-13 17:10:44 -07:00
|
|
|
}
|
2014-09-24 20:22:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:11:30 +00:00
|
|
|
/// Represents the #[stable], #[unstable], #[rustc_{deprecated,const_unstable}] 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,
|
2016-11-16 10:52:37 +00:00
|
|
|
pub feature: Symbol,
|
2015-12-04 19:34:28 +03:00
|
|
|
pub rustc_depr: Option<RustcDeprecation>,
|
2017-09-08 18:11:30 +00:00
|
|
|
pub rustc_const_unstable: Option<RustcConstUnstable>,
|
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
|
2016-11-16 10:52:37 +00:00
|
|
|
Unstable { reason: Option<Symbol>, issue: u32 },
|
|
|
|
Stable { since: Symbol },
|
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 {
|
2016-11-16 10:52:37 +00:00
|
|
|
pub since: Symbol,
|
|
|
|
pub reason: Symbol,
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
|
2017-09-08 18:11:30 +00:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub struct RustcConstUnstable {
|
|
|
|
pub feature: Symbol,
|
|
|
|
}
|
|
|
|
|
2015-12-04 19:34:28 +03:00
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub struct Deprecation {
|
2016-11-16 10:52:37 +00:00
|
|
|
pub since: Option<Symbol>,
|
|
|
|
pub note: Option<Symbol>,
|
2015-12-04 19:34:28 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2017-09-08 18:11:30 +00:00
|
|
|
let mut rustc_const_unstable: Option<RustcConstUnstable> = None;
|
2015-01-22 12:33:46 -08:00
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
'outer: for attr in attrs_iter {
|
2017-09-08 18:11:30 +00:00
|
|
|
if ![
|
|
|
|
"rustc_deprecated",
|
|
|
|
"rustc_const_unstable",
|
|
|
|
"unstable",
|
|
|
|
"stable",
|
|
|
|
].iter().any(|&s| attr.path == s) {
|
2015-01-22 12:33:46 -08:00
|
|
|
continue // not a stability level
|
|
|
|
}
|
|
|
|
|
2015-10-13 06:01:31 +03:00
|
|
|
mark_used(attr);
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
let meta = attr.meta();
|
|
|
|
if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta {
|
|
|
|
let meta = meta.as_ref().unwrap();
|
2016-11-16 10:52:37 +00:00
|
|
|
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
2015-10-13 06:01:31 +03:00
|
|
|
if item.is_some() {
|
2018-01-30 14:30:39 +09: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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-08 18:11:30 +00:00
|
|
|
macro_rules! get_meta {
|
|
|
|
($($name:ident),+) => {
|
|
|
|
$(
|
|
|
|
let mut $name = None;
|
|
|
|
)+
|
2015-10-13 06:01:31 +03:00
|
|
|
for meta in metas {
|
2016-08-19 18:58:14 -07:00
|
|
|
if let Some(mi) = meta.meta_item() {
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*mi.name().as_str() {
|
2017-09-08 18:11:30 +00:00
|
|
|
$(
|
|
|
|
stringify!($name)
|
|
|
|
=> if !get(mi, &mut $name) { continue 'outer },
|
|
|
|
)+
|
2016-08-19 18:58:14 -07:00
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, mi.span,
|
2018-01-30 14:30:39 +09:00
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
2016-08-19 18:58:14 -07:00
|
|
|
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
|
|
|
}
|
2017-09-08 18:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*meta.name().as_str() {
|
2017-09-08 18:11:30 +00:00
|
|
|
"rustc_deprecated" => {
|
|
|
|
if rustc_depr.is_some() {
|
|
|
|
span_err!(diagnostic, item_sp, E0540,
|
|
|
|
"multiple rustc_deprecated attributes");
|
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
|
|
|
|
get_meta!(since, reason);
|
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 {
|
2017-08-06 22:54:09 -07:00
|
|
|
since,
|
|
|
|
reason,
|
2015-10-13 06:01:31 +03:00
|
|
|
})
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 18:11:30 +00:00
|
|
|
"rustc_const_unstable" => {
|
|
|
|
if rustc_const_unstable.is_some() {
|
|
|
|
span_err!(diagnostic, item_sp, E0553,
|
|
|
|
"multiple rustc_const_unstable attributes");
|
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
|
|
|
|
get_meta!(feature);
|
|
|
|
if let Some(feature) = feature {
|
|
|
|
rustc_const_unstable = Some(RustcConstUnstable {
|
|
|
|
feature
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, attr.span(), E0629, "missing 'feature'");
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
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() {
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*mi.name().as_str() {
|
2016-08-19 18:58:14 -07:00
|
|
|
"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,
|
2018-01-30 14:30:39 +09:00
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
2016-08-19 18:58:14 -07:00
|
|
|
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 {
|
2017-08-06 22:54:09 -07:00
|
|
|
reason,
|
2015-10-13 06:01:31 +03:00
|
|
|
issue: {
|
2016-11-16 10:52:37 +00:00
|
|
|
if let Ok(issue) = issue.as_str().parse() {
|
2015-10-13 06:01:31 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-08-06 22:54:09 -07:00
|
|
|
feature,
|
2015-12-04 19:34:28 +03:00
|
|
|
rustc_depr: None,
|
2017-09-08 18:11:30 +00:00
|
|
|
rustc_const_unstable: 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 {
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*mi.name().as_str() {
|
2016-08-19 18:58:14 -07:00
|
|
|
"feature" => if !get(mi, &mut feature) { continue 'outer },
|
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, meta.span,
|
2018-01-30 14:30:39 +09:00
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
2016-08-19 18:58:14 -07:00
|
|
|
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 {
|
2017-08-06 22:54:09 -07:00
|
|
|
since,
|
2015-10-13 06:01:31 +03:00
|
|
|
},
|
2017-08-06 22:54:09 -07:00
|
|
|
feature,
|
2015-12-04 19:34:28 +03:00
|
|
|
rustc_depr: None,
|
2017-09-08 18:11:30 +00:00
|
|
|
rustc_const_unstable: 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
|
|
|
|
2017-09-08 18:11:30 +00:00
|
|
|
// Merge the const-unstable info into the stability info
|
|
|
|
if let Some(rustc_const_unstable) = rustc_const_unstable {
|
|
|
|
if let Some(ref mut stab) = stab {
|
|
|
|
stab.rustc_const_unstable = Some(rustc_const_unstable);
|
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, item_sp, E0630,
|
|
|
|
"rustc_const_unstable attribute must be paired with \
|
|
|
|
either stable or unstable attribute");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-03-03 09:23:59 +00:00
|
|
|
if attr.path != "deprecated" {
|
2015-12-04 19:34:28 +03:00
|
|
|
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() {
|
2016-11-16 10:52:37 +00:00
|
|
|
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
2015-12-04 19:34:28 +03:00
|
|
|
if item.is_some() {
|
2018-01-30 14:30:39 +09: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 {
|
2018-01-30 14:30:39 +09:00
|
|
|
match &*mi.name().as_str() {
|
2016-08-19 18:58:14 -07:00
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
"note" => if !get(mi, &mut note) { continue 'outer },
|
|
|
|
_ => {
|
|
|
|
handle_errors(diagnostic, meta.span,
|
2018-01-30 14:30:39 +09:00
|
|
|
AttrError::UnknownMetaItem(mi.name()));
|
2016-08-19 18:58:14 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
2018-01-03 17:43:30 +01:00
|
|
|
/// structure layout, `packed` to remove padding, and `transparent` to elegate representation
|
|
|
|
/// concerns to the only non-ZST field.
|
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();
|
2017-03-03 09:23:59 +00:00
|
|
|
if attr.path == "repr" {
|
|
|
|
if let Some(items) = attr.meta_item_list() {
|
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
|
|
|
|
2017-01-15 09:49:29 +11:00
|
|
|
let mut recognised = false;
|
2016-08-19 18:58:14 -07:00
|
|
|
if let Some(mi) = item.word() {
|
2018-01-30 14:30:39 +09:00
|
|
|
let word = &*mi.name().as_str();
|
2016-08-19 18:58:14 -07:00
|
|
|
let hint = match word {
|
2018-01-07 22:05:32 +01:00
|
|
|
"C" => Some(ReprC),
|
2018-02-04 22:10:28 +11:00
|
|
|
"packed" => Some(ReprPacked(1)),
|
2016-08-19 18:58:14 -07:00
|
|
|
"simd" => Some(ReprSimd),
|
2018-01-03 17:43:30 +01:00
|
|
|
"transparent" => Some(ReprTransparent),
|
2016-08-19 18:58:14 -07:00
|
|
|
_ => 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 => {
|
|
|
|
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 {
|
2017-01-15 09:49:29 +11:00
|
|
|
recognised = true;
|
2016-07-03 15:24:27 +12:00
|
|
|
acc.push(h);
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
2017-01-15 09:49:29 +11:00
|
|
|
} else if let Some((name, value)) = item.name_value_literal() {
|
2018-02-04 22:10:28 +11:00
|
|
|
let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
|
|
|
|
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
|
|
|
|
if literal.is_power_of_two() {
|
2017-07-08 10:28:20 -04:00
|
|
|
// rustc::ty::layout::Align restricts align to <= 2147483647
|
2018-02-04 22:10:28 +11:00
|
|
|
if *literal <= 2147483647 {
|
|
|
|
Ok(*literal as u32)
|
2017-03-25 19:00:49 +11:00
|
|
|
} else {
|
2018-02-04 22:10:28 +11:00
|
|
|
Err("larger than 2147483647")
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
2017-03-25 19:00:49 +11:00
|
|
|
} else {
|
2018-02-04 22:10:28 +11:00
|
|
|
Err("not a power of two")
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
2017-03-25 19:00:49 +11:00
|
|
|
} else {
|
2018-02-04 22:10:28 +11:00
|
|
|
Err("not an unsuffixed integer")
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
2018-02-04 22:10:28 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut literal_error = None;
|
|
|
|
if name == "align" {
|
|
|
|
recognised = true;
|
|
|
|
match parse_alignment(&value.node) {
|
|
|
|
Ok(literal) => acc.push(ReprAlign(literal)),
|
|
|
|
Err(message) => literal_error = Some(message)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if name == "packed" {
|
|
|
|
recognised = true;
|
|
|
|
match parse_alignment(&value.node) {
|
|
|
|
Ok(literal) => acc.push(ReprPacked(literal)),
|
|
|
|
Err(message) => literal_error = Some(message)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if let Some(literal_error) = literal_error {
|
|
|
|
span_err!(diagnostic, item.span, E0589,
|
|
|
|
"invalid `repr(align)` attribute: {}", literal_error);
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
2018-04-29 18:42:43 +01:00
|
|
|
} else {
|
|
|
|
if let Some(meta_item) = item.meta_item() {
|
|
|
|
if meta_item.ident.name == "align" {
|
|
|
|
if let MetaItemKind::NameValue(ref value) = meta_item.node {
|
|
|
|
recognised = true;
|
|
|
|
let mut err = struct_span_err!(diagnostic, item.span, E0693,
|
|
|
|
"incorrect `repr(align)` attribute format");
|
|
|
|
match value.node {
|
|
|
|
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
|
|
|
|
err.span_suggestion(item.span,
|
|
|
|
"use parentheses instead",
|
|
|
|
format!("align({})", int));
|
|
|
|
}
|
|
|
|
ast::LitKind::Str(s, _) => {
|
|
|
|
err.span_suggestion(item.span,
|
|
|
|
"use parentheses instead",
|
|
|
|
format!("align({})", s));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
|
|
|
if !recognised {
|
|
|
|
// Not a word we recognize
|
|
|
|
span_err!(diagnostic, item.span, E0552,
|
|
|
|
"unrecognized representation hint");
|
2013-05-24 18:08:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)),
|
2016-08-23 03:56:52 +03:00
|
|
|
"i128" => Some(SignedInt(ast::IntTy::I128)),
|
|
|
|
"u128" => Some(UnsignedInt(ast::UintTy::U128)),
|
2018-01-04 03:12:04 +02:00
|
|
|
"isize" => Some(SignedInt(ast::IntTy::Isize)),
|
|
|
|
"usize" => Some(UnsignedInt(ast::UintTy::Usize)),
|
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 {
|
2016-08-31 14:00:29 +03:00
|
|
|
ReprInt(IntType),
|
2018-01-07 22:05:32 +01:00
|
|
|
ReprC,
|
2018-02-04 22:10:28 +11:00
|
|
|
ReprPacked(u32),
|
2015-07-13 11:35:00 -07:00
|
|
|
ReprSimd,
|
2018-01-03 17:43:30 +01:00
|
|
|
ReprTransparent,
|
2017-07-06 18:00:46 -04:00
|
|
|
ReprAlign(u32),
|
2013-05-24 18:08:45 -04: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 17:39:51 +01:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
impl MetaItem {
|
|
|
|
fn tokens(&self) -> TokenStream {
|
2018-01-30 14:30:39 +09:00
|
|
|
let mut idents = vec![];
|
|
|
|
let mut last_pos = BytePos(0 as u32);
|
2018-02-04 15:36:26 +09:00
|
|
|
// FIXME: Share code with `parse_path`.
|
2018-01-30 14:30:39 +09:00
|
|
|
for (i, segment) in self.name.segments.iter().enumerate() {
|
|
|
|
let is_first = i == 0;
|
|
|
|
if !is_first {
|
|
|
|
let mod_sep_span = Span::new(last_pos, segment.span.lo(), segment.span.ctxt());
|
|
|
|
idents.push(TokenTree::Token(mod_sep_span, Token::ModSep).into());
|
|
|
|
}
|
|
|
|
idents.push(TokenTree::Token(segment.span, Token::Ident(segment.identifier)).into());
|
|
|
|
last_pos = segment.span.hi();
|
|
|
|
}
|
|
|
|
idents.push(self.node.tokens(self.span));
|
|
|
|
TokenStream::concat(idents)
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
|
|
|
|
where I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
2018-01-30 14:30:39 +09:00
|
|
|
let name = match tokens.next() {
|
|
|
|
Some(TokenTree::Token(span, Token::Ident(ident))) => {
|
|
|
|
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
|
|
|
|
tokens.next();
|
|
|
|
let mut segments = vec![];
|
|
|
|
loop {
|
|
|
|
if let Some(TokenTree::Token(span, Token::Ident(ident))) = tokens.next() {
|
|
|
|
segments.push(ast::PathSegment::from_ident(ident, span));
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
|
|
|
|
tokens.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::Path { span, segments }
|
|
|
|
} else {
|
|
|
|
ast::Path::from_ident(span, ident)
|
|
|
|
}
|
|
|
|
}
|
2017-03-29 01:55:01 +00:00
|
|
|
Some(TokenTree::Token(_, Token::Interpolated(ref nt))) => match nt.0 {
|
2018-01-30 14:30:39 +09:00
|
|
|
token::Nonterminal::NtIdent(ident) => {
|
|
|
|
ast::Path::from_ident(ident.span, ident.node)
|
|
|
|
}
|
2017-03-29 07:17:18 +00:00
|
|
|
token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()),
|
2018-01-30 14:30:39 +09:00
|
|
|
token::Nonterminal::NtPath(ref path) => path.clone(),
|
2017-03-29 07:17:18 +00:00
|
|
|
_ => return None,
|
2017-03-08 23:13:35 +00:00
|
|
|
},
|
2017-03-03 09:23:59 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
2017-07-31 23:04:34 +03:00
|
|
|
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
|
2017-12-08 17:32:04 -08:00
|
|
|
let node = MetaItemKind::from_tokens(tokens)?;
|
2017-07-31 23:04:34 +03:00
|
|
|
let hi = match node {
|
|
|
|
MetaItemKind::NameValue(ref lit) => lit.span.hi(),
|
2018-01-30 14:30:39 +09:00
|
|
|
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(name.span.hi()),
|
|
|
|
_ => name.span.hi(),
|
2017-08-17 21:58:01 +09:00
|
|
|
};
|
2018-01-30 14:30:39 +09:00
|
|
|
let span = name.span.with_hi(hi);
|
|
|
|
Some(MetaItem { name, node, span })
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MetaItemKind {
|
|
|
|
pub fn tokens(&self, span: Span) -> TokenStream {
|
|
|
|
match *self {
|
|
|
|
MetaItemKind::Word => TokenStream::empty(),
|
|
|
|
MetaItemKind::NameValue(ref lit) => {
|
|
|
|
TokenStream::concat(vec![TokenTree::Token(span, Token::Eq).into(), lit.tokens()])
|
|
|
|
}
|
|
|
|
MetaItemKind::List(ref list) => {
|
|
|
|
let mut tokens = Vec::new();
|
|
|
|
for (i, item) in list.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
tokens.push(TokenTree::Token(span, Token::Comma).into());
|
|
|
|
}
|
|
|
|
tokens.push(item.node.tokens());
|
|
|
|
}
|
|
|
|
TokenTree::Delimited(span, Delimited {
|
|
|
|
delim: token::Paren,
|
|
|
|
tts: TokenStream::concat(tokens).into(),
|
|
|
|
}).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItemKind>
|
|
|
|
where I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
|
|
|
let delimited = match tokens.peek().cloned() {
|
|
|
|
Some(TokenTree::Token(_, token::Eq)) => {
|
|
|
|
tokens.next();
|
|
|
|
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
|
|
|
|
LitKind::from_token(token)
|
|
|
|
.map(|lit| MetaItemKind::NameValue(Spanned { node: lit, span: span }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Some(TokenTree::Delimited(_, ref delimited)) if delimited.delim == token::Paren => {
|
|
|
|
tokens.next();
|
|
|
|
delimited.stream()
|
|
|
|
}
|
|
|
|
_ => return Some(MetaItemKind::Word),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tokens = delimited.into_trees().peekable();
|
|
|
|
let mut result = Vec::new();
|
|
|
|
while let Some(..) = tokens.peek() {
|
2017-12-08 17:32:04 -08:00
|
|
|
let item = NestedMetaItemKind::from_tokens(&mut tokens)?;
|
|
|
|
result.push(respan(item.span(), item));
|
2017-03-03 09:23:59 +00:00
|
|
|
match tokens.next() {
|
|
|
|
None | Some(TokenTree::Token(_, Token::Comma)) => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(MetaItemKind::List(result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NestedMetaItemKind {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
NestedMetaItemKind::MetaItem(ref item) => item.span,
|
|
|
|
NestedMetaItemKind::Literal(ref lit) => lit.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tokens(&self) -> TokenStream {
|
|
|
|
match *self {
|
|
|
|
NestedMetaItemKind::MetaItem(ref item) => item.tokens(),
|
|
|
|
NestedMetaItemKind::Literal(ref lit) => lit.tokens(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItemKind>
|
|
|
|
where I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
|
|
|
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
|
|
|
|
if let Some(node) = LitKind::from_token(token) {
|
|
|
|
tokens.next();
|
2017-08-17 21:24:08 +09:00
|
|
|
return Some(NestedMetaItemKind::Literal(respan(span, node)));
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaItem::from_tokens(tokens).map(NestedMetaItemKind::MetaItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lit {
|
|
|
|
fn tokens(&self) -> TokenStream {
|
|
|
|
TokenTree::Token(self.span, self.node.token()).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LitKind {
|
|
|
|
fn token(&self) -> Token {
|
|
|
|
use std::ascii;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
LitKind::Str(string, ast::StrStyle::Cooked) => {
|
|
|
|
let mut escaped = String::new();
|
|
|
|
for ch in string.as_str().chars() {
|
|
|
|
escaped.extend(ch.escape_unicode());
|
|
|
|
}
|
|
|
|
Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
|
|
|
|
}
|
|
|
|
LitKind::Str(string, ast::StrStyle::Raw(n)) => {
|
|
|
|
Token::Literal(token::Lit::StrRaw(string, n), None)
|
|
|
|
}
|
|
|
|
LitKind::ByteStr(ref bytes) => {
|
|
|
|
let string = bytes.iter().cloned().flat_map(ascii::escape_default)
|
|
|
|
.map(Into::<char>::into).collect::<String>();
|
|
|
|
Token::Literal(token::Lit::ByteStr(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Byte(byte) => {
|
|
|
|
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
|
|
|
|
Token::Literal(token::Lit::Byte(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Char(ch) => {
|
|
|
|
let string: String = ch.escape_default().map(Into::<char>::into).collect();
|
|
|
|
Token::Literal(token::Lit::Char(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Int(n, ty) => {
|
|
|
|
let suffix = match ty {
|
|
|
|
ast::LitIntType::Unsigned(ty) => Some(Symbol::intern(ty.ty_to_string())),
|
|
|
|
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
|
|
|
|
ast::LitIntType::Unsuffixed => None,
|
|
|
|
};
|
|
|
|
Token::Literal(token::Lit::Integer(Symbol::intern(&n.to_string())), suffix)
|
|
|
|
}
|
|
|
|
LitKind::Float(symbol, ty) => {
|
|
|
|
Token::Literal(token::Lit::Float(symbol), Some(Symbol::intern(ty.ty_to_string())))
|
|
|
|
}
|
|
|
|
LitKind::FloatUnsuffixed(symbol) => Token::Literal(token::Lit::Float(symbol), None),
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Bool(value) => Token::Ident(Ident::with_empty_ctxt(Symbol::intern(if value {
|
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
2018-03-09 23:56:40 -06:00
|
|
|
})), false),
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_token(token: Token) -> Option<LitKind> {
|
|
|
|
match token {
|
2018-03-09 23:56:40 -06:00
|
|
|
Token::Ident(ident, false) if ident.name == "true" => Some(LitKind::Bool(true)),
|
|
|
|
Token::Ident(ident, false) if ident.name == "false" => Some(LitKind::Bool(false)),
|
2017-03-29 01:55:01 +00:00
|
|
|
Token::Interpolated(ref nt) => match nt.0 {
|
2017-03-08 23:13:35 +00:00
|
|
|
token::NtExpr(ref v) => match v.node {
|
|
|
|
ExprKind::Lit(ref lit) => Some(lit.node.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
},
|
2017-03-03 09:23:59 +00:00
|
|
|
Token::Literal(lit, suf) => {
|
|
|
|
let (suffix_illegal, result) = parse::lit_token(lit, suf, None);
|
|
|
|
if suffix_illegal && suf.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
impl<T: HasAttrs> HasAttrs for Spanned<T> {
|
|
|
|
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
|
|
|
|
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
|
2017-08-17 21:24:08 +09:00
|
|
|
respan(self.span, self.node.map_attrs(f))
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for Vec<Attribute> {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2017-05-12 20:05:39 +02:00
|
|
|
self
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
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] {
|
2017-05-12 20:05:39 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
impl HasAttrs for Stmt {
|
|
|
|
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
|
|
|
|
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
|
|
|
|
Stmt { id: self.id, node: self.node.map_attrs(f), span: self.span }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! derive_has_attrs {
|
|
|
|
($($ty:path),*) => { $(
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for $ty {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2017-01-03 19:13:01 -08:00
|
|
|
&self.attrs
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn map_attrs<F>(mut self, f: F) -> Self
|
|
|
|
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
|
|
|
|
{
|
2017-01-03 19:13:01 -08:00
|
|
|
self.attrs = self.attrs.map_attrs(f);
|
2016-05-18 07:25:44 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
derive_has_attrs! {
|
|
|
|
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
|
|
|
|
ast::Field, ast::FieldPat, ast::Variant_
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|