diff options
| author | Nick Cameron <nrc@ncameron.org> | 2018-07-25 21:22:25 +1200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-07-25 21:22:25 +1200 |
| commit | 8ff2836eaf051fe0ef4daea6b07d0fecdb809dc3 (patch) | |
| tree | 6048fa8b07273881392ded30593b41ce0cc0f039 /src | |
| parent | e3fea15ca9b5157f901315595b2c16004d166307 (diff) | |
| parent | 6cecdd681f13c0800ba1383072725761cc17e57d (diff) | |
Merge pull request #2861 from topecongiro/issue-2859
Put lifetimes after trait when they gets orphaned
Diffstat (limited to 'src')
| -rw-r--r-- | src/items.rs | 3 | ||||
| -rw-r--r-- | src/types.rs | 53 | ||||
| -rw-r--r-- | src/visitor.rs | 1 |
3 files changed, 43 insertions, 14 deletions
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<ast::Ty>>, - generic_bounds_opt: Option<&ast::GenericBounds>, context: &RewriteContext, indent: Indent, ) -> Option<String> { - 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<T>( +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<String> -where - T: Rewrite, -{ +) -> Option<String> { // 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::<Option<Vec<_>>>()?; 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, ); |
