Skip to content

Commit d4fdaec

Browse files
authored
Merge pull request #2159 from topecongiro/issue-2158
Prevent long associated type from overflowing max width
2 parents c0e537d + f7ef1f6 commit d4fdaec

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/expr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2578,10 +2578,10 @@ fn rewrite_assignment(
25782578

25792579
// The left hand side must contain everything up to, and including, the
25802580
// assignment operator.
2581-
pub fn rewrite_assign_rhs<S: Into<String>>(
2581+
pub fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
25822582
context: &RewriteContext,
25832583
lhs: S,
2584-
ex: &ast::Expr,
2584+
ex: &R,
25852585
shape: Shape,
25862586
) -> Option<String> {
25872587
let lhs = lhs.into();
@@ -2596,9 +2596,9 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
25962596
Some(lhs + &rhs)
25972597
}
25982598

2599-
fn choose_rhs(
2599+
fn choose_rhs<R: Rewrite>(
26002600
context: &RewriteContext,
2601-
expr: &ast::Expr,
2601+
expr: &R,
26022602
shape: Shape,
26032603
orig_rhs: Option<String>,
26042604
) -> Option<String> {

src/items.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Rewrite for ast::Local {
111111
// 1 = trailing semicolon;
112112
let nested_shape = shape.sub_width(1)?;
113113

114-
result = rewrite_assign_rhs(context, result, ex, nested_shape)?;
114+
result = rewrite_assign_rhs(context, result, &**ex, nested_shape)?;
115115
}
116116

117117
result.push(';');
@@ -550,7 +550,7 @@ impl<'a> FmtVisitor<'a> {
550550
ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr {
551551
let lhs = format!("{} =", field.node.name);
552552
// 1 = ','
553-
rewrite_assign_rhs(&context, lhs, expr, shape.sub_width(1)?)?
553+
rewrite_assign_rhs(&context, lhs, &**expr, shape.sub_width(1)?)?
554554
} else {
555555
field.node.name.to_string()
556556
},
@@ -1593,7 +1593,7 @@ fn rewrite_static(
15931593
rewrite_assign_rhs(
15941594
context,
15951595
lhs,
1596-
expr,
1596+
&**expr,
15971597
Shape::legacy(remaining_width, offset.block_only()),
15981598
).and_then(|res| {
15991599
recover_comment_removed(res, static_parts.span, context)
@@ -1613,10 +1613,9 @@ pub fn rewrite_associated_type(
16131613
) -> Option<String> {
16141614
let prefix = format!("type {}", ident);
16151615

1616-
let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
1616+
let type_bounds_str = if let Some(ref bounds) = ty_param_bounds_opt {
16171617
// 2 = ": ".len()
16181618
let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?;
1619-
let bounds: &[_] = ty_param_bounds;
16201619
let bound_str = bounds
16211620
.iter()
16221621
.map(|ty_bound| ty_bound.rewrite(context, shape))
@@ -1631,14 +1630,10 @@ pub fn rewrite_associated_type(
16311630
};
16321631

16331632
if let Some(ty) = ty_opt {
1634-
let ty_str = ty.rewrite(
1635-
context,
1636-
Shape::legacy(
1637-
context.budget(indent.block_indent + prefix.len() + 2),
1638-
indent.block_only(),
1639-
),
1640-
)?;
1641-
Some(format!("{}{} = {};", prefix, type_bounds_str, ty_str))
1633+
// 1 = `;`
1634+
let shape = Shape::indented(indent, context.config).sub_width(1)?;
1635+
let lhs = format!("{}{} =", prefix, type_bounds_str);
1636+
rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
16421637
} else {
16431638
Some(format!("{}{};", prefix, type_bounds_str))
16441639
}

tests/source/trait.rs

+6
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ A + C
6565
// and B
6666
+ B
6767
{}
68+
69+
// #2158
70+
trait Foo {
71+
type ItRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
72+
type IteRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
73+
}

tests/target/trait.rs

+7
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ A + C
9292
// and B
9393
+ B
9494
{}
95+
96+
// #2158
97+
trait Foo {
98+
type ItRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
99+
type IteRev =
100+
<MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
101+
}

0 commit comments

Comments
 (0)