1 //! Parsing and validation of builtin attributes
3 use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
4 use crate::feature_gate::{Features, GatedCfg};
5 use crate::print::pprust;
6 use crate::sess::ParseSess;
8 use errors::{Applicability, Handler};
9 use std::num::NonZeroU32;
10 use syntax_pos::hygiene::Transparency;
11 use syntax_pos::{symbol::Symbol, symbol::sym, Span};
12 use rustc_macros::HashStable_Generic;
14 use super::{mark_used, MetaItemKind};
16 use rustc_error_codes::*;
20 UnknownMetaItem(String, &'static [&'static str]),
23 MultipleStabilityLevels,
24 UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
27 /// A template that the attribute input must match.
28 /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
29 #[derive(Clone, Copy)]
30 pub struct AttributeTemplate {
32 pub list: Option<&'static str>,
33 pub name_value_str: Option<&'static str>,
36 impl AttributeTemplate {
37 pub fn only_word() -> Self {
38 Self { word: true, list: None, name_value_str: None }
41 /// Checks that the given meta-item is compatible with this template.
42 pub fn compatible(&self, meta_item_kind: &ast::MetaItemKind) -> bool {
43 match meta_item_kind {
44 ast::MetaItemKind::Word => self.word,
45 ast::MetaItemKind::List(..) => self.list.is_some(),
46 ast::MetaItemKind::NameValue(lit) if lit.kind.is_str() => self.name_value_str.is_some(),
47 ast::MetaItemKind::NameValue(..) => false,
52 fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
53 let diag = &sess.span_diagnostic;
55 AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
56 "multiple '{}' items", item),
57 AttrError::UnknownMetaItem(item, expected) => {
58 let expected = expected
60 .map(|name| format!("`{}`", name))
62 struct_span_err!(diag, span, E0541, "unknown meta item '{}'", item)
63 .span_label(span, format!("expected one of {}", expected.join(", ")))
66 AttrError::MissingSince => span_err!(diag, span, E0542, "missing 'since'"),
67 AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
68 AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
69 "multiple stability levels"),
70 AttrError::UnsupportedLiteral(
74 let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
76 if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
79 "consider removing the prefix",
80 format!("{}", &lint_str[1..]),
81 Applicability::MaybeIncorrect,
90 #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
98 #[derive(Clone, RustcEncodable, RustcDecodable)]
99 pub enum OptimizeAttr {
105 #[derive(Copy, Clone, PartialEq)]
106 pub enum UnwindAttr {
111 /// Determine what `#[unwind]` attribute is present in `attrs`, if any.
112 pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Option<UnwindAttr> {
113 attrs.iter().fold(None, |ia, attr| {
114 if attr.check_name(sym::unwind) {
115 if let Some(meta) = attr.meta() {
116 if let MetaItemKind::List(items) = meta.kind {
117 if items.len() == 1 {
118 if items[0].check_name(sym::allowed) {
119 return Some(UnwindAttr::Allowed);
120 } else if items[0].check_name(sym::aborts) {
121 return Some(UnwindAttr::Aborts);
126 struct_span_err!(d, attr.span, E0633, "malformed `unwind` attribute input")
127 .span_label(attr.span, "invalid argument")
130 "the allowed arguments are `allowed` and `aborts`",
131 (vec!["allowed", "aborts"]).into_iter()
132 .map(|s| format!("#[unwind({})]", s)),
133 Applicability::MachineApplicable,
144 /// Represents the #[stable], #[unstable], #[rustc_{deprecated,const_unstable}] attributes.
145 #[derive(RustcEncodable, RustcDecodable, Copy, Clone, Debug,
146 PartialEq, Eq, Hash, HashStable_Generic)]
147 pub struct Stability {
148 pub level: StabilityLevel,
150 pub rustc_depr: Option<RustcDeprecation>,
151 /// `None` means the function is stable but needs to be a stable const fn, too
152 /// `Some` contains the feature gate required to be able to use the function
154 pub const_stability: Option<Symbol>,
155 /// whether the function has a `#[rustc_promotable]` attribute
156 pub promotable: bool,
157 /// whether the function has a `#[rustc_allow_const_fn_ptr]` attribute
158 pub allow_const_fn_ptr: bool,
161 /// The available stability levels.
162 #[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd,
163 Copy, Clone, Debug, Eq, Hash, HashStable_Generic)]
164 pub enum StabilityLevel {
165 // Reason for the current stability level and the relevant rust-lang issue
166 Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
167 Stable { since: Symbol },
170 impl StabilityLevel {
171 pub fn is_unstable(&self) -> bool {
172 if let StabilityLevel::Unstable {..} = *self {
178 pub fn is_stable(&self) -> bool {
179 if let StabilityLevel::Stable {..} = *self {
187 #[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd,
188 Copy, Clone, Debug, Eq, Hash, HashStable_Generic)]
189 pub struct RustcDeprecation {
192 /// A text snippet used to completely replace any use of the deprecated item in an expression.
193 pub suggestion: Option<Symbol>,
196 /// Checks if `attrs` contains an attribute like `#![feature(feature_name)]`.
197 /// This will not perform any "sanity checks" on the form of the attributes.
198 pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool {
199 attrs.iter().any(|item| {
200 item.check_name(sym::feature) &&
201 item.meta_item_list().map(|list| {
202 list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name))
207 /// Collects stability info from all stability attributes in `attrs`.
208 /// Returns `None` if no stability attributes are found.
209 pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
210 item_sp: Span) -> Option<Stability> {
211 find_stability_generic(sess, attrs.iter(), item_sp)
214 fn find_stability_generic<'a, I>(sess: &ParseSess,
218 where I: Iterator<Item = &'a Attribute>
220 use StabilityLevel::*;
222 let mut stab: Option<Stability> = None;
223 let mut rustc_depr: Option<RustcDeprecation> = None;
224 let mut rustc_const_unstable: Option<Symbol> = None;
225 let mut promotable = false;
226 let mut allow_const_fn_ptr = false;
227 let diagnostic = &sess.span_diagnostic;
229 'outer: for attr in attrs_iter {
231 sym::rustc_deprecated,
232 sym::rustc_const_unstable,
235 sym::rustc_promotable,
236 sym::rustc_allow_const_fn_ptr,
237 ].iter().any(|&s| attr.has_name(s)) {
238 continue // not a stability level
243 let meta = attr.meta();
245 if attr.has_name(sym::rustc_promotable) {
248 if attr.has_name(sym::rustc_allow_const_fn_ptr) {
249 allow_const_fn_ptr = true;
251 // attributes with data
252 else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
253 let meta = meta.as_ref().unwrap();
254 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
259 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
263 if let Some(v) = meta.value_str() {
267 span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
272 macro_rules! get_meta {
273 ($($name:ident),+) => {
275 let mut $name = None;
278 if let Some(mi) = meta.meta_item() {
279 match mi.name_or_empty() {
281 sym::$name => if !get(mi, &mut $name) { continue 'outer },
284 let expected = &[ $( stringify!($name) ),+ ];
288 AttrError::UnknownMetaItem(
289 pprust::path_to_string(&mi.path),
300 AttrError::UnsupportedLiteral(
301 "unsupported literal",
311 match meta.name_or_empty() {
312 sym::rustc_deprecated => {
313 if rustc_depr.is_some() {
314 span_err!(diagnostic, item_sp, E0540,
315 "multiple rustc_deprecated attributes");
319 get_meta!(since, reason, suggestion);
321 match (since, reason) {
322 (Some(since), Some(reason)) => {
323 rustc_depr = Some(RustcDeprecation {
330 handle_errors(sess, attr.span, AttrError::MissingSince);
334 span_err!(diagnostic, attr.span, E0543, "missing 'reason'");
339 sym::rustc_const_unstable => {
340 if rustc_const_unstable.is_some() {
341 span_err!(diagnostic, item_sp, E0553,
342 "multiple rustc_const_unstable attributes");
347 if let Some(feature) = feature {
348 rustc_const_unstable = Some(feature);
350 span_err!(diagnostic, attr.span, E0629, "missing 'feature'");
356 handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels);
360 let mut feature = None;
361 let mut reason = None;
362 let mut issue = None;
363 let mut is_soft = false;
365 if let Some(mi) = meta.meta_item() {
366 match mi.name_or_empty() {
367 sym::feature => if !get(mi, &mut feature) { continue 'outer },
368 sym::reason => if !get(mi, &mut reason) { continue 'outer },
369 sym::issue => if !get(mi, &mut issue) { continue 'outer },
372 let msg = "`soft` should not have any arguments";
373 sess.span_diagnostic.span_err(mi.span, msg);
381 AttrError::UnknownMetaItem(
382 pprust::path_to_string(&mi.path),
383 &["feature", "reason", "issue", "soft"]
393 AttrError::UnsupportedLiteral(
394 "unsupported literal",
402 match (feature, reason, issue) {
403 (Some(feature), reason, Some(issue)) => {
404 let issue = match &*issue.as_str() {
405 // FIXME(rossmacarthur): remove "0" because "none" should be used
407 "none" | "0" => None,
409 if let Ok(num) = issue.parse() {
422 stab = Some(Stability {
430 const_stability: None,
432 allow_const_fn_ptr: false,
436 handle_errors(sess, attr.span, AttrError::MissingFeature);
440 span_err!(diagnostic, attr.span, E0547, "missing 'issue'");
447 handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels);
451 let mut feature = None;
452 let mut since = None;
455 NestedMetaItem::MetaItem(mi) => {
456 match mi.name_or_empty() {
457 sym::feature => if !get(mi, &mut feature) { continue 'outer },
458 sym::since => if !get(mi, &mut since) { continue 'outer },
463 AttrError::UnknownMetaItem(
464 pprust::path_to_string(&mi.path),
472 NestedMetaItem::Literal(lit) => {
476 AttrError::UnsupportedLiteral(
477 "unsupported literal",
486 match (feature, since) {
487 (Some(feature), Some(since)) => {
488 stab = Some(Stability {
494 const_stability: None,
496 allow_const_fn_ptr: false,
500 handle_errors(sess, attr.span, AttrError::MissingFeature);
504 handle_errors(sess, attr.span, AttrError::MissingSince);
514 // Merge the deprecation info into the stability info
515 if let Some(rustc_depr) = rustc_depr {
516 if let Some(ref mut stab) = stab {
517 stab.rustc_depr = Some(rustc_depr);
519 span_err!(diagnostic, item_sp, E0549,
520 "rustc_deprecated attribute must be paired with \
521 either stable or unstable attribute");
525 // Merge the const-unstable info into the stability info
526 if let Some(feature) = rustc_const_unstable {
527 if let Some(ref mut stab) = stab {
528 stab.const_stability = Some(feature);
530 span_err!(diagnostic, item_sp, E0630,
531 "rustc_const_unstable attribute must be paired with \
532 either stable or unstable attribute");
536 // Merge the const-unstable info into the stability info
537 if promotable || allow_const_fn_ptr {
538 if let Some(ref mut stab) = stab {
539 stab.promotable = promotable;
540 stab.allow_const_fn_ptr = allow_const_fn_ptr;
542 span_err!(diagnostic, item_sp, E0717,
543 "rustc_promotable and rustc_allow_const_fn_ptr attributes \
544 must be paired with either stable or unstable attribute");
551 pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
552 super::first_attr_value_str_by_name(attrs, sym::crate_name)
555 /// Tests if a cfg-pattern matches the cfg set
556 pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
557 eval_condition(cfg, sess, &mut |cfg| {
558 if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
559 gated_cfg.check_and_emit(sess, feats);
561 let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true };
562 if cfg.path.segments.len() != 1 {
563 return error(cfg.path.span, "`cfg` predicate key must be an identifier");
566 MetaItemKind::List(..) => {
567 error(cfg.span, "unexpected parentheses after `cfg` predicate key")
569 MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
573 AttrError::UnsupportedLiteral(
574 "literal in `cfg` predicate value must be a string",
575 lit.kind.is_bytestr()
580 MetaItemKind::NameValue(..) | MetaItemKind::Word => {
581 let ident = cfg.ident().expect("multi-segment cfg predicate");
582 sess.config.contains(&(ident.name, cfg.value_str()))
588 /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
589 /// evaluate individual items.
590 pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
592 where F: FnMut(&ast::MetaItem) -> bool
595 ast::MetaItemKind::List(ref mis) => {
596 for mi in mis.iter() {
597 if !mi.is_meta_item() {
601 AttrError::UnsupportedLiteral(
602 "unsupported literal",
610 // The unwraps below may look dangerous, but we've already asserted
611 // that they won't fail with the loop above.
612 match cfg.name_or_empty() {
613 sym::any => mis.iter().any(|mi| {
614 eval_condition(mi.meta_item().unwrap(), sess, eval)
616 sym::all => mis.iter().all(|mi| {
617 eval_condition(mi.meta_item().unwrap(), sess, eval)
621 span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
625 !eval_condition(mis[0].meta_item().unwrap(), sess, eval)
629 sess.span_diagnostic, cfg.span, E0537,
630 "invalid predicate `{}`",
631 pprust::path_to_string(&cfg.path)
637 ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
643 #[derive(RustcEncodable, RustcDecodable, Clone, HashStable_Generic)]
644 pub struct Deprecation {
645 pub since: Option<Symbol>,
646 pub note: Option<Symbol>,
649 /// Finds the deprecation attribute. `None` if none exists.
650 pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
651 item_sp: Span) -> Option<Deprecation> {
652 find_deprecation_generic(sess, attrs.iter(), item_sp)
655 fn find_deprecation_generic<'a, I>(sess: &ParseSess,
658 -> Option<Deprecation>
659 where I: Iterator<Item = &'a Attribute>
661 let mut depr: Option<Deprecation> = None;
662 let diagnostic = &sess.span_diagnostic;
664 'outer: for attr in attrs_iter {
665 if !attr.check_name(sym::deprecated) {
670 span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
674 let meta = match attr.meta() {
678 depr = match &meta.kind {
679 MetaItemKind::Word => Some(Deprecation { since: None, note: None }),
680 MetaItemKind::NameValue(..) => {
681 meta.value_str().map(|note| {
682 Deprecation { since: None, note: Some(note) }
685 MetaItemKind::List(list) => {
686 let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
691 AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
695 if let Some(v) = meta.value_str() {
699 if let Some(lit) = meta.name_value_literal() {
703 AttrError::UnsupportedLiteral(
704 "literal in `deprecated` \
705 value must be a string",
706 lit.kind.is_bytestr()
710 span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
717 let mut since = None;
721 NestedMetaItem::MetaItem(mi) => {
722 match mi.name_or_empty() {
723 sym::since => if !get(mi, &mut since) { continue 'outer },
724 sym::note => if !get(mi, &mut note) { continue 'outer },
729 AttrError::UnknownMetaItem(
730 pprust::path_to_string(&mi.path),
738 NestedMetaItem::Literal(lit) => {
742 AttrError::UnsupportedLiteral(
743 "item in `deprecated` must be a key/value pair",
752 Some(Deprecation { since, note })
760 #[derive(PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone)]
770 #[derive(Eq, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone, HashStable_Generic)]
772 SignedInt(ast::IntTy),
773 UnsignedInt(ast::UintTy)
778 pub fn is_signed(self) -> bool {
782 SignedInt(..) => true,
783 UnsignedInt(..) => false
788 /// Parse #[repr(...)] forms.
790 /// Valid repr contents: any of the primitive integral type names (see
791 /// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
792 /// the same discriminant size that the corresponding C enum would or C
793 /// structure layout, `packed` to remove padding, and `transparent` to elegate representation
794 /// concerns to the only non-ZST field.
795 pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
798 let mut acc = Vec::new();
799 let diagnostic = &sess.span_diagnostic;
800 if attr.has_name(sym::repr) {
801 if let Some(items) = attr.meta_item_list() {
804 if !item.is_meta_item() {
808 AttrError::UnsupportedLiteral(
809 "meta item in `repr` must be an identifier",
816 let mut recognised = false;
818 let hint = match item.name_or_empty() {
819 sym::C => Some(ReprC),
820 sym::packed => Some(ReprPacked(1)),
821 sym::simd => Some(ReprSimd),
822 sym::transparent => Some(ReprTransparent),
823 name => int_type_of_word(name).map(ReprInt),
826 if let Some(h) = hint {
830 } else if let Some((name, value)) = item.name_value_literal() {
831 let parse_alignment = |node: &ast::LitKind| -> Result<u32, &'static str> {
832 if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
833 if literal.is_power_of_two() {
834 // rustc::ty::layout::Align restricts align to <= 2^29
835 if *literal <= 1 << 29 {
838 Err("larger than 2^29")
841 Err("not a power of two")
844 Err("not an unsuffixed integer")
848 let mut literal_error = None;
849 if name == sym::align {
851 match parse_alignment(&value.kind) {
852 Ok(literal) => acc.push(ReprAlign(literal)),
853 Err(message) => literal_error = Some(message)
856 else if name == sym::packed {
858 match parse_alignment(&value.kind) {
859 Ok(literal) => acc.push(ReprPacked(literal)),
860 Err(message) => literal_error = Some(message)
863 if let Some(literal_error) = literal_error {
864 span_err!(diagnostic, item.span(), E0589,
865 "invalid `repr(align)` attribute: {}", literal_error);
868 if let Some(meta_item) = item.meta_item() {
869 if meta_item.check_name(sym::align) {
870 if let MetaItemKind::NameValue(ref value) = meta_item.kind {
872 let mut err = struct_span_err!(diagnostic, item.span(), E0693,
873 "incorrect `repr(align)` attribute format");
875 ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
878 "use parentheses instead",
879 format!("align({})", int),
880 Applicability::MachineApplicable
883 ast::LitKind::Str(s, _) => {
886 "use parentheses instead",
887 format!("align({})", s),
888 Applicability::MachineApplicable
899 // Not a word we recognize
900 span_err!(diagnostic, item.span(), E0552,
901 "unrecognized representation hint");
909 fn int_type_of_word(s: Symbol) -> Option<IntType> {
913 sym::i8 => Some(SignedInt(ast::IntTy::I8)),
914 sym::u8 => Some(UnsignedInt(ast::UintTy::U8)),
915 sym::i16 => Some(SignedInt(ast::IntTy::I16)),
916 sym::u16 => Some(UnsignedInt(ast::UintTy::U16)),
917 sym::i32 => Some(SignedInt(ast::IntTy::I32)),
918 sym::u32 => Some(UnsignedInt(ast::UintTy::U32)),
919 sym::i64 => Some(SignedInt(ast::IntTy::I64)),
920 sym::u64 => Some(UnsignedInt(ast::UintTy::U64)),
921 sym::i128 => Some(SignedInt(ast::IntTy::I128)),
922 sym::u128 => Some(UnsignedInt(ast::UintTy::U128)),
923 sym::isize => Some(SignedInt(ast::IntTy::Isize)),
924 sym::usize => Some(UnsignedInt(ast::UintTy::Usize)),
929 pub enum TransparencyError {
930 UnknownTransparency(Symbol, Span),
931 MultipleTransparencyAttrs(Span, Span),
934 pub fn find_transparency(
935 attrs: &[Attribute], is_legacy: bool
936 ) -> (Transparency, Option<TransparencyError>) {
937 let mut transparency = None;
938 let mut error = None;
940 if attr.check_name(sym::rustc_macro_transparency) {
941 if let Some((_, old_span)) = transparency {
942 error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
944 } else if let Some(value) = attr.value_str() {
945 transparency = Some((match &*value.as_str() {
946 "transparent" => Transparency::Transparent,
947 "semitransparent" => Transparency::SemiTransparent,
948 "opaque" => Transparency::Opaque,
950 error = Some(TransparencyError::UnknownTransparency(value, attr.span));
957 let fallback = if is_legacy { Transparency::SemiTransparent } else { Transparency::Opaque };
958 (transparency.map_or(fallback, |t| t.0), error)