From 37b6855d9d1159d9a606c6bc5fe21f0aeb20df69 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 11:26:12 +0900 Subject: [PATCH 1/3] Add a test for #2158 --- tests/source/trait.rs | 6 ++++++ tests/target/trait.rs | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/tests/source/trait.rs b/tests/source/trait.rs index c5b9d71c094e..e625c4c97036 100644 --- a/tests/source/trait.rs +++ b/tests/source/trait.rs @@ -65,3 +65,9 @@ A + C // and B + B {} + +// #2158 +trait Foo { + type ItRev = > as UntypedTimeSeries>::IterRev; + type IteRev = > as UntypedTimeSeries>::IterRev; +} diff --git a/tests/target/trait.rs b/tests/target/trait.rs index e17191ce1eff..632107d89383 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -92,3 +92,10 @@ A + C // and B + B {} + +// #2158 +trait Foo { + type ItRev = > as UntypedTimeSeries>::IterRev; + type IteRev = + > as UntypedTimeSeries>::IterRev; +} From 4cd2e6f39a469206671d93b6d2f8f581db0aba41 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 11:26:36 +0900 Subject: [PATCH 2/3] Generalize rewrite_assign_rhs() --- src/expr.rs | 8 ++++---- src/items.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 7380dc4850ec..46e7e500f227 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2578,10 +2578,10 @@ fn rewrite_assignment( // The left hand side must contain everything up to, and including, the // assignment operator. -pub fn rewrite_assign_rhs>( +pub fn rewrite_assign_rhs, R: Rewrite>( context: &RewriteContext, lhs: S, - ex: &ast::Expr, + ex: &R, shape: Shape, ) -> Option { let lhs = lhs.into(); @@ -2596,9 +2596,9 @@ pub fn rewrite_assign_rhs>( Some(lhs + &rhs) } -fn choose_rhs( +fn choose_rhs( context: &RewriteContext, - expr: &ast::Expr, + expr: &R, shape: Shape, orig_rhs: Option, ) -> Option { diff --git a/src/items.rs b/src/items.rs index fa697157ea32..e0bf02abe65a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -111,7 +111,7 @@ impl Rewrite for ast::Local { // 1 = trailing semicolon; let nested_shape = shape.sub_width(1)?; - result = rewrite_assign_rhs(context, result, ex, nested_shape)?; + result = rewrite_assign_rhs(context, result, &**ex, nested_shape)?; } result.push(';'); @@ -550,7 +550,7 @@ impl<'a> FmtVisitor<'a> { ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr { let lhs = format!("{} =", field.node.name); // 1 = ',' - rewrite_assign_rhs(&context, lhs, expr, shape.sub_width(1)?)? + rewrite_assign_rhs(&context, lhs, &**expr, shape.sub_width(1)?)? } else { field.node.name.to_string() }, @@ -1593,7 +1593,7 @@ fn rewrite_static( rewrite_assign_rhs( context, lhs, - expr, + &**expr, Shape::legacy(remaining_width, offset.block_only()), ).and_then(|res| { recover_comment_removed(res, static_parts.span, context) From f7ef1f681cf026ee6e941f51de0fa9a3693e2153 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 16 Nov 2017 11:27:11 +0900 Subject: [PATCH 3/3] Use rewrite_assign_rhs() when rewriting associated type --- src/items.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/items.rs b/src/items.rs index e0bf02abe65a..5f6c043bb4a8 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1613,10 +1613,9 @@ pub fn rewrite_associated_type( ) -> Option { let prefix = format!("type {}", ident); - let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt { + let type_bounds_str = if let Some(ref bounds) = ty_param_bounds_opt { // 2 = ": ".len() let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?; - let bounds: &[_] = ty_param_bounds; let bound_str = bounds .iter() .map(|ty_bound| ty_bound.rewrite(context, shape)) @@ -1631,14 +1630,10 @@ pub fn rewrite_associated_type( }; if let Some(ty) = ty_opt { - let ty_str = ty.rewrite( - context, - Shape::legacy( - context.budget(indent.block_indent + prefix.len() + 2), - indent.block_only(), - ), - )?; - Some(format!("{}{} = {};", prefix, type_bounds_str, ty_str)) + // 1 = `;` + let shape = Shape::indented(indent, context.config).sub_width(1)?; + let lhs = format!("{}{} =", prefix, type_bounds_str); + rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";") } else { Some(format!("{}{};", prefix, type_bounds_str)) }