]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_parse/config.rs
nix syntax::errors & prefer rustc_errors over errors
[rust.git] / src / librustc_parse / config.rs
index 1e9203f377f383b028978416b6f6c34d5b2a9788..f2ffd9470ed7601908ae67a145307a6e31329e40 100644 (file)
@@ -1,7 +1,7 @@
 //! Process the potential `cfg` attributes on a module.
 //! Also determine if the module should be included in this configuration.
 //!
-//! This module properly belongs in syntax_expand, but for now it's tied into
+//! This module properly belongs in rustc_expand, but for now it's tied into
 //! parsing, so we leave it here to avoid complicated out-of-line dependencies.
 //!
 //! A principled solution to this wrong location would be to implement [#64197].
@@ -9,19 +9,19 @@
 //! [#64197]: https://github.com/rust-lang/rust/issues/64197
 
 use crate::{parse_in, validate_attr};
-use rustc_feature::Features;
 use rustc_errors::Applicability;
+use rustc_feature::Features;
+use rustc_span::edition::Edition;
+use rustc_span::symbol::sym;
+use rustc_span::Span;
+use syntax::ast::{self, AttrItem, Attribute, MetaItem};
+use syntax::attr;
 use syntax::attr::HasAttrs;
 use syntax::feature_gate::{feature_err, get_features};
-use syntax::attr;
-use syntax::ast::{self, Attribute, AttrItem, MetaItem};
-use syntax::edition::Edition;
 use syntax::mut_visit::*;
 use syntax::ptr::P;
 use syntax::sess::ParseSess;
 use syntax::util::map_in_place::MapInPlace;
-use syntax_pos::Span;
-use syntax_pos::symbol::sym;
 
 use smallvec::SmallVec;
 
@@ -32,20 +32,22 @@ pub struct StripUnconfigured<'a> {
 }
 
 // `cfg_attr`-process the crate's attributes and compute the crate's features.
-pub fn features(mut krate: ast::Crate, sess: &ParseSess, edition: Edition,
-                allow_features: &Option<Vec<String>>) -> (ast::Crate, Features) {
+pub fn features(
+    mut krate: ast::Crate,
+    sess: &ParseSess,
+    edition: Edition,
+    allow_features: &Option<Vec<String>>,
+) -> (ast::Crate, Features) {
     let features;
     {
-        let mut strip_unconfigured = StripUnconfigured {
-            sess,
-            features: None,
-        };
+        let mut strip_unconfigured = StripUnconfigured { sess, features: None };
 
         let unconfigured_attrs = krate.attrs.clone();
         let err_count = sess.span_diagnostic.err_count();
         if let Some(attrs) = strip_unconfigured.configure(krate.attrs) {
             krate.attrs = attrs;
-        } else { // the entire crate is unconfigured
+        } else {
+            // the entire crate is unconfigured
             krate.attrs = Vec::new();
             krate.module.items = Vec::new();
             return (krate, Features::default());
@@ -70,7 +72,7 @@ macro_rules! configure {
             Some(node) => node,
             None => return Default::default(),
         }
-    }
+    };
 }
 
 const CFG_ATTR_GRAMMAR_HELP: &str = "#[cfg_attr(condition, attribute, other_attribute, ...)]";
@@ -192,26 +194,38 @@ pub fn in_cfg(&self, attrs: &[Attribute]) -> bool {
 
             let meta_item = match validate_attr::parse_meta(self.sess, attr) {
                 Ok(meta_item) => meta_item,
-                Err(mut err) => { err.emit(); return true; }
+                Err(mut err) => {
+                    err.emit();
+                    return true;
+                }
             };
             let nested_meta_items = if let Some(nested_meta_items) = meta_item.meta_item_list() {
                 nested_meta_items
             } else {
-                return error(meta_item.span, "`cfg` is not followed by parentheses",
-                                             "cfg(/* predicate */)");
+                return error(
+                    meta_item.span,
+                    "`cfg` is not followed by parentheses",
+                    "cfg(/* predicate */)",
+                );
             };
 
             if nested_meta_items.is_empty() {
                 return error(meta_item.span, "`cfg` predicate is not specified", "");
             } else if nested_meta_items.len() > 1 {
-                return error(nested_meta_items.last().unwrap().span(),
-                             "multiple `cfg` predicates are specified", "");
+                return error(
+                    nested_meta_items.last().unwrap().span(),
+                    "multiple `cfg` predicates are specified",
+                    "",
+                );
             }
 
             match nested_meta_items[0].meta_item() {
                 Some(meta_item) => attr::cfg_matches(meta_item, self.sess, self.features),
-                None => error(nested_meta_items[0].span(),
-                              "`cfg` predicate key cannot be a literal", ""),
+                None => error(
+                    nested_meta_items[0].span(),
+                    "`cfg` predicate key cannot be a literal",
+                    "",
+                ),
             }
         })
     }
@@ -227,10 +241,12 @@ fn visit_expr_attrs(&mut self, attrs: &[Attribute]) {
     /// If attributes are not allowed on expressions, emit an error for `attr`
     pub fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
         if !self.features.map(|features| features.stmt_expr_attributes).unwrap_or(true) {
-            let mut err = feature_err(self.sess,
-                                      sym::stmt_expr_attributes,
-                                      attr.span,
-                                      "attributes on expressions are experimental");
+            let mut err = feature_err(
+                self.sess,
+                sym::stmt_expr_attributes,
+                attr.span,
+                "attributes on expressions are experimental",
+            );
 
             if attr.is_doc_comment() {
                 err.help("`///` is for documentation comments. For a plain comment, use `//`.");
@@ -251,16 +267,18 @@ pub fn configure_generic_params(&mut self, params: &mut Vec<ast::GenericParam>)
 
     fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
         match vdata {
-            ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) =>
-                fields.flat_map_in_place(|field| self.configure(field)),
+            ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
+                fields.flat_map_in_place(|field| self.configure(field))
+            }
             ast::VariantData::Unit(_) => {}
         }
     }
 
     pub fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
         match item {
-            ast::ItemKind::Struct(def, _generics) |
-            ast::ItemKind::Union(def, _generics) => self.configure_variant_data(def),
+            ast::ItemKind::Struct(def, _generics) | ast::ItemKind::Union(def, _generics) => {
+                self.configure_variant_data(def)
+            }
             ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
                 variants.flat_map_in_place(|variant| self.configure(variant));
                 for variant in variants {
@@ -344,12 +362,12 @@ fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
         noop_flat_map_item(configure!(self, item), self)
     }
 
-    fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
-        noop_flat_map_impl_item(configure!(self, item), self)
+    fn flat_map_impl_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> {
+        noop_flat_map_assoc_item(configure!(self, item), self)
     }
 
-    fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
-        noop_flat_map_trait_item(configure!(self, item), self)
+    fn flat_map_trait_item(&mut self, item: ast::AssocItem) -> SmallVec<[ast::AssocItem; 1]> {
+        noop_flat_map_assoc_item(configure!(self, item), self)
     }
 
     fn visit_mac(&mut self, _mac: &mut ast::Mac) {