]> git.lizzy.rs Git - rust.git/blobdiff - src/items.rs
Merge pull request #3035 from topecongiro/issue-3006
[rust.git] / src / items.rs
index 8c32d393df4b2f263034b780597085c472989c41..d1522d4648182cdc4459de9128cd66f087b88312 100644 (file)
 use config::lists::*;
 use regex::Regex;
 use rustc_target::spec::abi;
-use syntax::codemap::{self, BytePos, Span};
+use syntax::source_map::{self, BytePos, Span};
 use syntax::visit;
 use syntax::{ast, ptr, symbol};
 
-use codemap::{LineRangeUtils, SpanUtils};
 use comment::{
     combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
     recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
 use overflow;
 use rewrite::{Rewrite, RewriteContext};
 use shape::{Indent, Shape};
+use source_map::{LineRangeUtils, SpanUtils};
 use spanned::Spanned;
 use utils::*;
 use vertical::rewrite_with_alignment;
 use visitor::FmtVisitor;
 
-const DEFAULT_VISIBILITY: ast::Visibility = codemap::Spanned {
+const DEFAULT_VISIBILITY: ast::Visibility = source_map::Spanned {
     node: ast::VisibilityKind::Inherited,
-    span: codemap::DUMMY_SP,
+    span: source_map::DUMMY_SP,
 };
 
 fn type_annotation_separator(config: &Config) -> &str {
@@ -164,6 +164,7 @@ pub struct FnSig<'a> {
     decl: &'a ast::FnDecl,
     generics: &'a ast::Generics,
     abi: abi::Abi,
+    is_async: ast::IsAsync,
     constness: ast::Constness,
     defaultness: ast::Defaultness,
     unsafety: ast::Unsafety,
@@ -180,6 +181,7 @@ pub fn new(
             decl,
             generics,
             abi: abi::Abi::Rust,
+            is_async: ast::IsAsync::NotAsync,
             constness: ast::Constness::NotConst,
             defaultness: ast::Defaultness::Final,
             unsafety: ast::Unsafety::Normal,
@@ -193,6 +195,7 @@ pub fn from_method_sig(
     ) -> FnSig<'a> {
         FnSig {
             unsafety: method_sig.header.unsafety,
+            is_async: method_sig.header.asyncness,
             constness: method_sig.header.constness.node,
             defaultness: ast::Defaultness::Final,
             abi: method_sig.header.abi,
@@ -214,6 +217,7 @@ pub fn from_fn_kind(
                 generics,
                 abi: fn_header.abi,
                 constness: fn_header.constness.node,
+                is_async: fn_header.asyncness,
                 defaultness,
                 unsafety: fn_header.unsafety,
                 visibility: visibility.clone(),
@@ -237,6 +241,7 @@ fn to_str(&self, context: &RewriteContext) -> String {
         result.push_str(format_defaultness(self.defaultness));
         result.push_str(format_constness(self.constness));
         result.push_str(format_unsafety(self.unsafety));
+        result.push_str(format_async(self.is_async));
         result.push_str(&format_abi(
             self.abi,
             context.config.force_explicit_abi(),
@@ -375,16 +380,16 @@ fn single_line_fn(
             return None;
         }
 
-        let codemap = self.get_context().codemap;
+        let source_map = self.get_context().source_map;
 
         if self.config.empty_item_single_line()
-            && is_empty_block(block, None, codemap)
+            && is_empty_block(block, None, source_map)
             && self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
         {
             return Some(format!("{}{{}}", fn_str));
         }
 
-        if self.config.fn_single_line() && is_simple_block_stmt(block, None, codemap) {
+        if self.config.fn_single_line() && is_simple_block_stmt(block, None, source_map) {
             let rewrite = {
                 if let Some(stmt) = block.stmts.first() {
                     match stmt_expr(stmt) {
@@ -524,17 +529,9 @@ fn format_variant_list(
         }
 
         let shape = self.shape().sub_width(2)?;
-        let fmt = ListFormatting {
-            tactic: DefinitiveListTactic::Vertical,
-            separator: ",",
-            trailing_separator: self.config.trailing_comma(),
-            separator_place: SeparatorPlace::Back,
-            shape,
-            ends_with_newline: true,
-            preserve_newline: true,
-            nested: false,
-            config: self.config,
-        };
+        let fmt = ListFormatting::new(shape, self.config)
+            .trailing_separator(self.config.trailing_comma())
+            .preserve_newline(true);
 
         let list = write_list(&items, &fmt)?;
         result.push_str(&list);
@@ -1718,11 +1715,16 @@ fn rewrite_static(
 pub fn rewrite_associated_type(
     ident: ast::Ident,
     ty_opt: Option<&ptr::P<ast::Ty>>,
+    generics: &ast::Generics,
     generic_bounds_opt: Option<&ast::GenericBounds>,
     context: &RewriteContext,
     indent: Indent,
 ) -> Option<String> {
-    let prefix = format!("type {}", rewrite_ident(context, ident));
+    let ident_str = rewrite_ident(context, ident);
+    // 5 = "type "
+    let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
+    let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
+    let prefix = format!("type {}", generics_str);
 
     let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
         if bounds.is_empty() {
@@ -1749,10 +1751,11 @@ pub fn rewrite_associated_type(
 pub fn rewrite_existential_impl_type(
     context: &RewriteContext,
     ident: ast::Ident,
+    generics: &ast::Generics,
     generic_bounds: &ast::GenericBounds,
     indent: Indent,
 ) -> Option<String> {
-    rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
+    rewrite_associated_type(ident, None, generics, Some(generic_bounds), context, indent)
         .map(|s| format!("existential {}", s))
 }
 
@@ -1760,10 +1763,11 @@ pub fn rewrite_associated_impl_type(
     ident: ast::Ident,
     defaultness: ast::Defaultness,
     ty_opt: Option<&ptr::P<ast::Ty>>,
+    generics: &ast::Generics,
     context: &RewriteContext,
     indent: Indent,
 ) -> Option<String> {
-    let result = rewrite_associated_type(ident, ty_opt, None, context, indent)?;
+    let result = rewrite_associated_type(ident, ty_opt, generics, None, context, indent)?;
 
     match defaultness {
         ast::Defaultness::Default => Some(format!("default {}", result)),
@@ -2043,18 +2047,16 @@ fn rewrite_fn_base(
         let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
         // Put the closing brace on the next line if it overflows the max width.
         // 1 = `)`
-        if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
-            result.push('\n');
-        }
+        let closing_paren_overflow_max_width =
+            fd.inputs.is_empty() && used_width + 1 > context.config.max_width();
         // If the last line of args contains comment, we cannot put the closing paren
         // on the same line.
-        if arg_str
+        args_last_line_contains_comment = arg_str
             .lines()
             .last()
-            .map_or(false, |last_line| last_line.contains("//"))
-        {
-            args_last_line_contains_comment = true;
-            result.push_str(&arg_indent.to_string_with_newline(context.config));
+            .map_or(false, |last_line| last_line.contains("//"));
+        if closing_paren_overflow_max_width || args_last_line_contains_comment {
+            result.push_str(&indent.to_string_with_newline(context.config));
         }
         result.push(')');
     }
@@ -2235,8 +2237,10 @@ fn rewrite_args(
 ) -> Option<String> {
     let mut arg_item_strs = args
         .iter()
-        .map(|arg| arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent)))
-        .collect::<Option<Vec<_>>>()?;
+        .map(|arg| {
+            arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent))
+                .unwrap_or_else(|| context.snippet(arg.span()).to_owned())
+        }).collect::<Vec<_>>();
 
     // Account for sugary self.
     // FIXME: the comment for the self argument is dropped. This is blocked
@@ -2355,22 +2359,16 @@ enum ArgumentKind<'a> {
 
     debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
 
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: if variadic {
-            SeparatorTactic::Never
-        } else {
-            trailing_comma
-        },
-        separator_place: SeparatorPlace::Back,
-        shape: Shape::legacy(budget, indent),
-        ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
-        preserve_newline: true,
-        nested: false,
-        config: context.config,
+    let trailing_separator = if variadic {
+        SeparatorTactic::Never
+    } else {
+        trailing_comma
     };
-
+    let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
+        .tactic(tactic)
+        .trailing_separator(trailing_separator)
+        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
+        .preserve_newline(true);
     write_list(&arg_items, &fmt)
 }
 
@@ -2546,17 +2544,10 @@ fn rewrite_where_clause_rfc_style(
         DefinitiveListTactic::Vertical
     };
 
-    let fmt = ListFormatting {
-        tactic: shape_tactic,
-        separator: ",",
-        trailing_separator: comma_tactic,
-        separator_place: SeparatorPlace::Back,
-        shape: clause_shape,
-        ends_with_newline: true,
-        preserve_newline: true,
-        nested: false,
-        config: context.config,
-    };
+    let fmt = ListFormatting::new(clause_shape, context.config)
+        .tactic(shape_tactic)
+        .trailing_separator(comma_tactic)
+        .preserve_newline(true);
     let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
 
     let comment_separator = |comment: &str, shape: Shape| {
@@ -2661,17 +2652,11 @@ fn rewrite_where_clause(
         comma_tactic = SeparatorTactic::Never;
     }
 
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: comma_tactic,
-        separator_place: SeparatorPlace::Back,
-        shape: Shape::legacy(budget, offset),
-        ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
-        preserve_newline: true,
-        nested: false,
-        config: context.config,
-    };
+    let fmt = ListFormatting::new(Shape::legacy(budget, offset), context.config)
+        .tactic(tactic)
+        .trailing_separator(comma_tactic)
+        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
+        .preserve_newline(true);
     let preds_str = write_list(&item_vec, &fmt)?;
 
     let end_length = if terminator == "{" {