]> git.lizzy.rs Git - rust.git/blobdiff - src/types.rs
Merge pull request #3035 from topecongiro/issue-3006
[rust.git] / src / types.rs
index 360c7304988ba9667d388e9225339d2cc6a021ab..07794030ba8deab90d5071fc1011e4c0b20c6a75 100644 (file)
 
 use config::lists::*;
 use syntax::ast::{self, FunctionRetTy, Mutability};
-use syntax::codemap::{self, BytePos, Span};
+use syntax::source_map::{self, BytePos, Span};
 use syntax::symbol::keywords;
 
-use codemap::SpanUtils;
 use config::{IndentStyle, TypeDensity};
 use expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ToExpr};
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
@@ -25,6 +24,7 @@
 use pairs::{rewrite_pair, PairParts};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
+use source_map::SpanUtils;
 use spanned::Spanned;
 use utils::{
     colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
@@ -267,7 +267,7 @@ fn rewrite_segment(
             ast::GenericArgs::Parenthesized(ref data) => {
                 let output = match data.output {
                     Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
-                    None => FunctionRetTy::Default(codemap::DUMMY_SP),
+                    None => FunctionRetTy::Default(source_map::DUMMY_SP),
                 };
                 result.push_str(&format_function_type(
                     data.inputs.iter().map(|x| &**x),
@@ -298,6 +298,8 @@ fn format_function_type<'a, I>(
     <I as Iterator>::Item: Deref,
     <I::Item as Deref>::Target: Rewrite + Spanned + 'a,
 {
+    debug!("format_function_type {:#?}", shape);
+
     // Code for handling variadics is somewhat duplicated for items, but they
     // are different enough to need some serious refactoring to share code.
     enum ArgumentKind<T>
@@ -398,7 +400,7 @@ enum ArgumentKind<T>
             shape.block().indent.to_string_with_newline(context.config),
         )
     };
-    if last_line_width(&args) + first_line_width(&output) <= shape.width {
+    if output.is_empty() || last_line_width(&args) + first_line_width(&output) <= shape.width {
         Some(format!("{}{}", args, output))
     } else {
         Some(format!(
@@ -520,7 +522,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         }
 
         let span = mk_sp(self.get(0)?.span().lo(), self.last()?.span().hi());
-        let has_paren = context.snippet(span).starts_with("(");
+        let has_paren = context.snippet(span).starts_with('(');
         let bounds_shape = if has_paren {
             shape.offset_left(1)?.sub_width(1)?
         } else {
@@ -706,6 +708,8 @@ fn rewrite_bare_fn(
     context: &RewriteContext,
     shape: Shape,
 ) -> Option<String> {
+    debug!("rewrite_bare_fn {:#?}", shape);
+
     let mut result = String::with_capacity(128);
 
     if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params)
@@ -728,7 +732,11 @@ fn rewrite_bare_fn(
 
     result.push_str("fn");
 
-    let func_ty_shape = shape.offset_left(result.len())?;
+    let func_ty_shape = if context.use_block_indent() {
+        shape.offset_left(result.len())?
+    } else {
+        shape.visual_indent(result.len()).sub_width(result.len())?
+    };
 
     let rewrite = format_function_type(
         bare_fn.decl.inputs.iter(),