]> git.lizzy.rs Git - rust.git/blobdiff - src/visitor.rs
Format exitential type
[rust.git] / src / visitor.rs
index bf1f95381d9fccb454e061a4c73d9e3dd2ccb164..4fabb3301fc0b2a589a62abc9bf7aeb69e4cebdd 100644 (file)
@@ -19,8 +19,9 @@
 use config::{BraceStyle, Config};
 use items::{
     format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
-    rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
-    rewrite_type_alias, FnSig, StaticParts, StructParts,
+    rewrite_associated_impl_type, rewrite_associated_type, rewrite_existential_impl_type,
+    rewrite_existential_type, rewrite_extern_crate, rewrite_type_alias, FnSig, StaticParts,
+    StructParts,
 };
 use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
 use rewrite::{Rewrite, RewriteContext};
@@ -28,7 +29,7 @@
 use spanned::Spanned;
 use utils::{
     self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec,
-    DEPR_SKIP_ANNOTATION,
+    rewrite_ident, DEPR_SKIP_ANNOTATION,
 };
 use {ErrorKind, FormatReport, FormattingError};
 
@@ -70,6 +71,7 @@ pub struct FmtVisitor<'a> {
     pub snippet_provider: &'a SnippetProvider<'a>,
     pub line_number: usize,
     pub skipped_range: Vec<(usize, usize)>,
+    pub macro_rewrite_failure: bool,
     pub(crate) report: FormatReport,
 }
 
@@ -251,6 +253,7 @@ fn close_block(&mut self, unindent_comment: bool) {
 
     // Note that this only gets called for function definitions. Required methods
     // on traits do not get handled here.
+    // FIXME(topecongiro) Format async fn (#2812).
     fn visit_fn(
         &mut self,
         fk: visit::FnKind,
@@ -263,7 +266,7 @@ fn visit_fn(
         let indent = self.block_indent;
         let block;
         let rewrite = match fk {
-            visit::FnKind::ItemFn(ident, _, _, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
+            visit::FnKind::ItemFn(ident, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
                 block = b;
                 self.rewrite_fn(
                     indent,
@@ -353,13 +356,13 @@ pub fn visit_item(&mut self, item: &ast::Item) {
                 let rw = format_trait(&self.get_context(), item, self.block_indent);
                 self.push_rewrite(item.span, rw);
             }
-            ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => {
+            ast::ItemKind::TraitAlias(ref generics, ref generic_bounds) => {
                 let shape = Shape::indented(self.block_indent, self.config);
                 let rw = format_trait_alias(
                     &self.get_context(),
                     item.ident,
                     generics,
-                    ty_param_bounds,
+                    generic_bounds,
                     shape,
                 );
                 self.push_rewrite(item.span, rw);
@@ -391,10 +394,10 @@ pub fn visit_item(&mut self, item: &ast::Item) {
             ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
                 self.visit_static(&StaticParts::from_item(item));
             }
-            ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
+            ast::ItemKind::Fn(ref decl, fn_header, ref generics, ref body) => {
                 let inner_attrs = inner_attributes(&item.attrs);
                 self.visit_fn(
-                    visit::FnKind::ItemFn(item.ident, unsafety, constness, abi, &item.vis, body),
+                    visit::FnKind::ItemFn(item.ident, fn_header, &item.vis, body),
                     generics,
                     decl,
                     item.span,
@@ -410,7 +413,17 @@ pub fn visit_item(&mut self, item: &ast::Item) {
                     ty,
                     generics,
                     &item.vis,
-                    item.span,
+                );
+                self.push_rewrite(item.span, rewrite);
+            }
+            ast::ItemKind::Existential(ref generic_bounds, ref generics) => {
+                let rewrite = rewrite_existential_type(
+                    &self.get_context(),
+                    self.block_indent,
+                    item.ident,
+                    generic_bounds,
+                    generics,
+                    &item.vis,
                 );
                 self.push_rewrite(item.span, rewrite);
             }
@@ -460,11 +473,11 @@ pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
                     Some(&inner_attrs),
                 );
             }
-            ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
+            ast::TraitItemKind::Type(ref generic_bounds, ref type_default) => {
                 let rewrite = rewrite_associated_type(
                     ti.ident,
                     type_default.as_ref(),
-                    Some(type_param_bounds),
+                    Some(generic_bounds),
                     &self.get_context(),
                     self.block_indent,
                 );
@@ -508,6 +521,15 @@ pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
                 );
                 self.push_rewrite(ii.span, rewrite);
             }
+            ast::ImplItemKind::Existential(ref generic_bounds) => {
+                let rewrite = rewrite_existential_impl_type(
+                    &self.get_context(),
+                    ii.ident,
+                    generic_bounds,
+                    self.block_indent,
+                );
+                self.push_rewrite(ii.span, rewrite);
+            }
             ast::ImplItemKind::Macro(ref mac) => {
                 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
             }
@@ -519,7 +541,7 @@ fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPos
 
         // 1 = ;
         let shape = self.shape().sub_width(1).unwrap();
-        let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
+        let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
         self.push_rewrite(mac.span, rewrite);
     }
 
@@ -578,6 +600,7 @@ pub(crate) fn from_codemap(
             snippet_provider,
             line_number: 0,
             skipped_range: vec![],
+            macro_rewrite_failure: false,
             report,
         }
     }
@@ -594,7 +617,7 @@ pub fn snippet(&'b self, span: Span) -> &'a str {
     pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
         for attr in attrs {
             if attr.name() == DEPR_SKIP_ANNOTATION {
-                let file_name = self.codemap.span_to_filename(attr.span);
+                let file_name = self.codemap.span_to_filename(attr.span).into();
                 self.report.append(
                     file_name,
                     vec![FormattingError::from_span(
@@ -607,7 +630,7 @@ pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -
                 if attr.path.segments.len() == 1
                     || attr.path.segments[1].ident.to_string() != "skip"
                 {
-                    let file_name = self.codemap.span_to_filename(attr.span);
+                    let file_name = self.codemap.span_to_filename(attr.span).into();
                     self.report.append(
                         file_name,
                         vec![FormattingError::from_span(
@@ -680,9 +703,12 @@ fn format_mod(
         attrs: &[ast::Attribute],
         is_internal: bool,
     ) {
-        self.push_str(&*utils::format_visibility(vis));
+        let vis_str = utils::format_visibility(&self.get_context(), vis);
+        self.push_str(&*vis_str);
         self.push_str("mod ");
-        self.push_str(&ident.to_string());
+        // Calling `to_owned()` to work around borrow checker.
+        let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
+        self.push_str(&ident_str);
 
         if is_internal {
             match self.config.brace_style() {
@@ -736,6 +762,20 @@ pub fn skip_empty_lines(&mut self, end_pos: BytePos) {
         }
     }
 
+    pub fn with_context<F>(&mut self, f: F) -> Option<String>
+    where
+        F: Fn(&RewriteContext) -> Option<String>,
+    {
+        let result;
+        let macro_rewrite_failure = {
+            let context = self.get_context();
+            result = f(&context);
+            unsafe { *context.macro_rewrite_failure.as_ptr() }
+        };
+        self.macro_rewrite_failure |= macro_rewrite_failure;
+        result
+    }
+
     pub fn get_context(&self) -> RewriteContext {
         RewriteContext {
             parse_session: self.parse_session,
@@ -746,6 +786,7 @@ pub fn get_context(&self) -> RewriteContext {
             is_if_else_block: RefCell::new(false),
             force_one_line_chain: RefCell::new(false),
             snippet_provider: self.snippet_provider,
+            macro_rewrite_failure: RefCell::new(false),
             report: self.report.clone(),
         }
     }