From 6cecdd681f13c0800ba1383072725761cc17e57d Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 25 Jul 2018 08:34:43 +0900 Subject: Put lifetimes after trait when they gets orphaned --- src/items.rs | 3 +-- src/types.rs | 53 ++++++++++++++++++++++++++++++++++++++++++----------- src/visitor.rs | 1 - 3 files changed, 43 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/items.rs b/src/items.rs index 6b81dbaf796..7da84bfe68f 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1752,11 +1752,10 @@ pub fn rewrite_associated_impl_type( ident: ast::Ident, defaultness: ast::Defaultness, ty_opt: Option<&ptr::P>, - generic_bounds_opt: Option<&ast::GenericBounds>, context: &RewriteContext, indent: Indent, ) -> Option { - let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?; + let result = rewrite_associated_type(ident, ty_opt, None, context, indent)?; match defaultness { ast::Defaultness::Default => Some(format!("default {}", result)), diff --git a/src/types.rs b/src/types.rs index d6e4001eacf..a67ba876591 100644 --- a/src/types.rs +++ b/src/types.rs @@ -27,8 +27,8 @@ use rewrite::{Rewrite, RewriteContext}; use shape::Shape; use spanned::Spanned; use utils::{ - colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, last_line_width, - mk_sp, rewrite_ident, + colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, + last_line_extendable, last_line_width, mk_sp, rewrite_ident, }; #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -733,15 +733,28 @@ fn rewrite_bare_fn( Some(result) } -fn join_bounds( +fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool { + let is_trait = |b: &ast::GenericBound| match b { + ast::GenericBound::Outlives(..) => false, + ast::GenericBound::Trait(..) => true, + }; + let is_lifetime = |b: &ast::GenericBound| !is_trait(b); + let last_trait_index = generic_bounds.iter().rposition(is_trait); + let first_lifetime_index = generic_bounds.iter().position(is_lifetime); + match (last_trait_index, first_lifetime_index) { + (Some(last_trait_index), Some(first_lifetime_index)) => { + last_trait_index < first_lifetime_index + } + _ => true, + } +} + +fn join_bounds( context: &RewriteContext, shape: Shape, - items: &[T], + items: &[ast::GenericBound], need_indent: bool, -) -> Option -where - T: Rewrite, -{ +) -> Option { // Try to join types in a single line let joiner = match context.config.type_punctuation_density() { TypeDensity::Compressed => "+", @@ -752,7 +765,7 @@ where .map(|item| item.rewrite(context, shape)) .collect::>>()?; let result = type_strs.join(joiner); - if items.len() == 1 || (!result.contains('\n') && result.len() <= shape.width) { + if items.len() <= 1 || (!result.contains('\n') && result.len() <= shape.width) { return Some(result); } @@ -769,8 +782,26 @@ where (type_strs, shape.indent) }; - let joiner = format!("{}+ ", offset.to_string_with_newline(context.config)); - Some(type_strs.join(&joiner)) + let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b { + ast::GenericBound::Outlives(..) => true, + ast::GenericBound::Trait(..) => last_line_extendable(s), + }; + let mut result = String::with_capacity(128); + result.push_str(&type_strs[0]); + let mut can_be_put_on_the_same_line = is_bound_extendable(&result, &items[0]); + let generic_bounds_in_order = is_generic_bounds_in_order(items); + for (bound, bound_str) in items[1..].iter().zip(type_strs[1..].iter()) { + if generic_bounds_in_order && can_be_put_on_the_same_line { + result.push_str(joiner); + } else { + result.push_str(&offset.to_string_with_newline(context.config)); + result.push_str("+ "); + } + result.push_str(bound_str); + can_be_put_on_the_same_line = is_bound_extendable(bound_str, bound); + } + + Some(result) } pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize) -> bool { diff --git a/src/visitor.rs b/src/visitor.rs index 4fabb3301fc..fdbc9907f22 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -515,7 +515,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { ii.ident, ii.defaultness, Some(ty), - None, &self.get_context(), self.block_indent, ); -- cgit 1.4.1-3-g733a5