1 //! Parsing and validation of builtin attributes
3 use ast::{self, Attribute, MetaItem, Name, NestedMetaItemKind};
4 use errors::{Applicability, Handler};
5 use feature_gate::{Features, GatedCfg};
7 use syntax_pos::{symbol::Symbol, Span};
9 use super::{list_contains_name, mark_used, MetaItemKind};
13 UnknownMetaItem(Name, &'static [&'static str]),
16 MultipleStabilityLevels,
17 UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
20 fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
21 let diag = &sess.span_diagnostic;
23 AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
24 "multiple '{}' items", item),
25 AttrError::UnknownMetaItem(item, expected) => {
26 let expected = expected
28 .map(|name| format!("`{}`", name))
30 struct_span_err!(diag, span, E0541, "unknown meta item '{}'", item)
31 .span_label(span, format!("expected one of {}", expected.join(", ")))
34 AttrError::MissingSince => span_err!(diag, span, E0542, "missing 'since'"),
35 AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
36 AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
37 "multiple stability levels"),
38 AttrError::UnsupportedLiteral(
42 let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
44 if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
45 err.span_suggestion_with_applicability(
47 "consider removing the prefix",
48 format!("{}", &lint_str[1..]),
49 Applicability::MaybeIncorrect,
58 #[derive(Copy, Clone, Hash, PartialEq, RustcEncodable, RustcDecodable)]
66 #[derive(Copy, Clone, PartialEq)]
72 /// Determine what `#[unwind]` attribute is present in `attrs`, if any.
73 pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Option<UnwindAttr> {
74 let syntax_error = |attr: &Attribute| {
77 span_err!(d, attr.span, E0633, "malformed `#[unwind]` attribute");
82 attrs.iter().fold(None, |ia, attr| {
83 if attr.path != "unwind" {
86 let meta = match attr.meta() {
87 Some(meta) => meta.node,
91 MetaItemKind::Word => {
94 MetaItemKind::List(ref items) => {
98 } else if list_contains_name(&items[..], "allowed") {
99 Some(UnwindAttr::Allowed)
100 } else if list_contains_name(&items[..], "aborts") {
101 Some(UnwindAttr::Aborts)
111 /// Represents the #[stable], #[unstable], #[rustc_{deprecated,const_unstable}] attributes.
112 #[derive(RustcEncodable, RustcDecodable, Clone, Debug, PartialEq, Eq, Hash)]
113 pub struct Stability {
114 pub level: StabilityLevel,
116 pub rustc_depr: Option<RustcDeprecation>,
117 /// `None` means the function is stable but needs to be a stable const fn, too
118 /// `Some` contains the feature gate required to be able to use the function
120 pub const_stability: Option<Symbol>,
121 /// whether the function has a `#[rustc_promotable]` attribute
122 pub promotable: bool,
125 /// The available stability levels.
126 #[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
127 pub enum StabilityLevel {
128 // Reason for the current stability level and the relevant rust-lang issue
129 Unstable { reason: Option<Symbol>, issue: u32 },
130 Stable { since: Symbol },
133 impl StabilityLevel {
134 pub fn is_unstable(&self) -> bool {
135 if let StabilityLevel::Unstable {..} = *self {
141 pub fn is_stable(&self) -> bool {
142 if let StabilityLevel::Stable {..} = *self {
150 #[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
151 pub struct RustcDeprecation {
156 /// Check if `attrs` contains an attribute like `#![feature(feature_name)]`.
157 /// This will not perform any "sanity checks" on the form of the attributes.
158 pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
159 attrs.iter().any(|item| {
160 item.check_name("feature") &&
161 item.meta_item_list().map(|list| {
162 list.iter().any(|mi| {
163 mi.word().map(|w| w.name() == feature_name)
170 /// Find the first stability attribute. `None` if none exists.
171 pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
172 item_sp: Span) -> Option<Stability> {
173 find_stability_generic(sess, attrs.iter(), item_sp)
176 fn find_stability_generic<'a, I>(sess: &ParseSess,
180 where I: Iterator<Item = &'a Attribute>
182 use self::StabilityLevel::*;
184 let mut stab: Option<Stability> = None;
185 let mut rustc_depr: Option<RustcDeprecation> = None;
186 let mut rustc_const_unstable: Option<Symbol> = None;
187 let mut promotable = false;
188 let diagnostic = &sess.span_diagnostic;
190 'outer: for attr in attrs_iter {
193 "rustc_const_unstable",
197 ].iter().any(|&s| attr.path == s) {
198 continue // not a stability level
203 let meta = attr.meta();
205 if attr.path == "rustc_promotable" {
208 // attributes with data
209 else if let Some(MetaItem { node: MetaItemKind::List(ref metas), .. }) = meta {
210 let meta = meta.as_ref().unwrap();
211 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
213 handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
216 if let Some(v) = meta.value_str() {
220 span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
225 macro_rules! get_meta {
226 ($($name:ident),+) => {
228 let mut $name = None;
231 if let Some(mi) = meta.meta_item() {
232 match &*mi.name().as_str() {
235 => if !get(mi, &mut $name) { continue 'outer },
238 let expected = &[ $( stringify!($name) ),+ ];
242 AttrError::UnknownMetaItem(mi.name(), expected),
251 AttrError::UnsupportedLiteral(
252 "unsupported literal",
262 match &*meta.name().as_str() {
263 "rustc_deprecated" => {
264 if rustc_depr.is_some() {
265 span_err!(diagnostic, item_sp, E0540,
266 "multiple rustc_deprecated attributes");
270 get_meta!(since, reason);
272 match (since, reason) {
273 (Some(since), Some(reason)) => {
274 rustc_depr = Some(RustcDeprecation {
280 handle_errors(sess, attr.span(), AttrError::MissingSince);
284 span_err!(diagnostic, attr.span(), E0543, "missing 'reason'");
289 "rustc_const_unstable" => {
290 if rustc_const_unstable.is_some() {
291 span_err!(diagnostic, item_sp, E0553,
292 "multiple rustc_const_unstable attributes");
297 if let Some(feature) = feature {
298 rustc_const_unstable = Some(feature);
300 span_err!(diagnostic, attr.span(), E0629, "missing 'feature'");
306 handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
310 let mut feature = None;
311 let mut reason = None;
312 let mut issue = None;
314 if let Some(mi) = meta.meta_item() {
315 match &*mi.name().as_str() {
316 "feature" => if !get(mi, &mut feature) { continue 'outer },
317 "reason" => if !get(mi, &mut reason) { continue 'outer },
318 "issue" => if !get(mi, &mut issue) { continue 'outer },
323 AttrError::UnknownMetaItem(
325 &["feature", "reason", "issue"]
335 AttrError::UnsupportedLiteral(
336 "unsupported literal",
344 match (feature, reason, issue) {
345 (Some(feature), reason, Some(issue)) => {
346 stab = Some(Stability {
350 if let Ok(issue) = issue.as_str().parse() {
353 span_err!(diagnostic, attr.span(), E0545,
354 "incorrect 'issue'");
361 const_stability: None,
366 handle_errors(sess, attr.span(), AttrError::MissingFeature);
370 span_err!(diagnostic, attr.span(), E0547, "missing 'issue'");
377 handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
381 let mut feature = None;
382 let mut since = None;
385 NestedMetaItemKind::MetaItem(mi) => {
386 match &*mi.name().as_str() {
387 "feature" => if !get(mi, &mut feature) { continue 'outer },
388 "since" => if !get(mi, &mut since) { continue 'outer },
393 AttrError::UnknownMetaItem(
394 mi.name(), &["since", "note"],
401 NestedMetaItemKind::Literal(lit) => {
405 AttrError::UnsupportedLiteral(
406 "unsupported literal",
415 match (feature, since) {
416 (Some(feature), Some(since)) => {
417 stab = Some(Stability {
423 const_stability: None,
428 handle_errors(sess, attr.span(), AttrError::MissingFeature);
432 handle_errors(sess, attr.span(), AttrError::MissingSince);
442 // Merge the deprecation info into the stability info
443 if let Some(rustc_depr) = rustc_depr {
444 if let Some(ref mut stab) = stab {
445 stab.rustc_depr = Some(rustc_depr);
447 span_err!(diagnostic, item_sp, E0549,
448 "rustc_deprecated attribute must be paired with \
449 either stable or unstable attribute");
453 // Merge the const-unstable info into the stability info
454 if let Some(feature) = rustc_const_unstable {
455 if let Some(ref mut stab) = stab {
456 stab.const_stability = Some(feature);
458 span_err!(diagnostic, item_sp, E0630,
459 "rustc_const_unstable attribute must be paired with \
460 either stable or unstable attribute");
464 // Merge the const-unstable info into the stability info
466 if let Some(ref mut stab) = stab {
467 stab.promotable = true;
469 span_err!(diagnostic, item_sp, E0717,
470 "rustc_promotable attribute must be paired with \
471 either stable or unstable attribute");
478 pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
479 super::first_attr_value_str_by_name(attrs, "crate_name")
482 /// Tests if a cfg-pattern matches the cfg set
483 pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
484 eval_condition(cfg, sess, &mut |cfg| {
485 if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
486 gated_cfg.check_and_emit(sess, feats);
488 let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true };
489 if cfg.ident.segments.len() != 1 {
490 return error(cfg.ident.span, "`cfg` predicate key must be an identifier");
493 MetaItemKind::List(..) => {
494 error(cfg.span, "unexpected parentheses after `cfg` predicate key")
496 MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
500 AttrError::UnsupportedLiteral(
501 "literal in `cfg` predicate value must be a string",
502 lit.node.is_bytestr()
507 MetaItemKind::NameValue(..) | MetaItemKind::Word => {
508 sess.config.contains(&(cfg.name(), cfg.value_str()))
514 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
515 /// evaluate individual items.
516 pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
518 where F: FnMut(&ast::MetaItem) -> bool
521 ast::MetaItemKind::List(ref mis) => {
522 for mi in mis.iter() {
523 if !mi.is_meta_item() {
527 AttrError::UnsupportedLiteral(
528 "unsupported literal",
536 // The unwraps below may look dangerous, but we've already asserted
537 // that they won't fail with the loop above.
538 match &*cfg.name().as_str() {
539 "any" => mis.iter().any(|mi| {
540 eval_condition(mi.meta_item().unwrap(), sess, eval)
542 "all" => mis.iter().all(|mi| {
543 eval_condition(mi.meta_item().unwrap(), sess, eval)
547 span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
551 !eval_condition(mis[0].meta_item().unwrap(), sess, eval)
554 span_err!(sess.span_diagnostic, cfg.span, E0537, "invalid predicate `{}`", p);
559 ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
566 #[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, Clone, Debug, Eq, Hash)]
567 pub struct Deprecation {
568 pub since: Option<Symbol>,
569 pub note: Option<Symbol>,
572 /// Find the deprecation attribute. `None` if none exists.
573 pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
574 item_sp: Span) -> Option<Deprecation> {
575 find_deprecation_generic(sess, attrs.iter(), item_sp)
578 fn find_deprecation_generic<'a, I>(sess: &ParseSess,
581 -> Option<Deprecation>
582 where I: Iterator<Item = &'a Attribute>
584 let mut depr: Option<Deprecation> = None;
585 let diagnostic = &sess.span_diagnostic;
587 'outer: for attr in attrs_iter {
588 if attr.path != "deprecated" {
595 span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
599 depr = if let Some(metas) = attr.meta_item_list() {
600 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
602 handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
605 if let Some(v) = meta.value_str() {
609 if let Some(lit) = meta.name_value_literal() {
613 AttrError::UnsupportedLiteral(
614 "literal in `deprecated` \
615 value must be a string",
616 lit.node.is_bytestr()
620 span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
627 let mut since = None;
631 NestedMetaItemKind::MetaItem(mi) => {
632 match &*mi.name().as_str() {
633 "since" => if !get(mi, &mut since) { continue 'outer },
634 "note" => if !get(mi, &mut note) { continue 'outer },
639 AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
645 NestedMetaItemKind::Literal(lit) => {
649 AttrError::UnsupportedLiteral(
650 "item in `deprecated` must be a key/value pair",
659 Some(Deprecation {since: since, note: note})
661 Some(Deprecation{since: None, note: None})
668 #[derive(PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
678 #[derive(Eq, Hash, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
680 SignedInt(ast::IntTy),
681 UnsignedInt(ast::UintTy)
686 pub fn is_signed(self) -> bool {
687 use self::IntType::*;
690 SignedInt(..) => true,
691 UnsignedInt(..) => false
696 /// Parse #[repr(...)] forms.
698 /// Valid repr contents: any of the primitive integral type names (see
699 /// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
700 /// the same discriminant size that the corresponding C enum would or C
701 /// structure layout, `packed` to remove padding, and `transparent` to elegate representation
702 /// concerns to the only non-ZST field.
703 pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
704 use self::ReprAttr::*;
706 let mut acc = Vec::new();
707 let diagnostic = &sess.span_diagnostic;
708 if attr.path == "repr" {
709 if let Some(items) = attr.meta_item_list() {
712 if !item.is_meta_item() {
716 AttrError::UnsupportedLiteral(
717 "meta item in `repr` must be an identifier",
724 let mut recognised = false;
725 if let Some(mi) = item.word() {
726 let word = &*mi.name().as_str();
727 let hint = match word {
729 "packed" => Some(ReprPacked(1)),
730 "simd" => Some(ReprSimd),
731 "transparent" => Some(ReprTransparent),
732 _ => match int_type_of_word(word) {
733 Some(ity) => Some(ReprInt(ity)),
740 if let Some(h) = hint {
744 } else if let Some((name, value)) = item.name_value_literal() {
745 let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
746 if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
747 if literal.is_power_of_two() {
748 // rustc::ty::layout::Align restricts align to <= 2^29
749 if *literal <= 1 << 29 {
752 Err("larger than 2^29")
755 Err("not a power of two")
758 Err("not an unsuffixed integer")
762 let mut literal_error = None;
765 match parse_alignment(&value.node) {
766 Ok(literal) => acc.push(ReprAlign(literal)),
767 Err(message) => literal_error = Some(message)
770 else if name == "packed" {
772 match parse_alignment(&value.node) {
773 Ok(literal) => acc.push(ReprPacked(literal)),
774 Err(message) => literal_error = Some(message)
777 if let Some(literal_error) = literal_error {
778 span_err!(diagnostic, item.span, E0589,
779 "invalid `repr(align)` attribute: {}", literal_error);
782 if let Some(meta_item) = item.meta_item() {
783 if meta_item.name() == "align" {
784 if let MetaItemKind::NameValue(ref value) = meta_item.node {
786 let mut err = struct_span_err!(diagnostic, item.span, E0693,
787 "incorrect `repr(align)` attribute format");
789 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
790 err.span_suggestion_with_applicability(
792 "use parentheses instead",
793 format!("align({})", int),
794 Applicability::MachineApplicable
797 ast::LitKind::Str(s, _) => {
798 err.span_suggestion_with_applicability(
800 "use parentheses instead",
801 format!("align({})", s),
802 Applicability::MachineApplicable
813 // Not a word we recognize
814 span_err!(diagnostic, item.span, E0552,
815 "unrecognized representation hint");
823 fn int_type_of_word(s: &str) -> Option<IntType> {
824 use self::IntType::*;
827 "i8" => Some(SignedInt(ast::IntTy::I8)),
828 "u8" => Some(UnsignedInt(ast::UintTy::U8)),
829 "i16" => Some(SignedInt(ast::IntTy::I16)),
830 "u16" => Some(UnsignedInt(ast::UintTy::U16)),
831 "i32" => Some(SignedInt(ast::IntTy::I32)),
832 "u32" => Some(UnsignedInt(ast::UintTy::U32)),
833 "i64" => Some(SignedInt(ast::IntTy::I64)),
834 "u64" => Some(UnsignedInt(ast::UintTy::U64)),
835 "i128" => Some(SignedInt(ast::IntTy::I128)),
836 "u128" => Some(UnsignedInt(ast::UintTy::U128)),
837 "isize" => Some(SignedInt(ast::IntTy::Isize)),
838 "usize" => Some(UnsignedInt(ast::UintTy::Usize)),