diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/libsyntax/attr | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/libsyntax/attr')
| -rw-r--r-- | src/libsyntax/attr/builtin.rs | 455 | ||||
| -rw-r--r-- | src/libsyntax/attr/mod.rs | 194 |
2 files changed, 343 insertions, 306 deletions
diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index d780d0ad764..65b67981474 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -7,11 +7,11 @@ use crate::print::pprust; use crate::sess::ParseSess; use errors::{Applicability, Handler}; +use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg}; +use rustc_macros::HashStable_Generic; use std::num::NonZeroU32; use syntax_pos::hygiene::Transparency; -use syntax_pos::{symbol::Symbol, symbol::sym, Span}; -use rustc_feature::{Features, find_gated_cfg, GatedCfg, is_builtin_attr_name}; -use rustc_macros::HashStable_Generic; +use syntax_pos::{symbol::sym, symbol::Symbol, Span}; use rustc_error_codes::*; @@ -31,25 +31,19 @@ enum AttrError { fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) { let diag = &sess.span_diagnostic; match error { - AttrError::MultipleItem(item) => span_err!(diag, span, E0538, - "multiple '{}' items", item), + AttrError::MultipleItem(item) => span_err!(diag, span, E0538, "multiple '{}' items", item), AttrError::UnknownMetaItem(item, expected) => { - let expected = expected - .iter() - .map(|name| format!("`{}`", name)) - .collect::<Vec<_>>(); + 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(); } 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"), - AttrError::UnsupportedLiteral( - msg, - is_bytestr, - ) => { + AttrError::MultipleStabilityLevels => { + span_err!(diag, span, E0544, "multiple stability levels") + } + AttrError::UnsupportedLiteral(msg, is_bytestr) => { let mut err = struct_span_err!(diag, span, E0565, "{}", msg); if is_bytestr { if let Ok(lint_str) = sess.source_map().span_to_snippet(span) { @@ -107,10 +101,12 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op .span_suggestions( attr.span, "the allowed arguments are `allowed` and `aborts`", - (vec!["allowed", "aborts"]).into_iter() + (vec!["allowed", "aborts"]) + .into_iter() .map(|s| format!("#[unwind({})]", s)), Applicability::MachineApplicable, - ).emit(); + ) + .emit(); }); } } @@ -121,8 +117,17 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op } /// Represents the #[stable], #[unstable], #[rustc_deprecated] attributes. -#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Debug, - PartialEq, Eq, Hash, HashStable_Generic)] +#[derive( + RustcEncodable, + RustcDecodable, + Copy, + Clone, + Debug, + PartialEq, + Eq, + Hash, + HashStable_Generic +)] pub struct Stability { pub level: StabilityLevel, pub feature: Symbol, @@ -130,8 +135,17 @@ pub struct Stability { } /// Represents the #[rustc_const_unstable] and #[rustc_const_stable] attributes. -#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Debug, - PartialEq, Eq, Hash, HashStable_Generic)] +#[derive( + RustcEncodable, + RustcDecodable, + Copy, + Clone, + Debug, + PartialEq, + Eq, + Hash, + HashStable_Generic +)] pub struct ConstStability { pub level: StabilityLevel, pub feature: Symbol, @@ -142,8 +156,18 @@ pub struct ConstStability { } /// The available stability levels. -#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, - Copy, Clone, Debug, Eq, Hash, HashStable_Generic)] +#[derive( + RustcEncodable, + RustcDecodable, + PartialEq, + PartialOrd, + Copy, + Clone, + Debug, + Eq, + Hash, + HashStable_Generic +)] pub enum StabilityLevel { // Reason for the current stability level and the relevant rust-lang issue Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool }, @@ -152,23 +176,25 @@ pub enum StabilityLevel { impl StabilityLevel { pub fn is_unstable(&self) -> bool { - if let StabilityLevel::Unstable {..} = *self { - true - } else { - false - } + if let StabilityLevel::Unstable { .. } = *self { true } else { false } } pub fn is_stable(&self) -> bool { - if let StabilityLevel::Stable {..} = *self { - true - } else { - false - } + if let StabilityLevel::Stable { .. } = *self { true } else { false } } } -#[derive(RustcEncodable, RustcDecodable, PartialEq, PartialOrd, - Copy, Clone, Debug, Eq, Hash, HashStable_Generic)] +#[derive( + RustcEncodable, + RustcDecodable, + PartialEq, + PartialOrd, + Copy, + Clone, + Debug, + Eq, + Hash, + HashStable_Generic +)] pub struct RustcDeprecation { pub since: Symbol, pub reason: Symbol, @@ -180,25 +206,31 @@ pub struct RustcDeprecation { /// This will not perform any "sanity checks" on the form of the attributes. pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool { attrs.iter().any(|item| { - item.check_name(sym::feature) && - item.meta_item_list().map(|list| { - list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name)) - }).unwrap_or(false) + item.check_name(sym::feature) + && item + .meta_item_list() + .map(|list| list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name))) + .unwrap_or(false) }) } /// Collects stability info from all stability attributes in `attrs`. /// Returns `None` if no stability attributes are found. -pub fn find_stability(sess: &ParseSess, attrs: &[Attribute], - item_sp: Span) -> (Option<Stability>, Option<ConstStability>) { +pub fn find_stability( + sess: &ParseSess, + attrs: &[Attribute], + item_sp: Span, +) -> (Option<Stability>, Option<ConstStability>) { find_stability_generic(sess, attrs.iter(), item_sp) } -fn find_stability_generic<'a, I>(sess: &ParseSess, - attrs_iter: I, - item_sp: Span) - -> (Option<Stability>, Option<ConstStability>) - where I: Iterator<Item = &'a Attribute> +fn find_stability_generic<'a, I>( + sess: &ParseSess, + attrs_iter: I, + item_sp: Span, +) -> (Option<Stability>, Option<ConstStability>) +where + I: Iterator<Item = &'a Attribute>, { use StabilityLevel::*; @@ -218,8 +250,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, sym::stable, sym::rustc_promotable, sym::rustc_allow_const_fn_ptr, - ].iter().any(|&s| attr.has_name(s)) { - continue // not a stability level + ] + .iter() + .any(|&s| attr.has_name(s)) + { + continue; // not a stability level } mark_used(attr); @@ -242,7 +277,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, meta.span, AttrError::MultipleItem(pprust::path_to_string(&meta.path)), ); - return false + return false; } if let Some(v) = meta.value_str() { *item = Some(v); @@ -296,39 +331,38 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, match meta_name { sym::rustc_deprecated => { if rustc_depr.is_some() { - span_err!(diagnostic, item_sp, E0540, - "multiple rustc_deprecated attributes"); - continue 'outer + span_err!( + diagnostic, + item_sp, + E0540, + "multiple rustc_deprecated attributes" + ); + continue 'outer; } get_meta!(since, reason, suggestion); match (since, reason) { (Some(since), Some(reason)) => { - rustc_depr = Some(RustcDeprecation { - since, - reason, - suggestion, - }) + rustc_depr = Some(RustcDeprecation { since, reason, suggestion }) } (None, _) => { handle_errors(sess, attr.span, AttrError::MissingSince); - continue + continue; } _ => { span_err!(diagnostic, attr.span, E0543, "missing 'reason'"); - continue + continue; } } } - sym::rustc_const_unstable | - sym::unstable => { + sym::rustc_const_unstable | sym::unstable => { if meta_name == sym::unstable && stab.is_some() { handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels); - break + break; } else if meta_name == sym::rustc_const_unstable && const_stab.is_some() { handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels); - break + break; } let mut feature = None; @@ -338,9 +372,21 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, for meta in metas { if let Some(mi) = meta.meta_item() { match mi.name_or_empty() { - sym::feature => if !get(mi, &mut feature) { continue 'outer }, - sym::reason => if !get(mi, &mut reason) { continue 'outer }, - sym::issue => if !get(mi, &mut issue) { continue 'outer }, + sym::feature => { + if !get(mi, &mut feature) { + continue 'outer; + } + } + sym::reason => { + if !get(mi, &mut reason) { + continue 'outer; + } + } + sym::issue => { + if !get(mi, &mut issue) { + continue 'outer; + } + } sym::soft => { if !mi.is_word() { let msg = "`soft` should not have any arguments"; @@ -354,22 +400,19 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, meta.span(), AttrError::UnknownMetaItem( pprust::path_to_string(&mi.path), - &["feature", "reason", "issue", "soft"] + &["feature", "reason", "issue", "soft"], ), ); - continue 'outer + continue 'outer; } } } else { handle_errors( sess, meta.span(), - AttrError::UnsupportedLiteral( - "unsupported literal", - false, - ), + AttrError::UnsupportedLiteral("unsupported literal", false), ); - continue 'outer + continue 'outer; } } @@ -389,21 +432,13 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, E0545, "incorrect 'issue'" ); - continue + continue; } } }; - let level = Unstable { - reason, - issue, - is_soft, - }; + let level = Unstable { reason, issue, is_soft }; if sym::unstable == meta_name { - stab = Some(Stability { - level, - feature, - rustc_depr: None, - }); + stab = Some(Stability { level, feature, rustc_depr: None }); } else { const_stab = Some(ConstStability { level, @@ -415,68 +450,66 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, } (None, _, _) => { handle_errors(sess, attr.span, AttrError::MissingFeature); - continue + continue; } _ => { span_err!(diagnostic, attr.span, E0547, "missing 'issue'"); - continue + continue; } } } - sym::rustc_const_stable | - sym::stable => { + sym::rustc_const_stable | sym::stable => { if meta_name == sym::stable && stab.is_some() { handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels); - break - } else if meta_name == sym::rustc_const_stable &&const_stab.is_some() { + break; + } else if meta_name == sym::rustc_const_stable && const_stab.is_some() { handle_errors(sess, attr.span, AttrError::MultipleStabilityLevels); - break + break; } let mut feature = None; let mut since = None; for meta in metas { match meta { - NestedMetaItem::MetaItem(mi) => { - match mi.name_or_empty() { - sym::feature => if !get(mi, &mut feature) { continue 'outer }, - sym::since => if !get(mi, &mut since) { continue 'outer }, - _ => { - handle_errors( - sess, - meta.span(), - AttrError::UnknownMetaItem( - pprust::path_to_string(&mi.path), - &["since", "note"], - ), - ); - continue 'outer + NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() { + sym::feature => { + if !get(mi, &mut feature) { + continue 'outer; + } + } + sym::since => { + if !get(mi, &mut since) { + continue 'outer; } } + _ => { + handle_errors( + sess, + meta.span(), + AttrError::UnknownMetaItem( + pprust::path_to_string(&mi.path), + &["since", "note"], + ), + ); + continue 'outer; + } }, NestedMetaItem::Literal(lit) => { handle_errors( sess, lit.span, - AttrError::UnsupportedLiteral( - "unsupported literal", - false, - ), + AttrError::UnsupportedLiteral("unsupported literal", false), ); - continue 'outer + continue 'outer; } } } match (feature, since) { (Some(feature), Some(since)) => { - let level = Stable { since }; + let level = Stable { since }; if sym::stable == meta_name { - stab = Some(Stability { - level, - feature, - rustc_depr: None, - }); + stab = Some(Stability { level, feature, rustc_depr: None }); } else { const_stab = Some(ConstStability { level, @@ -488,15 +521,15 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, } (None, _) => { handle_errors(sess, attr.span, AttrError::MissingFeature); - continue + continue; } _ => { handle_errors(sess, attr.span, AttrError::MissingSince); - continue + continue; } } } - _ => unreachable!() + _ => unreachable!(), } } } @@ -506,9 +539,13 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, 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"); + span_err!( + diagnostic, + item_sp, + E0549, + "rustc_deprecated attribute must be paired with \ + either stable or unstable attribute" + ); } } @@ -518,10 +555,14 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, stab.promotable = promotable; stab.allow_const_fn_ptr = allow_const_fn_ptr; } else { - span_err!(diagnostic, item_sp, E0717, - "rustc_promotable and rustc_allow_const_fn_ptr attributes \ + span_err!( + diagnostic, + item_sp, + E0717, + "rustc_promotable and rustc_allow_const_fn_ptr attributes \ must be paired with either a rustc_const_unstable or a rustc_const_stable \ - attribute"); + attribute" + ); } } @@ -539,7 +580,10 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat if let (Some(feats), Some(gated_cfg)) = (features, gate) { gate_cfg(&gated_cfg, cfg.span, sess, feats); } - let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true }; + let error = |span, msg| { + sess.span_diagnostic.span_err(span, msg); + true + }; if cfg.path.segments.len() != 1 { return error(cfg.path.span, "`cfg` predicate key must be an identifier"); } @@ -553,7 +597,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat lit.span, AttrError::UnsupportedLiteral( "literal in `cfg` predicate value must be a string", - lit.kind.is_bytestr() + lit.kind.is_bytestr(), ), ); true @@ -588,10 +632,7 @@ pub fn eval_condition( handle_errors( sess, mi.span(), - AttrError::UnsupportedLiteral( - "unsupported literal", - false - ), + AttrError::UnsupportedLiteral("unsupported literal", false), ); return false; } @@ -600,12 +641,12 @@ pub fn eval_condition( // The unwraps below may look dangerous, but we've already asserted // that they won't fail with the loop above. match cfg.name_or_empty() { - sym::any => mis.iter().any(|mi| { - eval_condition(mi.meta_item().unwrap(), sess, eval) - }), - sym::all => mis.iter().all(|mi| { - eval_condition(mi.meta_item().unwrap(), sess, eval) - }), + sym::any => { + mis.iter().any(|mi| eval_condition(mi.meta_item().unwrap(), sess, eval)) + } + sym::all => { + mis.iter().all(|mi| eval_condition(mi.meta_item().unwrap(), sess, eval)) + } sym::not => { if mis.len() != 1 { span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern"); @@ -613,20 +654,20 @@ pub fn eval_condition( } !eval_condition(mis[0].meta_item().unwrap(), sess, eval) - }, + } _ => { span_err!( - sess.span_diagnostic, cfg.span, E0537, + sess.span_diagnostic, + cfg.span, + E0537, "invalid predicate `{}`", pprust::path_to_string(&cfg.path) ); false } } - }, - ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => { - eval(cfg) } + ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => eval(cfg), } } @@ -637,16 +678,21 @@ pub struct Deprecation { } /// Finds the deprecation attribute. `None` if none exists. -pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute], - item_sp: Span) -> Option<Deprecation> { +pub fn find_deprecation( + sess: &ParseSess, + attrs: &[Attribute], + item_sp: Span, +) -> Option<Deprecation> { find_deprecation_generic(sess, attrs.iter(), item_sp) } -fn find_deprecation_generic<'a, I>(sess: &ParseSess, - attrs_iter: I, - item_sp: Span) - -> Option<Deprecation> - where I: Iterator<Item = &'a Attribute> +fn find_deprecation_generic<'a, I>( + sess: &ParseSess, + attrs_iter: I, + item_sp: Span, +) -> Option<Deprecation> +where + I: Iterator<Item = &'a Attribute>, { let mut depr: Option<Deprecation> = None; let diagnostic = &sess.span_diagnostic; @@ -658,7 +704,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, if depr.is_some() { span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes"); - break + break; } let meta = match attr.meta() { @@ -668,9 +714,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, depr = match &meta.kind { MetaItemKind::Word => Some(Deprecation { since: None, note: None }), MetaItemKind::NameValue(..) => { - meta.value_str().map(|note| { - Deprecation { since: None, note: Some(note) } - }) + meta.value_str().map(|note| Deprecation { since: None, note: Some(note) }) } MetaItemKind::List(list) => { let get = |meta: &MetaItem, item: &mut Option<Symbol>| { @@ -680,7 +724,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, meta.span, AttrError::MultipleItem(pprust::path_to_string(&meta.path)), ); - return false + return false; } if let Some(v) = meta.value_str() { *item = Some(v); @@ -693,7 +737,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, AttrError::UnsupportedLiteral( "literal in `deprecated` \ value must be a string", - lit.kind.is_bytestr() + lit.kind.is_bytestr(), ), ); } else { @@ -708,23 +752,29 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, let mut note = None; for meta in list { match meta { - NestedMetaItem::MetaItem(mi) => { - match mi.name_or_empty() { - sym::since => if !get(mi, &mut since) { continue 'outer }, - sym::note => if !get(mi, &mut note) { continue 'outer }, - _ => { - handle_errors( - sess, - meta.span(), - AttrError::UnknownMetaItem( - pprust::path_to_string(&mi.path), - &["since", "note"], - ), - ); - continue 'outer + NestedMetaItem::MetaItem(mi) => match mi.name_or_empty() { + sym::since => { + if !get(mi, &mut since) { + continue 'outer; } } - } + sym::note => { + if !get(mi, &mut note) { + continue 'outer; + } + } + _ => { + handle_errors( + sess, + meta.span(), + AttrError::UnknownMetaItem( + pprust::path_to_string(&mi.path), + &["since", "note"], + ), + ); + continue 'outer; + } + }, NestedMetaItem::Literal(lit) => { handle_errors( sess, @@ -734,7 +784,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, false, ), ); - continue 'outer + continue 'outer; } } } @@ -760,7 +810,7 @@ pub enum ReprAttr { #[derive(Eq, PartialEq, Debug, RustcEncodable, RustcDecodable, Copy, Clone, HashStable_Generic)] pub enum IntType { SignedInt(ast::IntTy), - UnsignedInt(ast::UintTy) + UnsignedInt(ast::UintTy), } impl IntType { @@ -770,7 +820,7 @@ impl IntType { match self { SignedInt(..) => true, - UnsignedInt(..) => false + UnsignedInt(..) => false, } } } @@ -800,7 +850,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> { false, ), ); - continue + continue; } let mut recognised = false; @@ -840,34 +890,42 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> { recognised = true; match parse_alignment(&value.kind) { Ok(literal) => acc.push(ReprAlign(literal)), - Err(message) => literal_error = Some(message) + Err(message) => literal_error = Some(message), }; - } - else if name == sym::packed { + } else if name == sym::packed { recognised = true; match parse_alignment(&value.kind) { Ok(literal) => acc.push(ReprPacked(literal)), - Err(message) => literal_error = Some(message) + Err(message) => literal_error = Some(message), }; } if let Some(literal_error) = literal_error { - span_err!(diagnostic, item.span(), E0589, - "invalid `repr(align)` attribute: {}", literal_error); + span_err!( + diagnostic, + item.span(), + E0589, + "invalid `repr(align)` attribute: {}", + literal_error + ); } } else { if let Some(meta_item) = item.meta_item() { if meta_item.check_name(sym::align) { if let MetaItemKind::NameValue(ref value) = meta_item.kind { recognised = true; - let mut err = struct_span_err!(diagnostic, item.span(), E0693, - "incorrect `repr(align)` attribute format"); + let mut err = struct_span_err!( + diagnostic, + item.span(), + E0693, + "incorrect `repr(align)` attribute format" + ); match value.kind { ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => { err.span_suggestion( item.span(), "use parentheses instead", format!("align({})", int), - Applicability::MachineApplicable + Applicability::MachineApplicable, ); } ast::LitKind::Str(s, _) => { @@ -875,7 +933,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> { item.span(), "use parentheses instead", format!("align({})", s), - Applicability::MachineApplicable + Applicability::MachineApplicable, ); } _ => {} @@ -887,8 +945,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> { } if !recognised { // Not a word we recognize - span_err!(diagnostic, item.span(), E0552, - "unrecognized representation hint"); + span_err!(diagnostic, item.span(), E0552, "unrecognized representation hint"); } } } @@ -912,7 +969,7 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> { sym::u128 => Some(UnsignedInt(ast::UintTy::U128)), sym::isize => Some(SignedInt(ast::IntTy::Isize)), sym::usize => Some(UnsignedInt(ast::UintTy::Usize)), - _ => None + _ => None, } } @@ -922,7 +979,8 @@ pub enum TransparencyError { } pub fn find_transparency( - attrs: &[Attribute], is_legacy: bool + attrs: &[Attribute], + is_legacy: bool, ) -> (Transparency, Option<TransparencyError>) { let mut transparency = None; let mut error = None; @@ -932,15 +990,18 @@ pub fn find_transparency( error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span)); break; } else if let Some(value) = attr.value_str() { - transparency = Some((match &*value.as_str() { - "transparent" => Transparency::Transparent, - "semitransparent" => Transparency::SemiTransparent, - "opaque" => Transparency::Opaque, - _ => { - error = Some(TransparencyError::UnknownTransparency(value, attr.span)); - continue; - } - }, attr.span)); + transparency = Some(( + match &*value.as_str() { + "transparent" => Transparency::Transparent, + "semitransparent" => Transparency::SemiTransparent, + "opaque" => Transparency::Opaque, + _ => { + error = Some(TransparencyError::UnknownTransparency(value, attr.span)); + continue; + } + }, + attr.span, + )); } } } diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index ae6d50ba083..82685e98386 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -2,21 +2,21 @@ mod builtin; +pub use crate::ast::Attribute; pub use builtin::*; pub use IntType::*; pub use ReprAttr::*; pub use StabilityLevel::*; -pub use crate::ast::Attribute; use crate::ast; -use crate::ast::{AttrVec, AttrItem, AttrId, AttrKind, AttrStyle, Name, Ident, Path, PathSegment}; +use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Ident, Name, Path, PathSegment}; +use crate::ast::{Expr, GenericParam, Item, Lit, LitKind, Local, Stmt, StmtKind}; use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem}; -use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam}; use crate::mut_visit::visit_clobber; -use crate::source_map::{BytePos, Spanned}; -use crate::token::{self, Token}; use crate::ptr::P; +use crate::source_map::{BytePos, Spanned}; use crate::symbol::{sym, Symbol}; +use crate::token::{self, Token}; use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint}; use crate::GLOBALS; @@ -34,9 +34,7 @@ pub fn mark_used(attr: &Attribute) { } pub fn is_used(attr: &Attribute) -> bool { - GLOBALS.with(|globals| { - globals.used_attrs.lock().contains(attr.id) - }) + GLOBALS.with(|globals| globals.used_attrs.lock().contains(attr.id)) } pub fn mark_known(attr: &Attribute) { @@ -47,9 +45,7 @@ pub fn mark_known(attr: &Attribute) { } pub fn is_known(attr: &Attribute) -> bool { - GLOBALS.with(|globals| { - globals.known_attrs.lock().contains(attr.id) - }) + GLOBALS.with(|globals| globals.known_attrs.lock().contains(attr.id)) } pub fn is_known_lint_tool(m_item: Ident) -> bool { @@ -61,7 +57,7 @@ impl NestedMetaItem { pub fn meta_item(&self) -> Option<&MetaItem> { match *self { NestedMetaItem::MetaItem(ref item) => Some(item), - _ => None + _ => None, } } @@ -69,7 +65,7 @@ impl NestedMetaItem { pub fn literal(&self) -> Option<&Lit> { match *self { NestedMetaItem::Literal(ref lit) => Some(lit), - _ => None + _ => None, } } @@ -94,18 +90,18 @@ impl NestedMetaItem { /// Returns a name and single literal value tuple of the `MetaItem`. pub fn name_value_literal(&self) -> Option<(Name, &Lit)> { - self.meta_item().and_then( - |meta_item| meta_item.meta_item_list().and_then( - |meta_item_list| { - if meta_item_list.len() == 1 { - if let Some(ident) = meta_item.ident() { - if let Some(lit) = meta_item_list[0].literal() { - return Some((ident.name, lit)); - } + self.meta_item().and_then(|meta_item| { + meta_item.meta_item_list().and_then(|meta_item_list| { + if meta_item_list.len() == 1 { + if let Some(ident) = meta_item.ident() { + if let Some(lit) = meta_item_list[0].literal() { + return Some((ident.name, lit)); } } - None - })) + } + None + }) + }) } /// Gets a list of inner meta items from a list `MetaItem` type. @@ -176,21 +172,17 @@ impl Attribute { pub fn value_str(&self) -> Option<Symbol> { match self.kind { - AttrKind::Normal(ref item) => { - item.meta(self.span).and_then(|meta| meta.value_str()) - } + AttrKind::Normal(ref item) => item.meta(self.span).and_then(|meta| meta.value_str()), AttrKind::DocComment(comment) => Some(comment), } } pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> { match self.kind { - AttrKind::Normal(ref item) => { - match item.meta(self.span) { - Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list), - _ => None - } - } + AttrKind::Normal(ref item) => match item.meta(self.span) { + Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list), + _ => None, + }, AttrKind::DocComment(_) => None, } } @@ -216,11 +208,7 @@ impl Attribute { impl MetaItem { /// For a single-segment meta item, returns its name; otherwise, returns `None`. pub fn ident(&self) -> Option<Ident> { - if self.path.segments.len() == 1 { - Some(self.path.segments[0].ident) - } else { - None - } + if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None } } pub fn name_or_empty(&self) -> Symbol { self.ident().unwrap_or(Ident::invalid()).name @@ -238,20 +226,18 @@ impl MetaItem { pub fn value_str(&self) -> Option<Symbol> { match self.kind { - MetaItemKind::NameValue(ref v) => { - match v.kind { - LitKind::Str(ref s, _) => Some(*s), - _ => None, - } + MetaItemKind::NameValue(ref v) => match v.kind { + LitKind::Str(ref s, _) => Some(*s), + _ => None, }, - _ => None + _ => None, } } pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> { match self.kind { MetaItemKind::List(ref l) => Some(&l[..]), - _ => None + _ => None, } } @@ -311,8 +297,9 @@ impl Attribute { pub fn meta(&self) -> Option<MetaItem> { match self.kind { AttrKind::Normal(ref item) => item.meta(self.span), - AttrKind::DocComment(comment) => - Some(mk_name_value_item_str(Ident::new(sym::doc, self.span), comment, self.span)), + AttrKind::DocComment(comment) => { + Some(mk_name_value_item_str(Ident::new(sym::doc, self.span), comment, self.span)) + } } } } @@ -358,12 +345,7 @@ pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attri } pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute { - Attribute { - kind: AttrKind::Normal(item), - id: mk_attr_id(), - style, - span, - } + Attribute { kind: AttrKind::Normal(item), id: mk_attr_id(), style, span } } /// Returns an inner attribute with the given value and span. @@ -377,24 +359,15 @@ pub fn mk_attr_outer(item: MetaItem) -> Attribute { } pub fn mk_doc_comment(style: AttrStyle, comment: Symbol, span: Span) -> Attribute { - Attribute { - kind: AttrKind::DocComment(comment), - id: mk_attr_id(), - style, - span, - } + Attribute { kind: AttrKind::DocComment(comment), id: mk_attr_id(), style, span } } pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool { - items.iter().any(|item| { - item.check_name(name) - }) + items.iter().any(|item| item.check_name(name)) } pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool { - attrs.iter().any(|item| { - item.check_name(name) - }) + attrs.iter().any(|item| item.check_name(name)) } pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> { @@ -406,34 +379,31 @@ pub fn allow_internal_unstable<'a>( span_diagnostic: &'a errors::Handler, ) -> Option<impl Iterator<Item = Symbol> + 'a> { find_by_name(attrs, sym::allow_internal_unstable).and_then(|attr| { - attr.meta_item_list().or_else(|| { - span_diagnostic.span_err( - attr.span, - "allow_internal_unstable expects list of feature names" - ); - None - }).map(|features| features.into_iter().filter_map(move |it| { - let name = it.ident().map(|ident| ident.name); - if name.is_none() { - span_diagnostic.span_err( - it.span(), - "`allow_internal_unstable` expects feature names", - ) - } - name - })) + attr.meta_item_list() + .or_else(|| { + span_diagnostic + .span_err(attr.span, "allow_internal_unstable expects list of feature names"); + None + }) + .map(|features| { + features.into_iter().filter_map(move |it| { + let name = it.ident().map(|ident| ident.name); + if name.is_none() { + span_diagnostic + .span_err(it.span(), "`allow_internal_unstable` expects feature names") + } + name + }) + }) }) } -pub fn filter_by_name(attrs: &[Attribute], name: Symbol) - -> impl Iterator<Item=&Attribute> { +pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> { attrs.iter().filter(move |attr| attr.check_name(name)) } pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> { - attrs.iter() - .find(|at| at.check_name(name)) - .and_then(|at| at.value_str()) + attrs.iter().find(|at| at.check_name(name)).and_then(|at| at.value_str()) } impl MetaItem { @@ -443,9 +413,8 @@ impl MetaItem { for (i, segment) in self.path.segments.iter().enumerate() { let is_first = i == 0; if !is_first { - let mod_sep_span = Span::new(last_pos, - segment.ident.span.lo(), - segment.ident.span.ctxt()); + let mod_sep_span = + Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt()); idents.push(TokenTree::token(token::ModSep, mod_sep_span).into()); } idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into()); @@ -456,15 +425,16 @@ impl MetaItem { } fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem> - where I: Iterator<Item = TokenTree>, + where + I: Iterator<Item = TokenTree>, { // FIXME: Share code with `parse_path`. let path = match tokens.next() { - Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span })) | - Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: { + Some(TokenTree::Token(Token { kind: kind @ token::Ident(..), span })) + | Some(TokenTree::Token(Token { kind: kind @ token::ModSep, span })) => 'arm: { let mut segments = if let token::Ident(name, _) = kind { - if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) - = tokens.peek() { + if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek() + { tokens.next(); vec![PathSegment::from_ident(Ident::new(name, span))] } else { @@ -474,14 +444,15 @@ impl MetaItem { vec![PathSegment::path_root(span)] }; loop { - if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) - = tokens.next() { + if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) = + tokens.next() + { segments.push(PathSegment::from_ident(Ident::new(name, span))); } else { return None; } - if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) - = tokens.peek() { + if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek() + { tokens.next(); } else { break; @@ -524,7 +495,9 @@ impl MetaItemKind { tts.extend(item.token_trees_and_joints()) } MacArgs::Delimited( - DelimSpan::from_single(span), MacDelimiter::Parenthesis, TokenStream::new(tts) + DelimSpan::from_single(span), + MacDelimiter::Parenthesis, + TokenStream::new(tts), ) } } @@ -534,10 +507,7 @@ impl MetaItemKind { match *self { MetaItemKind::Word => vec![], MetaItemKind::NameValue(ref lit) => { - vec![ - TokenTree::token(token::Eq, span).into(), - lit.token_tree().into(), - ] + vec![TokenTree::token(token::Eq, span).into(), lit.token_tree().into()] } MetaItemKind::List(ref list) => { let mut tokens = Vec::new(); @@ -552,7 +522,8 @@ impl MetaItemKind { DelimSpan::from_single(span), token::Paren, TokenStream::new(tokens).into(), - ).into() + ) + .into(), ] } } @@ -576,16 +547,18 @@ impl MetaItemKind { tokens: &mut impl Iterator<Item = TokenTree>, ) -> Option<MetaItemKind> { match tokens.next() { - Some(TokenTree::Token(token)) => - Lit::from_token(&token).ok().map(MetaItemKind::NameValue), + Some(TokenTree::Token(token)) => { + Lit::from_token(&token).ok().map(MetaItemKind::NameValue) + } _ => None, } } fn from_mac_args(args: &MacArgs) -> Option<MetaItemKind> { match args { - MacArgs::Delimited(_, MacDelimiter::Parenthesis, tokens) => - MetaItemKind::list_from_tokens(tokens.clone()), + MacArgs::Delimited(_, MacDelimiter::Parenthesis, tokens) => { + MetaItemKind::list_from_tokens(tokens.clone()) + } MacArgs::Delimited(..) => None, MacArgs::Eq(_, tokens) => { assert!(tokens.len() == 1); @@ -630,7 +603,8 @@ impl NestedMetaItem { } fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem> - where I: Iterator<Item = TokenTree>, + where + I: Iterator<Item = TokenTree>, { if let Some(TokenTree::Token(token)) = tokens.peek() { if let Ok(lit) = Lit::from_token(token) { @@ -649,7 +623,9 @@ pub trait HasAttrs: Sized { } impl<T: HasAttrs> HasAttrs for Spanned<T> { - fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() } + fn attrs(&self) -> &[ast::Attribute] { + self.node.attrs() + } fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F) { self.node.visit_attrs(f); } |
