2018-06-30 12:19:18 -04:00
|
|
|
//! Parsing and validation of builtin attributes
|
|
|
|
|
2019-03-03 20:56:24 +03:00
|
|
|
use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::feature_gate::{Features, GatedCfg};
|
|
|
|
use crate::parse::ParseSess;
|
|
|
|
|
2019-02-09 11:24:02 +09:00
|
|
|
use errors::{Applicability, Handler};
|
2019-05-08 13:21:18 +10:00
|
|
|
use syntax_pos::{symbol::Symbol, symbol::sym, Span};
|
2018-06-30 12:19:18 -04:00
|
|
|
|
2019-02-26 22:42:50 +03:00
|
|
|
use super::{mark_used, MetaItemKind};
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
enum AttrError {
|
2019-02-28 09:17:24 +03:00
|
|
|
MultipleItem(String),
|
|
|
|
UnknownMetaItem(String, &'static [&'static str]),
|
2018-06-30 12:19:18 -04:00
|
|
|
MissingSince,
|
|
|
|
MissingFeature,
|
|
|
|
MultipleStabilityLevels,
|
2018-10-20 11:11:01 +08:00
|
|
|
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-20 11:11:01 +08:00
|
|
|
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
|
2018-10-09 19:49:18 +08:00
|
|
|
let diag = &sess.span_diagnostic;
|
2018-06-30 12:19:18 -04:00
|
|
|
match error {
|
|
|
|
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
|
|
|
|
"multiple '{}' items", item),
|
2018-06-30 15:33:59 -04:00
|
|
|
AttrError::UnknownMetaItem(item, expected) => {
|
|
|
|
let expected = expected
|
|
|
|
.iter()
|
|
|
|
.map(|name| format!("`{}`", name))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
struct_span_err!(diag, span, E0541, "unknown meta item '{}'", item)
|
|
|
|
.span_label(span, format!("expected one of {}", expected.join(", ")))
|
|
|
|
.emit();
|
|
|
|
}
|
2018-06-30 12:19:18 -04:00
|
|
|
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"),
|
2018-10-20 11:11:01 +08:00
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
msg,
|
|
|
|
is_bytestr,
|
|
|
|
) => {
|
|
|
|
let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
|
2018-10-09 17:53:59 +08:00
|
|
|
if is_bytestr {
|
2018-10-09 19:49:18 +08:00
|
|
|
if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2018-10-09 17:53:59 +08:00
|
|
|
span,
|
|
|
|
"consider removing the prefix",
|
2018-10-09 19:49:18 +08:00
|
|
|
format!("{}", &lint_str[1..]),
|
2018-10-09 17:53:59 +08:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum InlineAttr {
|
|
|
|
None,
|
|
|
|
Hint,
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2018-10-27 15:29:06 +03:00
|
|
|
#[derive(Copy, Clone, Hash, PartialEq, RustcEncodable, RustcDecodable)]
|
|
|
|
pub enum OptimizeAttr {
|
|
|
|
None,
|
|
|
|
Speed,
|
|
|
|
Size,
|
|
|
|
}
|
|
|
|
|
2018-06-30 12:19:18 -04: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> {
|
|
|
|
attrs.iter().fold(None, |ia, attr| {
|
2019-05-08 13:21:18 +10:00
|
|
|
if attr.check_name(sym::unwind) {
|
2019-02-26 22:42:50 +03:00
|
|
|
if let Some(meta) = attr.meta() {
|
|
|
|
if let MetaItemKind::List(items) = meta.node {
|
|
|
|
if items.len() == 1 {
|
2019-05-08 13:21:18 +10:00
|
|
|
if items[0].check_name(sym::allowed) {
|
2019-02-26 22:42:50 +03:00
|
|
|
return Some(UnwindAttr::Allowed);
|
2019-05-08 13:21:18 +10:00
|
|
|
} else if items[0].check_name(sym::aborts) {
|
2019-02-26 22:42:50 +03:00
|
|
|
return Some(UnwindAttr::Aborts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostic.map(|d| {
|
|
|
|
span_err!(d, attr.span, E0633, "malformed `#[unwind]` attribute");
|
|
|
|
});
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-26 22:42:50 +03:00
|
|
|
|
|
|
|
ia
|
2018-06-30 12:19:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents the #[stable], #[unstable], #[rustc_{deprecated,const_unstable}] attributes.
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Stability {
|
|
|
|
pub level: StabilityLevel,
|
|
|
|
pub feature: Symbol,
|
|
|
|
pub rustc_depr: Option<RustcDeprecation>,
|
2018-10-05 10:17:16 +02:00
|
|
|
/// `None` means the function is stable but needs to be a stable const fn, too
|
2018-08-22 15:56:37 +02:00
|
|
|
/// `Some` contains the feature gate required to be able to use the function
|
|
|
|
/// as const fn
|
|
|
|
pub const_stability: Option<Symbol>,
|
2018-08-31 13:50:14 +02:00
|
|
|
/// whether the function has a `#[rustc_promotable]` attribute
|
|
|
|
pub promotable: bool,
|
2019-04-05 14:13:44 -07:00
|
|
|
/// whether the function has a `#[rustc_allow_const_fn_ptr]` attribute
|
|
|
|
pub allow_const_fn_ptr: bool,
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The available stability levels.
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub enum StabilityLevel {
|
|
|
|
// Reason for the current stability level and the relevant rust-lang issue
|
|
|
|
Unstable { reason: Option<Symbol>, issue: u32 },
|
|
|
|
Stable { since: Symbol },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StabilityLevel {
|
|
|
|
pub fn is_unstable(&self) -> bool {
|
|
|
|
if let StabilityLevel::Unstable {..} = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn is_stable(&self) -> bool {
|
|
|
|
if let StabilityLevel::Stable {..} = *self {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub struct RustcDeprecation {
|
|
|
|
pub since: Symbol,
|
|
|
|
pub reason: Symbol,
|
2019-01-30 17:47:36 +01:00
|
|
|
/// A text snippet used to completely replace any use of the deprecated item in an expression.
|
|
|
|
pub suggestion: Option<Symbol>,
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Checks if `attrs` contains an attribute like `#![feature(feature_name)]`.
|
2018-06-30 12:19:18 -04:00
|
|
|
/// This will not perform any "sanity checks" on the form of the attributes.
|
2019-05-08 13:21:18 +10:00
|
|
|
pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool {
|
2018-06-30 12:19:18 -04:00
|
|
|
attrs.iter().any(|item| {
|
2019-05-08 13:21:18 +10:00
|
|
|
item.check_name(sym::feature) &&
|
2018-06-30 12:19:18 -04:00
|
|
|
item.meta_item_list().map(|list| {
|
2019-02-28 09:17:24 +03:00
|
|
|
list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name))
|
2018-06-30 12:19:18 -04:00
|
|
|
}).unwrap_or(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Finds the first stability attribute. `None` if none exists.
|
2018-10-09 19:49:18 +08:00
|
|
|
pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
|
2018-06-30 12:19:18 -04:00
|
|
|
item_sp: Span) -> Option<Stability> {
|
2018-10-09 19:49:18 +08:00
|
|
|
find_stability_generic(sess, attrs.iter(), item_sp)
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:49:18 +08:00
|
|
|
fn find_stability_generic<'a, I>(sess: &ParseSess,
|
2018-06-30 12:19:18 -04:00
|
|
|
attrs_iter: I,
|
|
|
|
item_sp: Span)
|
|
|
|
-> Option<Stability>
|
|
|
|
where I: Iterator<Item = &'a Attribute>
|
|
|
|
{
|
2019-02-07 02:33:01 +09:00
|
|
|
use StabilityLevel::*;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
let mut stab: Option<Stability> = None;
|
|
|
|
let mut rustc_depr: Option<RustcDeprecation> = None;
|
2018-08-22 15:56:37 +02:00
|
|
|
let mut rustc_const_unstable: Option<Symbol> = None;
|
2018-08-31 13:50:14 +02:00
|
|
|
let mut promotable = false;
|
2019-04-05 14:13:44 -07:00
|
|
|
let mut allow_const_fn_ptr = false;
|
2018-10-09 19:49:18 +08:00
|
|
|
let diagnostic = &sess.span_diagnostic;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
'outer: for attr in attrs_iter {
|
|
|
|
if ![
|
2019-05-07 16:03:44 +10:00
|
|
|
sym::rustc_deprecated,
|
|
|
|
sym::rustc_const_unstable,
|
|
|
|
sym::unstable,
|
|
|
|
sym::stable,
|
|
|
|
sym::rustc_promotable,
|
|
|
|
sym::rustc_allow_const_fn_ptr,
|
2018-06-30 12:19:18 -04:00
|
|
|
].iter().any(|&s| attr.path == s) {
|
|
|
|
continue // not a stability level
|
|
|
|
}
|
|
|
|
|
|
|
|
mark_used(attr);
|
|
|
|
|
|
|
|
let meta = attr.meta();
|
2018-08-31 13:50:14 +02:00
|
|
|
|
2019-05-07 16:03:44 +10:00
|
|
|
if attr.path == sym::rustc_promotable {
|
2018-08-31 13:50:14 +02:00
|
|
|
promotable = true;
|
|
|
|
}
|
2019-05-07 16:03:44 +10:00
|
|
|
if attr.path == sym::rustc_allow_const_fn_ptr {
|
2019-04-05 14:13:44 -07:00
|
|
|
allow_const_fn_ptr = true;
|
|
|
|
}
|
2018-08-22 15:56:37 +02:00
|
|
|
// attributes with data
|
2018-08-31 13:50:14 +02:00
|
|
|
else if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta {
|
2018-06-30 12:19:18 -04:00
|
|
|
let meta = meta.as_ref().unwrap();
|
|
|
|
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
|
|
|
if item.is_some() {
|
2019-03-02 19:15:26 +03:00
|
|
|
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.path.to_string()));
|
2018-06-30 12:19:18 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if let Some(v) = meta.value_str() {
|
|
|
|
*item = Some(v);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
macro_rules! get_meta {
|
|
|
|
($($name:ident),+) => {
|
|
|
|
$(
|
|
|
|
let mut $name = None;
|
|
|
|
)+
|
|
|
|
for meta in metas {
|
|
|
|
if let Some(mi) = meta.meta_item() {
|
2019-03-17 14:17:47 +03:00
|
|
|
match mi.name_or_empty().get() {
|
2018-06-30 12:19:18 -04:00
|
|
|
$(
|
2019-03-17 14:17:47 +03:00
|
|
|
stringify!($name)
|
2018-06-30 12:19:18 -04:00
|
|
|
=> if !get(mi, &mut $name) { continue 'outer },
|
|
|
|
)+
|
|
|
|
_ => {
|
2018-06-30 15:33:59 -04:00
|
|
|
let expected = &[ $( stringify!($name) ),+ ];
|
|
|
|
handle_errors(
|
2018-10-09 19:49:18 +08:00
|
|
|
sess,
|
2018-06-30 15:33:59 -04:00
|
|
|
mi.span,
|
2019-03-02 19:15:26 +03:00
|
|
|
AttrError::UnknownMetaItem(mi.path.to_string(), expected),
|
2018-10-09 17:53:59 +08:00
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-20 11:11:01 +08:00
|
|
|
handle_errors(
|
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
meta.span(),
|
2018-10-20 11:11:01 +08:00
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"unsupported literal",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:17:47 +03:00
|
|
|
match meta.name_or_empty().get() {
|
2018-06-30 12:19:18 -04:00
|
|
|
"rustc_deprecated" => {
|
|
|
|
if rustc_depr.is_some() {
|
|
|
|
span_err!(diagnostic, item_sp, E0540,
|
|
|
|
"multiple rustc_deprecated attributes");
|
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:47:36 +01:00
|
|
|
get_meta!(since, reason, suggestion);
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
match (since, reason) {
|
|
|
|
(Some(since), Some(reason)) => {
|
|
|
|
rustc_depr = Some(RustcDeprecation {
|
|
|
|
since,
|
|
|
|
reason,
|
2019-01-30 17:47:36 +01:00
|
|
|
suggestion,
|
2018-06-30 12:19:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(None, _) => {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MissingSince);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, attr.span, E0543, "missing 'reason'");
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"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 {
|
2018-08-22 15:56:37 +02:00
|
|
|
rustc_const_unstable = Some(feature);
|
2018-06-30 12:19:18 -04:00
|
|
|
} else {
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, attr.span, E0629, "missing 'feature'");
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"unstable" => {
|
|
|
|
if stab.is_some() {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels);
|
2018-06-30 12:19:18 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut feature = None;
|
|
|
|
let mut reason = None;
|
|
|
|
let mut issue = None;
|
|
|
|
for meta in metas {
|
|
|
|
if let Some(mi) = meta.meta_item() {
|
2019-03-17 14:17:47 +03:00
|
|
|
match mi.name_or_empty().get() {
|
|
|
|
"feature" => if !get(mi, &mut feature) { continue 'outer },
|
|
|
|
"reason" => if !get(mi, &mut reason) { continue 'outer },
|
|
|
|
"issue" => if !get(mi, &mut issue) { continue 'outer },
|
2018-06-30 12:19:18 -04:00
|
|
|
_ => {
|
2018-06-30 15:33:59 -04:00
|
|
|
handle_errors(
|
2018-10-09 19:49:18 +08:00
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
meta.span(),
|
2018-06-30 15:33:59 -04:00
|
|
|
AttrError::UnknownMetaItem(
|
2019-03-02 19:15:26 +03:00
|
|
|
mi.path.to_string(),
|
2018-06-30 15:33:59 -04:00
|
|
|
&["feature", "reason", "issue"]
|
|
|
|
),
|
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-20 11:11:01 +08:00
|
|
|
handle_errors(
|
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
meta.span(),
|
2018-10-20 11:11:01 +08:00
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"unsupported literal",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue 'outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match (feature, reason, issue) {
|
|
|
|
(Some(feature), reason, Some(issue)) => {
|
|
|
|
stab = Some(Stability {
|
|
|
|
level: Unstable {
|
|
|
|
reason,
|
|
|
|
issue: {
|
|
|
|
if let Ok(issue) = issue.as_str().parse() {
|
|
|
|
issue
|
|
|
|
} else {
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, attr.span, E0545,
|
2018-06-30 12:19:18 -04:00
|
|
|
"incorrect 'issue'");
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
feature,
|
|
|
|
rustc_depr: None,
|
2018-08-22 15:56:37 +02:00
|
|
|
const_stability: None,
|
2018-08-31 13:50:14 +02:00
|
|
|
promotable: false,
|
2019-04-05 14:13:44 -07:00
|
|
|
allow_const_fn_ptr: false,
|
2018-06-30 12:19:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(None, _, _) => {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MissingFeature);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, attr.span, E0547, "missing 'issue'");
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"stable" => {
|
|
|
|
if stab.is_some() {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels);
|
2018-06-30 12:19:18 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut feature = None;
|
|
|
|
let mut since = None;
|
|
|
|
for meta in metas {
|
2019-03-03 20:56:24 +03:00
|
|
|
match meta {
|
|
|
|
NestedMetaItem::MetaItem(mi) => {
|
2019-03-17 14:17:47 +03:00
|
|
|
match mi.name_or_empty().get() {
|
|
|
|
"feature" =>
|
2019-02-28 09:17:24 +03:00
|
|
|
if !get(mi, &mut feature) { continue 'outer },
|
2019-03-17 14:17:47 +03:00
|
|
|
"since" =>
|
2019-02-28 09:17:24 +03:00
|
|
|
if !get(mi, &mut since) { continue 'outer },
|
2018-10-09 17:53:59 +08:00
|
|
|
_ => {
|
|
|
|
handle_errors(
|
2018-10-09 19:49:18 +08:00
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
meta.span(),
|
2018-10-09 22:55:07 +08:00
|
|
|
AttrError::UnknownMetaItem(
|
2019-03-02 19:15:26 +03:00
|
|
|
mi.path.to_string(), &["since", "note"],
|
2018-10-09 22:55:07 +08:00
|
|
|
),
|
2018-10-09 17:53:59 +08:00
|
|
|
);
|
|
|
|
continue 'outer
|
|
|
|
}
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
2018-10-09 19:49:18 +08:00
|
|
|
},
|
2019-03-03 20:56:24 +03:00
|
|
|
NestedMetaItem::Literal(lit) => {
|
2018-10-09 17:53:59 +08:00
|
|
|
handle_errors(
|
2018-10-09 19:49:18 +08:00
|
|
|
sess,
|
2018-10-20 11:11:01 +08:00
|
|
|
lit.span,
|
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"unsupported literal",
|
|
|
|
false,
|
|
|
|
),
|
2018-10-09 17:53:59 +08:00
|
|
|
);
|
|
|
|
continue 'outer
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match (feature, since) {
|
|
|
|
(Some(feature), Some(since)) => {
|
|
|
|
stab = Some(Stability {
|
|
|
|
level: Stable {
|
|
|
|
since,
|
|
|
|
},
|
|
|
|
feature,
|
|
|
|
rustc_depr: None,
|
2018-08-22 15:56:37 +02:00
|
|
|
const_stability: None,
|
2018-08-31 13:50:14 +02:00
|
|
|
promotable: false,
|
2019-04-05 14:13:44 -07:00
|
|
|
allow_const_fn_ptr: false,
|
2018-06-30 12:19:18 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
(None, _) => {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MissingFeature);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ => {
|
2019-03-03 20:56:24 +03:00
|
|
|
handle_errors(sess, attr.span, AttrError::MissingSince);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the deprecation info into the stability info
|
|
|
|
if let Some(rustc_depr) = rustc_depr {
|
|
|
|
if let Some(ref mut stab) = stab {
|
|
|
|
stab.rustc_depr = Some(rustc_depr);
|
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, item_sp, E0549,
|
|
|
|
"rustc_deprecated attribute must be paired with \
|
|
|
|
either stable or unstable attribute");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the const-unstable info into the stability info
|
2018-08-22 15:56:37 +02:00
|
|
|
if let Some(feature) = rustc_const_unstable {
|
2018-06-30 12:19:18 -04:00
|
|
|
if let Some(ref mut stab) = stab {
|
2018-08-22 15:56:37 +02:00
|
|
|
stab.const_stability = Some(feature);
|
2018-06-30 12:19:18 -04:00
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, item_sp, E0630,
|
|
|
|
"rustc_const_unstable attribute must be paired with \
|
|
|
|
either stable or unstable attribute");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 13:50:14 +02:00
|
|
|
// Merge the const-unstable info into the stability info
|
2019-04-05 14:13:44 -07:00
|
|
|
if promotable || allow_const_fn_ptr {
|
2018-08-31 13:50:14 +02:00
|
|
|
if let Some(ref mut stab) = stab {
|
2019-04-05 14:13:44 -07:00
|
|
|
stab.promotable = promotable;
|
|
|
|
stab.allow_const_fn_ptr = allow_const_fn_ptr;
|
2018-08-31 13:50:14 +02:00
|
|
|
} else {
|
2018-10-01 14:28:15 +02:00
|
|
|
span_err!(diagnostic, item_sp, E0717,
|
2019-04-05 14:13:44 -07:00
|
|
|
"rustc_promotable and rustc_allow_const_fn_ptr attributes \
|
|
|
|
must be paired with either stable or unstable attribute");
|
2018-08-31 13:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 12:19:18 -04:00
|
|
|
stab
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
|
2019-05-08 13:21:18 +10:00
|
|
|
super::first_attr_value_str_by_name(attrs, sym::crate_name)
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests if a cfg-pattern matches the cfg set
|
|
|
|
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
|
|
|
|
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-09-02 00:13:22 +03:00
|
|
|
let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true };
|
2019-03-02 19:15:26 +03:00
|
|
|
if cfg.path.segments.len() != 1 {
|
|
|
|
return error(cfg.path.span, "`cfg` predicate key must be an identifier");
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
|
|
|
match &cfg.node {
|
|
|
|
MetaItemKind::List(..) => {
|
|
|
|
error(cfg.span, "unexpected parentheses after `cfg` predicate key")
|
|
|
|
}
|
2018-10-09 19:49:18 +08:00
|
|
|
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
|
2018-10-09 17:53:59 +08:00
|
|
|
handle_errors(
|
2018-10-09 19:49:18 +08:00
|
|
|
sess,
|
2018-10-20 11:11:01 +08:00
|
|
|
lit.span,
|
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"literal in `cfg` predicate value must be a string",
|
|
|
|
lit.node.is_bytestr()
|
|
|
|
),
|
2018-10-09 17:53:59 +08:00
|
|
|
);
|
|
|
|
true
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
|
|
|
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
|
2019-02-28 09:17:24 +03:00
|
|
|
let ident = cfg.ident().expect("multi-segment cfg predicate");
|
|
|
|
sess.config.contains(&(ident.name, cfg.value_str()))
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-30 12:19:18 -04: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
|
|
|
|
{
|
|
|
|
match cfg.node {
|
|
|
|
ast::MetaItemKind::List(ref mis) => {
|
|
|
|
for mi in mis.iter() {
|
|
|
|
if !mi.is_meta_item() {
|
2018-10-20 11:11:01 +08:00
|
|
|
handle_errors(
|
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
mi.span(),
|
2018-10-20 11:11:01 +08:00
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"unsupported literal",
|
|
|
|
false
|
|
|
|
),
|
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The unwraps below may look dangerous, but we've already asserted
|
|
|
|
// that they won't fail with the loop above.
|
2019-03-17 14:17:47 +03:00
|
|
|
match cfg.name_or_empty().get() {
|
|
|
|
"any" => mis.iter().any(|mi| {
|
2018-06-30 12:19:18 -04:00
|
|
|
eval_condition(mi.meta_item().unwrap(), sess, eval)
|
|
|
|
}),
|
2019-03-17 14:17:47 +03:00
|
|
|
"all" => mis.iter().all(|mi| {
|
2018-06-30 12:19:18 -04:00
|
|
|
eval_condition(mi.meta_item().unwrap(), sess, eval)
|
|
|
|
}),
|
2019-03-17 14:17:47 +03:00
|
|
|
"not" => {
|
2018-06-30 12:19:18 -04:00
|
|
|
if mis.len() != 1 {
|
|
|
|
span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
!eval_condition(mis[0].meta_item().unwrap(), sess, eval)
|
|
|
|
},
|
2019-02-28 09:17:24 +03:00
|
|
|
_ => {
|
|
|
|
span_err!(sess.span_diagnostic, cfg.span, E0537,
|
2019-03-02 19:15:26 +03:00
|
|
|
"invalid predicate `{}`", cfg.path);
|
2018-06-30 12:19:18 -04:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
|
|
|
|
eval(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
|
|
|
|
pub struct Deprecation {
|
|
|
|
pub since: Option<Symbol>,
|
|
|
|
pub note: Option<Symbol>,
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Finds the deprecation attribute. `None` if none exists.
|
2018-10-09 19:49:18 +08:00
|
|
|
pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
|
2018-06-30 12:19:18 -04:00
|
|
|
item_sp: Span) -> Option<Deprecation> {
|
2018-10-09 19:49:18 +08:00
|
|
|
find_deprecation_generic(sess, attrs.iter(), item_sp)
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:49:18 +08:00
|
|
|
fn find_deprecation_generic<'a, I>(sess: &ParseSess,
|
2018-06-30 12:19:18 -04:00
|
|
|
attrs_iter: I,
|
|
|
|
item_sp: Span)
|
|
|
|
-> Option<Deprecation>
|
|
|
|
where I: Iterator<Item = &'a Attribute>
|
|
|
|
{
|
|
|
|
let mut depr: Option<Deprecation> = None;
|
2018-10-09 19:49:18 +08:00
|
|
|
let diagnostic = &sess.span_diagnostic;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
'outer: for attr in attrs_iter {
|
2019-05-08 13:21:18 +10:00
|
|
|
if !attr.check_name(sym::deprecated) {
|
2019-02-04 12:41:01 -05:00
|
|
|
continue;
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if depr.is_some() {
|
|
|
|
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:41:01 -05:00
|
|
|
let meta = attr.meta().unwrap();
|
|
|
|
depr = match &meta.node {
|
|
|
|
MetaItemKind::Word => Some(Deprecation { since: None, note: None }),
|
|
|
|
MetaItemKind::NameValue(..) => {
|
|
|
|
meta.value_str().map(|note| {
|
|
|
|
Deprecation { since: None, note: Some(note) }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
MetaItemKind::List(list) => {
|
|
|
|
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
|
|
|
if item.is_some() {
|
2019-02-28 09:17:24 +03:00
|
|
|
handle_errors(
|
2019-03-02 19:15:26 +03:00
|
|
|
sess, meta.span, AttrError::MultipleItem(meta.path.to_string())
|
2019-02-28 09:17:24 +03:00
|
|
|
);
|
2019-02-04 12:41:01 -05:00
|
|
|
return false
|
2018-10-09 22:54:47 +08:00
|
|
|
}
|
2019-02-04 12:41:01 -05:00
|
|
|
if let Some(v) = meta.value_str() {
|
|
|
|
*item = Some(v);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
if let Some(lit) = meta.name_value_literal() {
|
|
|
|
handle_errors(
|
|
|
|
sess,
|
|
|
|
lit.span,
|
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"literal in `deprecated` \
|
|
|
|
value must be a string",
|
|
|
|
lit.node.is_bytestr()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
|
|
|
|
}
|
2018-10-09 22:54:47 +08:00
|
|
|
|
2019-02-04 12:41:01 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2018-06-30 12:19:18 -04:00
|
|
|
|
2019-02-04 12:41:01 -05:00
|
|
|
let mut since = None;
|
|
|
|
let mut note = None;
|
|
|
|
for meta in list {
|
2019-03-03 20:56:24 +03:00
|
|
|
match meta {
|
|
|
|
NestedMetaItem::MetaItem(mi) => {
|
2019-03-17 14:17:47 +03:00
|
|
|
match mi.name_or_empty().get() {
|
|
|
|
"since" => if !get(mi, &mut since) { continue 'outer },
|
|
|
|
"note" => if !get(mi, &mut note) { continue 'outer },
|
2019-02-04 12:41:01 -05:00
|
|
|
_ => {
|
|
|
|
handle_errors(
|
|
|
|
sess,
|
2019-02-28 09:17:24 +03:00
|
|
|
meta.span(),
|
2019-03-02 19:15:26 +03:00
|
|
|
AttrError::UnknownMetaItem(mi.path.to_string(),
|
2019-02-28 09:17:24 +03:00
|
|
|
&["since", "note"]),
|
2019-02-04 12:41:01 -05:00
|
|
|
);
|
|
|
|
continue 'outer
|
|
|
|
}
|
2018-10-09 17:53:59 +08:00
|
|
|
}
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
2019-03-03 20:56:24 +03:00
|
|
|
NestedMetaItem::Literal(lit) => {
|
2019-02-04 12:41:01 -05:00
|
|
|
handle_errors(
|
|
|
|
sess,
|
|
|
|
lit.span,
|
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"item in `deprecated` must be a key/value pair",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
continue 'outer
|
|
|
|
}
|
2018-10-09 17:53:59 +08:00
|
|
|
}
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:41:01 -05:00
|
|
|
Some(Deprecation { since, note })
|
|
|
|
}
|
|
|
|
};
|
2018-06-30 12:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
depr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
|
|
|
|
pub enum ReprAttr {
|
|
|
|
ReprInt(IntType),
|
|
|
|
ReprC,
|
|
|
|
ReprPacked(u32),
|
|
|
|
ReprSimd,
|
|
|
|
ReprTransparent,
|
|
|
|
ReprAlign(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, Hash, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
|
|
|
|
pub enum IntType {
|
|
|
|
SignedInt(ast::IntTy),
|
|
|
|
UnsignedInt(ast::UintTy)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntType {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_signed(self) -> bool {
|
2019-02-07 02:33:01 +09:00
|
|
|
use IntType::*;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
match self {
|
|
|
|
SignedInt(..) => true,
|
|
|
|
UnsignedInt(..) => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse #[repr(...)] forms.
|
|
|
|
///
|
|
|
|
/// Valid repr contents: any of the primitive integral type names (see
|
|
|
|
/// `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, `packed` to remove padding, and `transparent` to elegate representation
|
|
|
|
/// concerns to the only non-ZST field.
|
2018-10-09 19:49:18 +08:00
|
|
|
pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
|
2019-02-07 02:33:01 +09:00
|
|
|
use ReprAttr::*;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
let mut acc = Vec::new();
|
2018-10-09 19:49:18 +08:00
|
|
|
let diagnostic = &sess.span_diagnostic;
|
2019-05-07 16:03:44 +10:00
|
|
|
if attr.path == sym::repr {
|
2018-06-30 12:19:18 -04:00
|
|
|
if let Some(items) = attr.meta_item_list() {
|
|
|
|
mark_used(attr);
|
|
|
|
for item in items {
|
|
|
|
if !item.is_meta_item() {
|
2018-10-20 11:11:01 +08:00
|
|
|
handle_errors(
|
|
|
|
sess,
|
2019-03-03 20:56:24 +03:00
|
|
|
item.span(),
|
2018-10-20 11:11:01 +08:00
|
|
|
AttrError::UnsupportedLiteral(
|
|
|
|
"meta item in `repr` must be an identifier",
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
);
|
2018-06-30 12:19:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut recognised = false;
|
2019-02-28 09:17:24 +03:00
|
|
|
if item.is_word() {
|
2019-03-17 14:17:47 +03:00
|
|
|
let hint = match item.name_or_empty().get() {
|
|
|
|
"C" => Some(ReprC),
|
|
|
|
"packed" => Some(ReprPacked(1)),
|
|
|
|
"simd" => Some(ReprSimd),
|
|
|
|
"transparent" => Some(ReprTransparent),
|
|
|
|
name => int_type_of_word(name).map(ReprInt),
|
2018-06-30 12:19:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(h) = hint {
|
|
|
|
recognised = true;
|
|
|
|
acc.push(h);
|
|
|
|
}
|
|
|
|
} else if let Some((name, value)) = item.name_value_literal() {
|
|
|
|
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() {
|
|
|
|
// rustc::ty::layout::Align restricts align to <= 2^29
|
|
|
|
if *literal <= 1 << 29 {
|
|
|
|
Ok(*literal as u32)
|
|
|
|
} else {
|
|
|
|
Err("larger than 2^29")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err("not a power of two")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err("not an unsuffixed integer")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut literal_error = None;
|
2019-05-07 16:03:44 +10:00
|
|
|
if name == sym::align {
|
2018-06-30 12:19:18 -04:00
|
|
|
recognised = true;
|
|
|
|
match parse_alignment(&value.node) {
|
|
|
|
Ok(literal) => acc.push(ReprAlign(literal)),
|
|
|
|
Err(message) => literal_error = Some(message)
|
|
|
|
};
|
|
|
|
}
|
2019-05-07 16:03:44 +10:00
|
|
|
else if name == sym::packed {
|
2018-06-30 12:19:18 -04:00
|
|
|
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 {
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, item.span(), E0589,
|
2018-06-30 12:19:18 -04:00
|
|
|
"invalid `repr(align)` attribute: {}", literal_error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(meta_item) = item.meta_item() {
|
2019-05-08 13:21:18 +10:00
|
|
|
if meta_item.check_name(sym::align) {
|
2018-06-30 12:19:18 -04:00
|
|
|
if let MetaItemKind::NameValue(ref value) = meta_item.node {
|
|
|
|
recognised = true;
|
2019-03-03 20:56:24 +03:00
|
|
|
let mut err = struct_span_err!(diagnostic, item.span(), E0693,
|
2018-06-30 12:19:18 -04:00
|
|
|
"incorrect `repr(align)` attribute format");
|
|
|
|
match value.node {
|
|
|
|
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2019-03-03 20:56:24 +03:00
|
|
|
item.span(),
|
2018-06-30 12:19:18 -04:00
|
|
|
"use parentheses instead",
|
|
|
|
format!("align({})", int),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::LitKind::Str(s, _) => {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2019-03-03 20:56:24 +03:00
|
|
|
item.span(),
|
2018-06-30 12:19:18 -04:00
|
|
|
"use parentheses instead",
|
|
|
|
format!("align({})", s),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !recognised {
|
|
|
|
// Not a word we recognize
|
2019-03-03 20:56:24 +03:00
|
|
|
span_err!(diagnostic, item.span(), E0552,
|
2018-06-30 12:19:18 -04:00
|
|
|
"unrecognized representation hint");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_type_of_word(s: &str) -> Option<IntType> {
|
2019-02-07 02:33:01 +09:00
|
|
|
use IntType::*;
|
2018-06-30 12:19:18 -04:00
|
|
|
|
|
|
|
match s {
|
|
|
|
"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)),
|
|
|
|
"i128" => Some(SignedInt(ast::IntTy::I128)),
|
|
|
|
"u128" => Some(UnsignedInt(ast::UintTy::U128)),
|
|
|
|
"isize" => Some(SignedInt(ast::IntTy::Isize)),
|
|
|
|
"usize" => Some(UnsignedInt(ast::UintTy::Usize)),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|