Skip to content

Commit 96ee060

Browse files
dtolnaycalebcartwright
authored andcommitted
Preserve polarity on negative non-trait impl
1 parent 29f33eb commit 96ee060

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/items.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ fn format_impl_ref_and_type(
857857
ast::ImplPolarity::Positive => "",
858858
};
859859

860+
let polarity_overhead;
861+
let trait_ref_overhead;
860862
if let Some(ref trait_ref) = *trait_ref {
861863
let result_len = last_line_width(&result);
862864
result.push_str(&rewrite_trait_ref(
@@ -866,11 +868,14 @@ fn format_impl_ref_and_type(
866868
polarity_str,
867869
result_len,
868870
)?);
871+
polarity_overhead = 0; // already written
872+
trait_ref_overhead = " for".len();
873+
} else {
874+
polarity_overhead = polarity_str.len();
875+
trait_ref_overhead = 0;
869876
}
870877

871878
// Try to put the self type in a single line.
872-
// ` for`
873-
let trait_ref_overhead = if trait_ref.is_some() { 4 } else { 0 };
874879
let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
875880
// If there is no where-clause adapt budget for type formatting to take space and curly
876881
// brace into account.
@@ -881,7 +886,10 @@ fn format_impl_ref_and_type(
881886
} else {
882887
0
883888
};
884-
let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
889+
let used_space = last_line_width(&result)
890+
+ polarity_overhead
891+
+ trait_ref_overhead
892+
+ curly_brace_overhead;
885893
// 1 = space before the type.
886894
let budget = context.budget(used_space + 1);
887895
if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
@@ -890,6 +898,7 @@ fn format_impl_ref_and_type(
890898
result.push_str(" for ");
891899
} else {
892900
result.push(' ');
901+
result.push_str(polarity_str);
893902
}
894903
result.push_str(&self_ty_str);
895904
return Some(result);
@@ -903,8 +912,10 @@ fn format_impl_ref_and_type(
903912
result.push_str(&new_line_offset.to_string(context.config));
904913
if trait_ref.is_some() {
905914
result.push_str("for ");
915+
} else {
916+
result.push_str(polarity_str);
906917
}
907-
let budget = context.budget(last_line_width(&result));
918+
let budget = context.budget(last_line_width(&result) + polarity_overhead);
908919
let type_offset = match context.config.indent_style() {
909920
IndentStyle::Visual => new_line_offset + trait_ref_overhead,
910921
IndentStyle::Block => new_line_offset,

0 commit comments

Comments
 (0)