X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibsyntax%2Fattr%2Fbuiltin.rs;h=e84adc01cf04a875a0f8c5e625fe5d7348d36366;hb=9a3e22e32f634014b02f13495aef3f0e8cdbb1b7;hp=6f7761b54fc29daf22b45aef5dbb87702b68f3e6;hpb=dbd73f640fc49b447c7e8c9e888fe238c3c714df;p=rust.git diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index 6f7761b54fc..e84adc01cf0 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -1,10 +1,10 @@ //! Parsing and validation of builtin attributes use crate::ast::{self, Attribute, MetaItem, Name, NestedMetaItemKind}; -use crate::errors::{Applicability, Handler}; use crate::feature_gate::{Features, GatedCfg}; use crate::parse::ParseSess; +use errors::{Applicability, Handler}; use syntax_pos::{symbol::Symbol, Span}; use super::{list_contains_name, mark_used, MetaItemKind}; @@ -163,7 +163,7 @@ pub struct RustcDeprecation { pub suggestion: Option, } -/// Check if `attrs` contains an attribute like `#![feature(feature_name)]`. +/// Checks if `attrs` contains an attribute like `#![feature(feature_name)]`. /// This will not perform any "sanity checks" on the form of the attributes. pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool { attrs.iter().any(|item| { @@ -177,7 +177,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool { }) } -/// Find the first stability attribute. `None` if none exists. +/// Finds the first stability attribute. `None` if none exists. pub fn find_stability(sess: &ParseSess, attrs: &[Attribute], item_sp: Span) -> Option { find_stability_generic(sess, attrs.iter(), item_sp) @@ -580,7 +580,7 @@ pub struct Deprecation { pub note: Option, } -/// Find the deprecation attribute. `None` if none exists. +/// Finds the deprecation attribute. `None` if none exists. pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute], item_sp: Span) -> Option { find_deprecation_generic(sess, attrs.iter(), item_sp) @@ -596,81 +596,86 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, let diagnostic = &sess.span_diagnostic; 'outer: for attr in attrs_iter { - if attr.path != "deprecated" { - continue + if !attr.check_name("deprecated") { + continue; } - mark_used(attr); - if depr.is_some() { span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes"); break } - depr = if let Some(metas) = attr.meta_item_list() { - let get = |meta: &MetaItem, item: &mut Option| { - if item.is_some() { - handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name())); - return false - } - 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"); + 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| { + if item.is_some() { + handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name())); + return false } + 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"); + } - false - } - }; + false + } + }; - let mut since = None; - let mut note = None; - for meta in metas { - match &meta.node { - NestedMetaItemKind::MetaItem(mi) => { - match &*mi.name().as_str() { - "since" => if !get(mi, &mut since) { continue 'outer }, - "note" => if !get(mi, &mut note) { continue 'outer }, - _ => { - handle_errors( - sess, - meta.span, - AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), - ); - continue 'outer + let mut since = None; + let mut note = None; + for meta in list { + match &meta.node { + NestedMetaItemKind::MetaItem(mi) => { + match &*mi.name().as_str() { + "since" => if !get(mi, &mut since) { continue 'outer }, + "note" => if !get(mi, &mut note) { continue 'outer }, + _ => { + handle_errors( + sess, + meta.span, + AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), + ); + continue 'outer + } } } - } - NestedMetaItemKind::Literal(lit) => { - handle_errors( - sess, - lit.span, - AttrError::UnsupportedLiteral( - "item in `deprecated` must be a key/value pair", - false, - ), - ); - continue 'outer + NestedMetaItemKind::Literal(lit) => { + handle_errors( + sess, + lit.span, + AttrError::UnsupportedLiteral( + "item in `deprecated` must be a key/value pair", + false, + ), + ); + continue 'outer + } } } - } - Some(Deprecation {since: since, note: note}) - } else { - Some(Deprecation{since: None, note: None}) - } + Some(Deprecation { since, note }) + } + }; } depr