Skip to content

Commit 4f1c81b

Browse files
dtolnaycalebcartwright
authored andcommitted
Preserve polarity on negative non-trait impl
1 parent fd9e7d3 commit 4f1c81b

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/formatting/items.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ fn format_impl_ref_and_type(
964964
ast::ImplPolarity::Positive => "",
965965
};
966966

967+
let polarity_overhead;
968+
let trait_ref_overhead;
967969
if let Some(ref trait_ref) = *trait_ref {
968970
let result_len = last_line_width(&result);
969971
result.push_str(&rewrite_trait_ref(
@@ -973,11 +975,14 @@ fn format_impl_ref_and_type(
973975
polarity_str,
974976
result_len,
975977
)?);
978+
polarity_overhead = 0; // already written
979+
trait_ref_overhead = " for".len();
980+
} else {
981+
polarity_overhead = polarity_str.len();
982+
trait_ref_overhead = 0;
976983
}
977984

978985
// Try to put the self type in a single line.
979-
// ` for`
980-
let trait_ref_overhead = if trait_ref.is_some() { 4 } else { 0 };
981986
let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
982987
// If there is no where-clause adapt budget for type formatting to take space and curly
983988
// brace into account.
@@ -988,7 +993,10 @@ fn format_impl_ref_and_type(
988993
} else {
989994
0
990995
};
991-
let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
996+
let used_space = last_line_width(&result)
997+
+ polarity_overhead
998+
+ trait_ref_overhead
999+
+ curly_brace_overhead;
9921000
// 1 = space before the type.
9931001
let budget = context.budget(used_space + 1);
9941002
if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
@@ -997,6 +1005,7 @@ fn format_impl_ref_and_type(
9971005
result.push_str(" for ");
9981006
} else {
9991007
result.push(' ');
1008+
result.push_str(polarity_str);
10001009
}
10011010
result.push_str(&self_ty_str);
10021011
return Some(result);
@@ -1010,8 +1019,10 @@ fn format_impl_ref_and_type(
10101019
result.push_str(&new_line_offset.to_string(context.config));
10111020
if trait_ref.is_some() {
10121021
result.push_str("for ");
1022+
} else {
1023+
result.push_str(polarity_str);
10131024
}
1014-
let budget = context.budget(last_line_width(&result));
1025+
let budget = context.budget(last_line_width(&result) + polarity_overhead);
10151026
let type_offset = match context.config.indent_style() {
10161027
IndentStyle::Visual => new_line_offset + trait_ref_overhead,
10171028
IndentStyle::Block => new_line_offset,

0 commit comments

Comments
 (0)