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 18:48:01 -06: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 06:51:37 -05:00
|
|
|
// Functions dealing with attributes and meta items
|
2011-06-28 17:25:20 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2014-05-20 02:07:24 -05:00
|
|
|
use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaWord, MetaNameValue, MetaList};
|
2013-05-24 17:08:45 -05:00
|
|
|
use codemap::{Span, Spanned, spanned, dummy_spanned};
|
2012-11-15 21:37:29 -06:00
|
|
|
use codemap::BytePos;
|
2013-12-31 08:17:59 -06:00
|
|
|
use diagnostic::SpanHandler;
|
2014-05-21 18:57:31 -05:00
|
|
|
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
2014-01-08 12:35:15 -06:00
|
|
|
use parse::token::InternedString;
|
2014-01-10 16:02:36 -06:00
|
|
|
use parse::token;
|
2014-09-13 11:06:01 -05:00
|
|
|
use ptr::P;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::collections::BitvSet;
|
2012-03-02 15:14:10 -06:00
|
|
|
|
2014-05-23 10:39:26 -05:00
|
|
|
local_data_key!(used_attrs: BitvSet)
|
2014-05-20 02:07:24 -05:00
|
|
|
|
|
|
|
pub fn mark_used(attr: &Attribute) {
|
2014-05-23 10:39:26 -05:00
|
|
|
let mut used = used_attrs.replace(None).unwrap_or_else(|| BitvSet::new());
|
|
|
|
let AttrId(id) = attr.node.id;
|
|
|
|
used.insert(id);
|
2014-05-20 02:07:24 -05:00
|
|
|
used_attrs.replace(Some(used));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_used(attr: &Attribute) -> bool {
|
2014-05-23 10:39:26 -05:00
|
|
|
let AttrId(id) = attr.node.id;
|
|
|
|
used_attrs.get().map_or(false, |used| used.contains(&id))
|
2014-05-20 02:07:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub trait AttrMetaMethods {
|
2014-05-21 00:07:42 -05:00
|
|
|
fn check_name(&self, name: &str) -> bool {
|
|
|
|
name == self.name().get()
|
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
|
|
|
|
/// Retrieve the name of the meta item, e.g. foo in #[foo],
|
|
|
|
/// #[foo="bar"] and #[foo(bar)]
|
2014-01-08 12:35:15 -06:00
|
|
|
fn name(&self) -> InternedString;
|
2013-07-19 06:51:37 -05:00
|
|
|
|
2014-06-09 15:12:30 -05:00
|
|
|
/// Gets the string value if self is a MetaNameValue variant
|
|
|
|
/// containing a string, otherwise None.
|
2014-01-10 16:02:36 -06:00
|
|
|
fn value_str(&self) -> Option<InternedString>;
|
2013-07-19 06:51:37 -05:00
|
|
|
/// Gets a list of inner meta items from a list MetaItem type.
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]>;
|
2012-03-02 12:05:30 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
impl AttrMetaMethods for Attribute {
|
2014-05-21 00:07:42 -05:00
|
|
|
fn check_name(&self, name: &str) -> bool {
|
2014-05-23 10:39:26 -05:00
|
|
|
let matches = name == self.name().get();
|
|
|
|
if matches {
|
2014-05-21 00:07:42 -05:00
|
|
|
mark_used(self);
|
|
|
|
}
|
2014-05-23 10:39:26 -05:00
|
|
|
matches
|
2014-05-21 00:07:42 -05:00
|
|
|
}
|
2014-01-08 12:35:15 -06:00
|
|
|
fn name(&self) -> InternedString { self.meta().name() }
|
2014-01-10 16:02:36 -06:00
|
|
|
fn value_str(&self) -> Option<InternedString> {
|
|
|
|
self.meta().value_str()
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> {
|
2013-07-19 06:51:37 -05:00
|
|
|
self.node.value.meta_item_list()
|
|
|
|
}
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
impl AttrMetaMethods for MetaItem {
|
2014-01-08 12:35:15 -06:00
|
|
|
fn name(&self) -> InternedString {
|
2013-07-19 06:51:37 -05:00
|
|
|
match self.node {
|
2014-01-08 12:35:15 -06:00
|
|
|
MetaWord(ref n) => (*n).clone(),
|
|
|
|
MetaNameValue(ref n, _) => (*n).clone(),
|
|
|
|
MetaList(ref n, _) => (*n).clone(),
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
2014-01-10 16:02:36 -06:00
|
|
|
fn value_str(&self) -> Option<InternedString> {
|
2013-07-19 06:51:37 -05:00
|
|
|
match self.node {
|
|
|
|
MetaNameValue(_, ref v) => {
|
|
|
|
match v.node {
|
2014-01-10 16:02:36 -06:00
|
|
|
ast::LitStr(ref s, _) => Some((*s).clone()),
|
2013-07-19 06:51:37 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> {
|
2013-07-19 06:51:37 -05:00
|
|
|
match self.node {
|
|
|
|
MetaList(_, ref l) => Some(l.as_slice()),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 18:23:34 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
// Annoying, but required to get test_cfg to work
|
2014-09-13 11:06:01 -05:00
|
|
|
impl AttrMetaMethods for P<MetaItem> {
|
2014-01-08 12:35:15 -06:00
|
|
|
fn name(&self) -> InternedString { (**self).name() }
|
2014-01-10 16:02:36 -06:00
|
|
|
fn value_str(&self) -> Option<InternedString> { (**self).value_str() }
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_item_list<'a>(&'a self) -> Option<&'a [P<MetaItem>]> {
|
2013-07-19 06:51:37 -05:00
|
|
|
(**self).meta_item_list()
|
2012-06-30 05:54:54 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-15 03:07:47 -05:00
|
|
|
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub trait AttributeMethods {
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta<'a>(&'a self) -> &'a MetaItem;
|
|
|
|
fn with_desugared_doc<T>(&self, f: |&Attribute| -> T) -> T;
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
impl AttributeMethods for Attribute {
|
|
|
|
/// Extract the MetaItem from inside this Attribute.
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta<'a>(&'a self) -> &'a MetaItem {
|
|
|
|
&*self.node.value
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/// Convert self to a normal #[doc="foo"] comment, if it is a
|
|
|
|
/// comment like `///` or `/** */`. (Returns self unchanged for
|
|
|
|
/// non-sugared doc attributes.)
|
2014-09-13 11:06:01 -05:00
|
|
|
fn with_desugared_doc<T>(&self, f: |&Attribute| -> T) -> T {
|
2013-07-19 06:51:37 -05:00
|
|
|
if self.node.is_sugared_doc {
|
2013-08-03 18:59:24 -05:00
|
|
|
let comment = self.value_str().unwrap();
|
2014-01-10 16:02:36 -06:00
|
|
|
let meta = mk_name_value_item_str(
|
|
|
|
InternedString::new("doc"),
|
|
|
|
token::intern_and_get_ident(strip_doc_comment_decoration(
|
2014-05-07 18:33:43 -05:00
|
|
|
comment.get()).as_slice()));
|
2014-05-20 23:25:42 -05:00
|
|
|
if self.node.style == ast::AttrOuter {
|
2014-09-13 11:06:01 -05:00
|
|
|
f(&mk_attr_outer(self.node.id, meta))
|
2014-05-20 23:25:42 -05:00
|
|
|
} else {
|
2014-09-13 11:06:01 -05:00
|
|
|
f(&mk_attr_inner(self.node.id, meta))
|
2014-05-20 23:25:42 -05:00
|
|
|
}
|
2013-07-19 06:51:37 -05:00
|
|
|
} else {
|
2014-09-13 11:06:01 -05:00
|
|
|
f(self)
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
2011-07-05 19:01:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/* Constructors */
|
2011-10-29 19:35:45 -05:00
|
|
|
|
2014-01-10 16:02:36 -06:00
|
|
|
pub fn mk_name_value_item_str(name: InternedString, value: InternedString)
|
2014-09-13 11:06:01 -05:00
|
|
|
-> P<MetaItem> {
|
2014-01-09 07:05:33 -06:00
|
|
|
let value_lit = dummy_spanned(ast::LitStr(value, ast::CookedStr));
|
2013-07-19 06:51:37 -05:00
|
|
|
mk_name_value_item(name, value_lit)
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
2011-06-28 14:53:59 -05:00
|
|
|
|
2014-01-08 12:35:15 -06:00
|
|
|
pub fn mk_name_value_item(name: InternedString, value: ast::Lit)
|
2014-09-13 11:06:01 -05:00
|
|
|
-> P<MetaItem> {
|
|
|
|
P(dummy_spanned(MetaNameValue(name, value)))
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn mk_list_item(name: InternedString, items: Vec<P<MetaItem>>) -> P<MetaItem> {
|
|
|
|
P(dummy_spanned(MetaList(name, items)))
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn mk_word_item(name: InternedString) -> P<MetaItem> {
|
|
|
|
P(dummy_spanned(MetaWord(name)))
|
2011-06-28 14:53:59 -05:00
|
|
|
}
|
|
|
|
|
2014-05-20 02:07:24 -05:00
|
|
|
local_data_key!(next_attr_id: uint)
|
|
|
|
|
|
|
|
pub fn mk_attr_id() -> AttrId {
|
|
|
|
let id = next_attr_id.replace(None).unwrap_or(0);
|
|
|
|
next_attr_id.replace(Some(id + 1));
|
|
|
|
AttrId(id)
|
|
|
|
}
|
|
|
|
|
2014-05-20 23:25:42 -05:00
|
|
|
/// Returns an inner attribute with the given value.
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn mk_attr_inner(id: AttrId, item: P<MetaItem>) -> Attribute {
|
2013-07-19 06:51:37 -05:00
|
|
|
dummy_spanned(Attribute_ {
|
2014-05-20 02:07:24 -05:00
|
|
|
id: id,
|
2013-07-19 06:51:37 -05:00
|
|
|
style: ast::AttrInner,
|
|
|
|
value: item,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
2011-06-28 14:53:59 -05:00
|
|
|
}
|
|
|
|
|
2014-05-20 23:25:42 -05:00
|
|
|
/// Returns an outer attribute with the given value.
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn mk_attr_outer(id: AttrId, item: P<MetaItem>) -> Attribute {
|
2014-05-20 23:25:42 -05:00
|
|
|
dummy_spanned(Attribute_ {
|
2014-05-20 02:07:24 -05:00
|
|
|
id: id,
|
2014-05-20 23:25:42 -05:00
|
|
|
style: ast::AttrOuter,
|
|
|
|
value: item,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-20 02:07:24 -05:00
|
|
|
pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos,
|
|
|
|
hi: BytePos)
|
2014-01-10 16:02:36 -06:00
|
|
|
-> Attribute {
|
|
|
|
let style = doc_comment_style(text.get());
|
2014-01-09 07:05:33 -06:00
|
|
|
let lit = spanned(lo, hi, ast::LitStr(text, ast::CookedStr));
|
2013-07-19 06:51:37 -05:00
|
|
|
let attr = Attribute_ {
|
2014-05-20 02:07:24 -05:00
|
|
|
id: id,
|
2013-07-19 06:51:37 -05:00
|
|
|
style: style,
|
2014-09-13 11:06:01 -05:00
|
|
|
value: P(spanned(lo, hi, MetaNameValue(InternedString::new("doc"),
|
|
|
|
lit))),
|
2013-07-19 06:51:37 -05:00
|
|
|
is_sugared_doc: true
|
|
|
|
};
|
|
|
|
spanned(lo, hi, attr)
|
2011-07-13 20:13:19 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05: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 11:06:01 -05:00
|
|
|
pub fn contains(haystack: &[P<MetaItem>], needle: &MetaItem) -> bool {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("attr::contains (name={})", needle.name());
|
2013-11-20 18:23:04 -06:00
|
|
|
haystack.iter().any(|item| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" testing: {}", item.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
item.node == needle.node
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn contains_name<AM: AttrMetaMethods>(metas: &[AM], name: &str) -> bool {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("attr::contains_name (name={})", name);
|
2013-11-20 18:23:04 -06:00
|
|
|
metas.iter().any(|item| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!(" testing: {}", item.name());
|
2014-05-21 02:05:45 -05:00
|
|
|
item.check_name(name)
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str)
|
2014-01-10 16:02:36 -06:00
|
|
|
-> Option<InternedString> {
|
2013-07-19 06:51:37 -05:00
|
|
|
attrs.iter()
|
2014-05-21 02:05:45 -05:00
|
|
|
.find(|at| at.check_name(name))
|
2013-09-11 14:52:17 -05:00
|
|
|
.and_then(|at| at.value_str())
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn last_meta_item_value_str_by_name(items: &[P<MetaItem>], name: &str)
|
2014-01-10 16:02:36 -06: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 02:05:45 -05:00
|
|
|
.find(|mi| mi.check_name(name))
|
2014-01-08 12:35:15 -06:00
|
|
|
.and_then(|i| i.value_str())
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Higher-level applications */
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn sort_meta_items(items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> {
|
2013-07-19 06:51:37 -05:00
|
|
|
// This is sort of stupid here, but we need to sort by
|
|
|
|
// human-readable strings.
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut v = items.into_iter()
|
2014-09-13 11:06:01 -05:00
|
|
|
.map(|mi| (mi.name(), mi))
|
|
|
|
.collect::<Vec<(InternedString, P<MetaItem>)>>();
|
2013-07-19 06:51:37 -05:00
|
|
|
|
2014-01-08 12:35:15 -06:00
|
|
|
v.sort_by(|&(ref a, _), &(ref b, _)| a.cmp(b));
|
2013-02-16 08:31:57 -06:00
|
|
|
|
|
|
|
// There doesn't seem to be a more optimal way to do this
|
2014-09-14 22:27:36 -05:00
|
|
|
v.into_iter().map(|(_, m)| m.map(|Spanned {node, span}| {
|
2014-09-13 11:06:01 -05:00
|
|
|
Spanned {
|
|
|
|
node: match node {
|
|
|
|
MetaList(n, mis) => MetaList(n, sort_meta_items(mis)),
|
|
|
|
_ => node
|
|
|
|
},
|
|
|
|
span: span
|
2013-02-16 08:31:57 -06:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
})).collect()
|
2011-06-29 16:17:23 -05:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:21:18 -05:00
|
|
|
pub fn find_crate_name(attrs: &[Attribute]) -> Option<InternedString> {
|
|
|
|
first_attr_value_str_by_name(attrs, "crate_name")
|
2013-12-09 15:56:53 -06:00
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(PartialEq)]
|
2013-07-19 06:51:37 -05:00
|
|
|
pub enum InlineAttr {
|
|
|
|
InlineNone,
|
|
|
|
InlineHint,
|
|
|
|
InlineAlways,
|
|
|
|
InlineNever,
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
|
2014-08-01 14:27:12 -05:00
|
|
|
/// Determine what `#[inline]` attribute is present in `attrs`, if any.
|
2013-07-19 06:51:37 -05:00
|
|
|
pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr {
|
2013-06-18 16:45:18 -05:00
|
|
|
// FIXME (#2809)---validate the usage of #[inline] and #[inline]
|
2013-11-20 18:23:04 -06:00
|
|
|
attrs.iter().fold(InlineNone, |ia,attr| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match attr.node.value.node {
|
2014-05-21 02:05:45 -05:00
|
|
|
MetaWord(ref n) if n.equiv(&("inline")) => {
|
|
|
|
mark_used(attr);
|
2013-07-19 06:51:37 -05:00
|
|
|
InlineHint
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
2014-05-21 02:05:45 -05:00
|
|
|
MetaList(ref n, ref items) if n.equiv(&("inline")) => {
|
|
|
|
mark_used(attr);
|
|
|
|
if contains_name(items.as_slice(), "always") {
|
|
|
|
InlineAlways
|
|
|
|
} else if contains_name(items.as_slice(), "never") {
|
|
|
|
InlineNever
|
|
|
|
} else {
|
|
|
|
InlineHint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ia
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
|
2014-08-01 14:27:12 -05:00
|
|
|
/// True if `#[inline]` or `#[inline(always)]` is present in `attrs`.
|
|
|
|
pub fn requests_inline(attrs: &[Attribute]) -> bool {
|
|
|
|
match find_inline_attr(attrs) {
|
|
|
|
InlineHint | InlineAlways => true,
|
|
|
|
InlineNone | InlineNever => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:51:37 -05:00
|
|
|
/// Tests if any `cfg(...)` meta items in `metas` match `cfg`. e.g.
|
|
|
|
///
|
|
|
|
/// test_cfg(`[foo="a", bar]`, `[cfg(foo), cfg(bar)]`) == true
|
|
|
|
/// test_cfg(`[foo="a", bar]`, `[cfg(not(bar))]`) == false
|
|
|
|
/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="a")]`) == true
|
|
|
|
/// test_cfg(`[foo="a", bar]`, `[cfg(bar, foo="b")]`) == false
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn test_cfg<'a, AM: AttrMetaMethods, It: Iterator<&'a AM>>
|
|
|
|
(cfg: &[P<MetaItem>], mut metas: It) -> bool {
|
2013-07-19 06:51:37 -05:00
|
|
|
// having no #[cfg(...)] attributes counts as matching.
|
|
|
|
let mut no_cfgs = true;
|
|
|
|
|
|
|
|
// this would be much nicer as a chain of iterator adaptors, but
|
|
|
|
// this doesn't work.
|
2014-05-23 00:29:13 -05:00
|
|
|
let some_cfg_matches = metas.fold(false, |matches, mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("testing name: {}", mi.name());
|
2014-05-23 00:29:13 -05:00
|
|
|
let this_matches = if mi.check_name("cfg") { // it is a #[cfg()] attribute
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is cfg");
|
2013-07-19 06:51:37 -05:00
|
|
|
no_cfgs = false;
|
|
|
|
// only #[cfg(...)] ones are understood.
|
|
|
|
match mi.meta_item_list() {
|
|
|
|
Some(cfg_meta) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is cfg(...)");
|
2013-11-20 18:23:04 -06:00
|
|
|
cfg_meta.iter().all(|cfg_mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cfg({}[...])", cfg_mi.name());
|
2013-07-19 06:51:37 -05:00
|
|
|
match cfg_mi.node {
|
2014-01-08 12:35:15 -06:00
|
|
|
ast::MetaList(ref s, ref not_cfgs)
|
|
|
|
if s.equiv(&("not")) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("not!");
|
2013-07-19 06:51:37 -05:00
|
|
|
// inside #[cfg(not(...))], so these need to all
|
|
|
|
// not match.
|
2014-03-14 12:29:13 -05:00
|
|
|
!not_cfgs.iter().all(|mi| {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("cfg(not({}[...]))", mi.name());
|
2014-09-13 11:06:01 -05:00
|
|
|
contains(cfg, &**mi)
|
2013-07-19 06:51:37 -05:00
|
|
|
})
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
_ => contains(cfg, &**cfg_mi)
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
2013-11-20 18:23:04 -06:00
|
|
|
})
|
2013-07-19 06:51:37 -05:00
|
|
|
}
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
2014-05-23 00:29:13 -05:00
|
|
|
};
|
|
|
|
matches || this_matches
|
2013-11-20 18:23:04 -06:00
|
|
|
});
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("test_cfg (no_cfgs={}, some_cfg_matches={})", no_cfgs, some_cfg_matches);
|
2013-07-19 06:51:37 -05:00
|
|
|
no_cfgs || some_cfg_matches
|
|
|
|
}
|
2012-01-16 23:12:08 -06:00
|
|
|
|
2014-05-21 22:33:00 -05:00
|
|
|
/// Represents the #[deprecated="foo"] and friends attributes.
|
Add stability inheritance
This commit makes several changes to the stability index infrastructure:
* Stability levels are now inherited lexically, i.e., each item's
stability level becomes the default for any nested items.
* The computed stability level for an item is stored as part of the
metadata. When using an item from an external crate, this data is
looked up and cached.
* The stability lint works from the computed stability level, rather
than manual stability attribute annotations. However, the lint still
checks only a limited set of item uses (e.g., it does not check every
component of a path on import). This will be addressed in a later PR,
as part of issue #8962.
* The stability lint only applies to items originating from external
crates, since the stability index is intended as a promise to
downstream crates.
* The "experimental" lint is now _allow_ by default. This is because
almost all existing crates have been marked "experimental", pending
library stabilization. With inheritance in place, this would generate
a massive explosion of warnings for every Rust program.
The lint should be changed back to deny-by-default after library
stabilization is complete.
* The "deprecated" lint still warns by default.
The net result: we can begin tracking stability index for the standard
libraries as we stabilize, without impacting most clients.
Closes #13540.
2014-06-11 19:23:11 -05:00
|
|
|
#[deriving(Encodable,Decodable,Clone,Show)]
|
2013-08-31 02:13:57 -05:00
|
|
|
pub struct Stability {
|
2014-03-27 17:39:48 -05:00
|
|
|
pub level: StabilityLevel,
|
|
|
|
pub text: Option<InternedString>
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The available stability levels.
|
Add stability inheritance
This commit makes several changes to the stability index infrastructure:
* Stability levels are now inherited lexically, i.e., each item's
stability level becomes the default for any nested items.
* The computed stability level for an item is stored as part of the
metadata. When using an item from an external crate, this data is
looked up and cached.
* The stability lint works from the computed stability level, rather
than manual stability attribute annotations. However, the lint still
checks only a limited set of item uses (e.g., it does not check every
component of a path on import). This will be addressed in a later PR,
as part of issue #8962.
* The stability lint only applies to items originating from external
crates, since the stability index is intended as a promise to
downstream crates.
* The "experimental" lint is now _allow_ by default. This is because
almost all existing crates have been marked "experimental", pending
library stabilization. With inheritance in place, this would generate
a massive explosion of warnings for every Rust program.
The lint should be changed back to deny-by-default after library
stabilization is complete.
* The "deprecated" lint still warns by default.
The net result: we can begin tracking stability index for the standard
libraries as we stabilize, without impacting most clients.
Closes #13540.
2014-06-11 19:23:11 -05:00
|
|
|
#[deriving(Encodable,Decodable,PartialEq,PartialOrd,Clone,Show)]
|
2013-08-31 02:13:57 -05:00
|
|
|
pub enum StabilityLevel {
|
|
|
|
Deprecated,
|
|
|
|
Experimental,
|
|
|
|
Unstable,
|
|
|
|
Stable,
|
|
|
|
Frozen,
|
|
|
|
Locked
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:05:45 -05:00
|
|
|
pub fn find_stability_generic<'a,
|
|
|
|
AM: AttrMetaMethods,
|
|
|
|
I: Iterator<&'a AM>>
|
|
|
|
(mut attrs: I)
|
|
|
|
-> Option<(Stability, &'a AM)> {
|
|
|
|
for attr in attrs {
|
|
|
|
let level = match attr.name().get() {
|
2013-08-31 02:13:57 -05:00
|
|
|
"deprecated" => Deprecated,
|
|
|
|
"experimental" => Experimental,
|
|
|
|
"unstable" => Unstable,
|
|
|
|
"stable" => Stable,
|
|
|
|
"frozen" => Frozen,
|
|
|
|
"locked" => Locked,
|
2013-10-01 16:31:03 -05:00
|
|
|
_ => continue // not a stability level
|
2013-08-31 02:13:57 -05:00
|
|
|
};
|
|
|
|
|
2014-05-21 02:05:45 -05:00
|
|
|
return Some((Stability {
|
2014-09-13 11:06:01 -05:00
|
|
|
level: level,
|
2014-05-21 02:05:45 -05:00
|
|
|
text: attr.value_str()
|
|
|
|
}, attr));
|
2013-08-31 02:13:57 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:05:45 -05:00
|
|
|
/// Find the first stability attribute. `None` if none exists.
|
|
|
|
pub fn find_stability(attrs: &[Attribute]) -> Option<Stability> {
|
|
|
|
find_stability_generic(attrs.iter()).map(|(s, attr)| {
|
|
|
|
mark_used(attr);
|
|
|
|
s
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[P<MetaItem>]) {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut set = HashSet::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for meta in metas.iter() {
|
2013-07-19 06:51:37 -05:00
|
|
|
let name = meta.name();
|
2012-06-07 22:12:05 -05:00
|
|
|
|
2014-01-08 12:35:15 -06:00
|
|
|
if !set.insert(name.clone()) {
|
2012-04-15 03:07:47 -05:00
|
|
|
diagnostic.span_fatal(meta.span,
|
2014-05-16 12:45:16 -05:00
|
|
|
format!("duplicate meta item `{}`",
|
|
|
|
name).as_slice());
|
2012-04-15 03:07:47 -05:00
|
|
|
}
|
2012-01-16 23:12:08 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 17:08:45 -05:00
|
|
|
|
|
|
|
|
2014-05-27 01:56:52 -05:00
|
|
|
/// Parse #[repr(...)] forms.
|
2014-06-09 15:12:30 -05:00
|
|
|
///
|
|
|
|
/// Valid repr contents: any of the primitive integral type names (see
|
2014-05-27 01:56:52 -05: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.
|
|
|
|
pub fn find_repr_attrs(diagnostic: &SpanHandler, attr: &Attribute) -> Vec<ReprAttr> {
|
|
|
|
let mut acc = Vec::new();
|
2014-05-21 02:05:45 -05:00
|
|
|
match attr.node.value.node {
|
2014-01-08 12:35:15 -06:00
|
|
|
ast::MetaList(ref s, ref items) if s.equiv(&("repr")) => {
|
2014-05-21 02:05:45 -05:00
|
|
|
mark_used(attr);
|
2013-05-24 17:08:45 -05:00
|
|
|
for item in items.iter() {
|
|
|
|
match item.node {
|
2014-01-08 12:35:15 -06:00
|
|
|
ast::MetaWord(ref word) => {
|
|
|
|
let hint = match word.get() {
|
2013-05-24 17:08:45 -05:00
|
|
|
// Can't use "extern" because it's not a lexical identifier.
|
2014-05-27 01:56:52 -05:00
|
|
|
"C" => Some(ReprExtern),
|
|
|
|
"packed" => Some(ReprPacked),
|
2014-01-08 12:35:15 -06:00
|
|
|
_ => match int_type_of_word(word.get()) {
|
2014-05-27 01:56:52 -05:00
|
|
|
Some(ity) => Some(ReprInt(item.span, ity)),
|
2013-05-24 17:08:45 -05:00
|
|
|
None => {
|
|
|
|
// Not a word we recognize
|
|
|
|
diagnostic.span_err(item.span,
|
|
|
|
"unrecognized representation hint");
|
2014-05-27 01:56:52 -05:00
|
|
|
None
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-05-27 01:56:52 -05:00
|
|
|
|
|
|
|
match hint {
|
|
|
|
Some(h) => acc.push(h),
|
|
|
|
None => { }
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not a word:
|
2014-05-27 01:56:52 -05:00
|
|
|
_ => diagnostic.span_err(item.span, "unrecognized enum representation hint")
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not a "repr" hint: ignore.
|
|
|
|
_ => { }
|
|
|
|
}
|
2014-03-16 13:56:24 -05:00
|
|
|
acc
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn int_type_of_word(s: &str) -> Option<IntType> {
|
|
|
|
match s {
|
2014-01-09 07:05:33 -06:00
|
|
|
"i8" => Some(SignedInt(ast::TyI8)),
|
|
|
|
"u8" => Some(UnsignedInt(ast::TyU8)),
|
|
|
|
"i16" => Some(SignedInt(ast::TyI16)),
|
|
|
|
"u16" => Some(UnsignedInt(ast::TyU16)),
|
|
|
|
"i32" => Some(SignedInt(ast::TyI32)),
|
|
|
|
"u32" => Some(UnsignedInt(ast::TyU32)),
|
|
|
|
"i64" => Some(SignedInt(ast::TyI64)),
|
|
|
|
"u64" => Some(UnsignedInt(ast::TyU64)),
|
|
|
|
"int" => Some(SignedInt(ast::TyI)),
|
|
|
|
"uint" => Some(UnsignedInt(ast::TyU)),
|
2013-05-24 17:08:45 -05:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(PartialEq, Show)]
|
2013-05-24 17:08:45 -05:00
|
|
|
pub enum ReprAttr {
|
|
|
|
ReprAny,
|
|
|
|
ReprInt(Span, IntType),
|
2014-05-27 01:56:52 -05:00
|
|
|
ReprExtern,
|
|
|
|
ReprPacked,
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
|
2013-06-02 15:03:35 -05:00
|
|
|
impl ReprAttr {
|
|
|
|
pub fn is_ffi_safe(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
ReprAny => false,
|
|
|
|
ReprInt(_sp, ity) => ity.is_ffi_safe(),
|
2014-05-27 01:56:52 -05:00
|
|
|
ReprExtern => true,
|
|
|
|
ReprPacked => false
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 09:22:19 -05:00
|
|
|
#[deriving(Eq, Hash, PartialEq, Show)]
|
2013-05-24 17:08:45 -05:00
|
|
|
pub enum IntType {
|
2014-01-09 07:05:33 -06:00
|
|
|
SignedInt(ast::IntTy),
|
|
|
|
UnsignedInt(ast::UintTy)
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntType {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_signed(self) -> bool {
|
|
|
|
match self {
|
2013-11-28 14:22:53 -06:00
|
|
|
SignedInt(..) => true,
|
|
|
|
UnsignedInt(..) => false
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-02 15:03:35 -05:00
|
|
|
fn is_ffi_safe(self) -> bool {
|
|
|
|
match self {
|
2014-01-09 07:05:33 -06:00
|
|
|
SignedInt(ast::TyI8) | UnsignedInt(ast::TyU8) |
|
|
|
|
SignedInt(ast::TyI16) | UnsignedInt(ast::TyU16) |
|
|
|
|
SignedInt(ast::TyI32) | UnsignedInt(ast::TyU32) |
|
|
|
|
SignedInt(ast::TyI64) | UnsignedInt(ast::TyU64) => true,
|
2014-05-27 01:56:52 -05:00
|
|
|
SignedInt(ast::TyI) | UnsignedInt(ast::TyU) => false
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 17:08:45 -05:00
|
|
|
}
|