From 476992a15d4e01071ac57cec7c8e54b36e606d46 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 14 Oct 2018 21:43:35 +0900 Subject: [PATCH 1/3] Add a test for #3092 --- tests/source/trait.rs | 9 +++++++++ tests/target/trait.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/source/trait.rs b/tests/source/trait.rs index 2fa25d0802cf..d5ee4f044173 100644 --- a/tests/source/trait.rs +++ b/tests/source/trait.rs @@ -106,3 +106,12 @@ trait Foo<'a> { impl<'a> Foo<'a> for i32 { type Bar< 'a > = i32; } + +// #3092 +pub mod test { + pub trait ATraitWithALooongName {} + pub trait ATrait + :ATraitWithALooongName + ATraitWithALooongName + ATraitWithALooongName + ATraitWithALooongName +{ +} +} diff --git a/tests/target/trait.rs b/tests/target/trait.rs index 2eb2c12aa305..18747c47fef3 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -144,3 +144,15 @@ trait Foo<'a> { impl<'a> Foo<'a> for i32 { type Bar<'a> = i32; } + +// #3092 +pub mod test { + pub trait ATraitWithALooongName {} + pub trait ATrait: + ATraitWithALooongName + + ATraitWithALooongName + + ATraitWithALooongName + + ATraitWithALooongName + { + } +} From fdfb489c48a1996076cede1bc1a417134fef57f2 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 14 Oct 2018 21:43:59 +0900 Subject: [PATCH 2/3] Add a correct indent before trait's closing brace with empty body --- src/items.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/items.rs b/src/items.rs index f74458fdaa65..1673bffbce9b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1121,6 +1121,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let snippet = context.snippet(item.span); let open_pos = snippet.find_uncommented("{")? + 1; + let outer_indent_str = offset.block_only().to_string_with_newline(context.config); if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) { let mut visitor = FmtVisitor::from_context(context); @@ -1134,13 +1135,12 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) visitor.format_missing(item.span.hi() - BytePos(1)); let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config); - let outer_indent_str = offset.block_only().to_string_with_newline(context.config); result.push_str(&inner_indent_str); result.push_str(visitor.buffer.to_string().trim()); result.push_str(&outer_indent_str); } else if result.contains('\n') { - result.push('\n'); + result.push_str(&outer_indent_str); } result.push('}'); From 5f5d04283ce68b514a13ae5c41ba3961549ab46f Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 14 Oct 2018 21:44:56 +0900 Subject: [PATCH 3/3] Use correct width When rewriting trait bounds on the next line, we do not want to add an extra indentation. However, the max width should be smaller. --- src/expr.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/expr.rs b/src/expr.rs index 7a6a14730601..8a5b3de7a147 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1937,7 +1937,9 @@ fn shape_from_rhs_tactic( rhs_tactic: RhsTactics, ) -> Option { match rhs_tactic { - RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)), + RhsTactics::ForceNextLineWithoutIndent => shape + .with_max_width(context.config) + .sub_width(shape.indent.width()), RhsTactics::Default => { Shape::indented(shape.indent.block_indent(context.config), context.config) .sub_width(shape.rhs_overhead(context.config))