Skip to content

Put lifetimes after trait when they gets orphaned #2861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
53 changes: 42 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 => "+",
Expand All @@ -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);
}

Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
ii.ident,
ii.defaultness,
Some(ty),
None,
&self.get_context(),
self.block_indent,
);
Expand Down
44 changes: 44 additions & 0 deletions tests/source/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,47 @@ impl CombineTypes {
self.query_callbacks()(&query_id)
}
}

// #2859
pub fn do_something<'a, T: Trait1 + Trait2 + 'a>(&fooo: u32) -> impl Future<
Item = (
impl Future<Item = (
), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError > + 'a,
),
Error = SomeError,
>
+
'a {
}

pub fn do_something<'a, T: Trait1 + Trait2 + 'a>( &fooo: u32,
) -> impl Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
>
+ Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
>
+ Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
>
+
'a + 'b +
'c {
}
39 changes: 39 additions & 0 deletions tests/target/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,42 @@ impl CombineTypes {
self.query_callbacks()(&query_id)
}
}

// #2859
pub fn do_something<'a, T: Trait1 + Trait2 + 'a>(
&fooo: u32,
) -> impl Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
> + 'a {
}

pub fn do_something<'a, T: Trait1 + Trait2 + 'a>(
&fooo: u32,
) -> impl Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
> + Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
> + Future<
Item = (
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
impl Future<Item = (), Error = SomeError> + 'a,
),
Error = SomeError,
> + 'a + 'b + 'c {
}