From 57dba240781a686ab0d9a0327c48e72020f2c690 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 10 Jul 2016 10:38:10 +0200 Subject: [PATCH 1/9] Fix issue-1049 --- src/items.rs | 9 ++++----- tests/source/issue-1049.rs | 6 ++++++ tests/target/issue-1049.rs | 8 ++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 tests/source/issue-1049.rs create mode 100644 tests/target/issue-1049.rs diff --git a/src/items.rs b/src/items.rs index 1de56220cf4..cc2ad2b64a4 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1359,8 +1359,9 @@ fn rewrite_fn_base(context: &RewriteContext, _ => { // If we've already gone multi-line, or the return type would push // over the max width, then put the return type on a new line. - result.contains("\n") || multi_line_ret_str || - result.len() + indent.width() + ret_str_len > context.config.max_width + let overlong_ret = result.len() + indent.width() + ret_str_len > + context.config.max_width - 1; + result.contains("\n") || multi_line_ret_str || overlong_ret } }; let ret_indent = if ret_should_indent { @@ -1388,10 +1389,8 @@ fn rewrite_fn_base(context: &RewriteContext, if multi_line_ret_str { // Now that we know the proper indent and width, we need to // re-layout the return type. - let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width())); - let ret_str = try_opt!(fd.output - .rewrite(context, budget, ret_indent)); + let ret_str = try_opt!(fd.output.rewrite(context, budget, ret_indent)); result.push_str(&ret_str); } else { result.push_str(&ret_str); diff --git a/tests/source/issue-1049.rs b/tests/source/issue-1049.rs new file mode 100644 index 00000000000..2d76cc489e2 --- /dev/null +++ b/tests/source/issue-1049.rs @@ -0,0 +1,6 @@ +// Test issue-1049 +pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle, HandleType> { +} + +pub fn merge(mut X: Abcdef) -> Handle, K, V, marker::Internal>, marker::Edge> { +} diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs new file mode 100644 index 00000000000..0529e861f69 --- /dev/null +++ b/tests/target/issue-1049.rs @@ -0,0 +1,8 @@ +// Test issue-1049 +pub unsafe fn reborrow_mut(&mut X: Abcde) + -> Handle, HandleType> { +} + +pub fn merge(mut X: Abcdef) + -> Handle, K, V, marker::Internal>, marker::Edge> { +} From 0f54a28b4c510d2e41862fe1cac36697e1f3e58b Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 10 Jul 2016 23:33:11 +0200 Subject: [PATCH 2/9] Add testcase suggested by pepyakin --- tests/source/issue-1049.rs | 5 +++++ tests/target/issue-1049.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/source/issue-1049.rs b/tests/source/issue-1049.rs index 2d76cc489e2..6be04609e4f 100644 --- a/tests/source/issue-1049.rs +++ b/tests/source/issue-1049.rs @@ -4,3 +4,8 @@ pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle Handle, K, V, marker::Internal>, marker::Edge> { } + +impl Handle { + pub fn merge(a: Abcd) -> Handle, K, V, marker::Internal>, marker::Edge> { + } +} diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs index 0529e861f69..e2cd827888b 100644 --- a/tests/target/issue-1049.rs +++ b/tests/target/issue-1049.rs @@ -6,3 +6,8 @@ pub unsafe fn reborrow_mut(&mut X: Abcde) pub fn merge(mut X: Abcdef) -> Handle, K, V, marker::Internal>, marker::Edge> { } + +impl Handle { + pub fn merge(a: Abcd) -> Handle, K, V, marker::Internal>, marker::Edge> { + } +} From d62d43cf8458a155cb18370410df058bc80ced37 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 10 Jul 2016 23:44:46 +0200 Subject: [PATCH 3/9] Fix last commit --- tests/target/issue-1049.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs index e2cd827888b..96b576b3b29 100644 --- a/tests/target/issue-1049.rs +++ b/tests/target/issue-1049.rs @@ -8,6 +8,7 @@ pub fn merge(mut X: Abcdef) } impl Handle { - pub fn merge(a: Abcd) -> Handle, K, V, marker::Internal>, marker::Edge> { + pub fn merge(a: Abcd) + -> Handle, K, V, marker::Internal>, marker::Edge> { } } From 1e18a4be169dd462745ebffd504602425de9754c Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Jul 2016 22:34:50 +0200 Subject: [PATCH 4/9] Handle special case --- src/items.rs | 21 ++++++++++++++++----- tests/source/issue-1049.rs | 2 +- tests/target/issue-1049.rs | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/items.rs b/src/items.rs index cc2ad2b64a4..2e9d3a9bf3b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1278,6 +1278,7 @@ fn rewrite_fn_base(context: &RewriteContext, let multi_line_ret_str = ret_str.contains('\n'); let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() }; + println!("ret: \"{}\"", ret_str); // Args. let (mut one_line_budget, mut multi_line_budget, mut arg_indent) = @@ -1357,11 +1358,21 @@ fn rewrite_fn_base(context: &RewriteContext, FnArgLayoutStyle::Block if put_args_in_block => false, FnArgLayoutStyle::BlockAlways => false, _ => { - // If we've already gone multi-line, or the return type would push - // over the max width, then put the return type on a new line. - let overlong_ret = result.len() + indent.width() + ret_str_len > - context.config.max_width - 1; - result.contains("\n") || multi_line_ret_str || overlong_ret + // If we've already gone multi-line, or the return type would push over the max + // width, then put the return type on a new line. The +1 for the signature length + // is to consider an additional space between the closing brace of the argument and + // the arrow '->'. + let mut sig_length = result.len() + indent.width() + ret_str_len + 1; + + // If there is no where clause, take into account the space after the return type + // and the brace. + if where_clause.predicates.is_empty() { + sig_length += 2; + } + + let overlong_sig = sig_length > context.config.max_width; + + result.contains("\n") || multi_line_ret_str || overlong_sig } }; let ret_indent = if ret_should_indent { diff --git a/tests/source/issue-1049.rs b/tests/source/issue-1049.rs index 6be04609e4f..d37d52071d7 100644 --- a/tests/source/issue-1049.rs +++ b/tests/source/issue-1049.rs @@ -1,4 +1,4 @@ -// Test issue-1049 +// Test overlong function signature pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle, HandleType> { } diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs index 96b576b3b29..4010112c489 100644 --- a/tests/target/issue-1049.rs +++ b/tests/target/issue-1049.rs @@ -1,4 +1,4 @@ -// Test issue-1049 +// Test overlong function signature pub unsafe fn reborrow_mut(&mut X: Abcde) -> Handle, HandleType> { } From b6550c4e860c86fd11646a4929005e8c0054031f Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Jul 2016 22:36:35 +0200 Subject: [PATCH 5/9] Remove debugging println --- src/items.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 2e9d3a9bf3b..67aa9ae483b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1278,7 +1278,6 @@ fn rewrite_fn_base(context: &RewriteContext, let multi_line_ret_str = ret_str.contains('\n'); let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() }; - println!("ret: \"{}\"", ret_str); // Args. let (mut one_line_budget, mut multi_line_budget, mut arg_indent) = From b702137e72f3e1b03fdf62b4ef81dfff4cb88dcc Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Jul 2016 22:48:53 +0200 Subject: [PATCH 6/9] Fix grammar in comment --- src/items.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/items.rs b/src/items.rs index 67aa9ae483b..2777b76b1cd 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1358,9 +1358,9 @@ fn rewrite_fn_base(context: &RewriteContext, FnArgLayoutStyle::BlockAlways => false, _ => { // If we've already gone multi-line, or the return type would push over the max - // width, then put the return type on a new line. The +1 for the signature length - // is to consider an additional space between the closing brace of the argument and - // the arrow '->'. + // width, then put the return type on a new line. With the +1 for the signature + // length an additional space between the closing brace of the argument and the + // arrow '->' is considered. let mut sig_length = result.len() + indent.width() + ret_str_len + 1; // If there is no where clause, take into account the space after the return type From accd0c402be78a1ff11705677690158bdcdcedfb Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 31 Jul 2016 11:19:56 +0200 Subject: [PATCH 7/9] Change word in comment --- src/items.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/items.rs b/src/items.rs index 2777b76b1cd..dcb0888fed4 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1359,8 +1359,8 @@ fn rewrite_fn_base(context: &RewriteContext, _ => { // If we've already gone multi-line, or the return type would push over the max // width, then put the return type on a new line. With the +1 for the signature - // length an additional space between the closing brace of the argument and the - // arrow '->' is considered. + // length an additional space between the closing parenthesis of the argument and + // the arrow '->' is considered. let mut sig_length = result.len() + indent.width() + ret_str_len + 1; // If there is no where clause, take into account the space after the return type From 16af34a2c59160fea665506797e0c9965e5a5758 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 31 Jul 2016 11:28:48 +0200 Subject: [PATCH 8/9] Add test for long func without ret type --- tests/source/issue-1049.rs | 3 +++ tests/target/issue-1049.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tests/source/issue-1049.rs b/tests/source/issue-1049.rs index d37d52071d7..2314f1c5c6f 100644 --- a/tests/source/issue-1049.rs +++ b/tests/source/issue-1049.rs @@ -9,3 +9,6 @@ impl Handle { pub fn merge(a: Abcd) -> Handle, K, V, marker::Internal>, marker::Edge> { } } + +// Long function without return type that should not be reformated. +fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {} diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs index 4010112c489..f2c04ce21ae 100644 --- a/tests/target/issue-1049.rs +++ b/tests/target/issue-1049.rs @@ -12,3 +12,6 @@ impl Handle { -> Handle, K, V, marker::Internal>, marker::Edge> { } } + +// Long function without return type that should not be reformated. +fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {} From adc37590043a01f4a228bbfcd6e6c21ff5919407 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 31 Jul 2016 13:01:15 +0200 Subject: [PATCH 9/9] Add one more test --- tests/source/issue-1049.rs | 2 ++ tests/target/issue-1049.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tests/source/issue-1049.rs b/tests/source/issue-1049.rs index 2314f1c5c6f..a0240b83b63 100644 --- a/tests/source/issue-1049.rs +++ b/tests/source/issue-1049.rs @@ -12,3 +12,5 @@ impl Handle { // Long function without return type that should not be reformated. fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {} + +fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {} diff --git a/tests/target/issue-1049.rs b/tests/target/issue-1049.rs index f2c04ce21ae..fac49b80b51 100644 --- a/tests/target/issue-1049.rs +++ b/tests/target/issue-1049.rs @@ -15,3 +15,7 @@ impl Handle { // Long function without return type that should not be reformated. fn veeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) {} + +fn veeeeeeeeeeeeeeeeeeeeeery_long_name(a: FirstTypeeeeeeeeee, + b: SecondTypeeeeeeeeeeeeeeeeeeeeeee) { +}