From 5ee6304d8d738d05debfcb712ee57b024f5e1b95 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sun, 20 Sep 2015 18:44:49 +0200 Subject: [PATCH] Fix struct overflows --- src/expr.rs | 23 +++++++++++++---------- tests/source/expr.rs | 8 -------- tests/source/struct_lits.rs | 8 ++++++++ tests/target/expr.rs | 7 ------- tests/target/struct_lits.rs | 16 ++++++++++++---- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 155553a7615..95da7d3659f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1057,7 +1057,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, let path_str = try_opt!(path.rewrite(context, path_budget, offset)); // Foo { a: Foo } - indent is +3, width is -5. - let h_budget = try_opt!(width.checked_sub(path_str.len() + 5)); + let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0); let (indent, v_budget) = match context.config.struct_lit_style { StructLitStyle::Visual => { (offset + path_str.len() + 3, h_budget) @@ -1103,16 +1103,16 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, |item| { match *item { StructLitField::Regular(ref field) => { - rewrite_field(inner_context, &field, h_budget, indent) + rewrite_field(inner_context, &field, v_budget, indent) .unwrap_or(context.snippet(field.span)) } StructLitField::Base(ref expr) => { // 2 = .. format!("..{}", - h_budget.checked_sub(2) - .and_then(|h_budget| { + v_budget.checked_sub(2) + .and_then(|v_budget| { expr.rewrite(inner_context, - h_budget, + v_budget, indent + 2) }) .unwrap_or(context.snippet(expr.span))) @@ -1122,11 +1122,13 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, span_after(span, "{", context.codemap), span.hi); + let tactic = match (context.config.struct_lit_style, fields.len()) { + (StructLitStyle::Visual, 1) => ListTactic::HorizontalVertical, + _ => context.config.struct_lit_multiline_style.to_list_tactic(), + }; + let fmt = ListFormatting { - tactic: match (context.config.struct_lit_style, fields.len()) { - (StructLitStyle::Visual, 1) => ListTactic::HorizontalVertical, - _ => context.config.struct_lit_multiline_style.to_list_tactic(), - }, + tactic: tactic, separator: ",", trailing_separator: if base.is_some() { SeparatorTactic::Never @@ -1150,7 +1152,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, }; match (context.config.struct_lit_style, context.config.struct_lit_multiline_style) { - (StructLitStyle::Block, _) if fields_str.contains('\n') => format_on_newline(), + (StructLitStyle::Block, _) if fields_str.contains('\n') || fields_str.len() > h_budget => + format_on_newline(), (StructLitStyle::Block, MultilineStyle::ForceMulti) => format_on_newline(), _ => Some(format!("{} {{ {} }}", path_str, fields_str)), } diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 4b5e262bd26..faa0d3775c7 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -173,11 +173,3 @@ fn arrays() { [ 1 + 3, 4 , 5, 6, 7, 7, fncall::>(3-1)] } - -fn struct_exprs() { - Foo - { a : 1, b:f( 2)}; - Foo{a:1,b:f(2),..g(3)}; - // FIXME: should be wrapped (#231) - LoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongStruct { ..base }; -} diff --git a/tests/source/struct_lits.rs b/tests/source/struct_lits.rs index ead7b749d5a..69f90879164 100644 --- a/tests/source/struct_lits.rs +++ b/tests/source/struct_lits.rs @@ -83,3 +83,11 @@ fn issue278() { b: 0, }; } + +fn struct_exprs() { + Foo + { a : 1, b:f( 2)}; + Foo{a:1,b:f(2),..g(3)}; + LoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongStruct { ..base }; + IntrinsicISizesContribution { content_intrinsic_sizes: IntrinsicISizes { minimum_inline_size: 0, }, }; +} diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 7b579960d53..bb629f12126 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -183,10 +183,3 @@ fn arrays() { [1 + 3, 4, 5, 6, 7, 7, fncall::>(3 - 1)] } - -fn struct_exprs() { - Foo { a: 1, b: f(2) }; - Foo { a: 1, b: f(2), ..g(3) }; - // FIXME: should be wrapped (#231) - LoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongStruct { ..base }; -} diff --git a/tests/target/struct_lits.rs b/tests/target/struct_lits.rs index b95772d41ec..ac7b8aaaeab 100644 --- a/tests/target/struct_lits.rs +++ b/tests/target/struct_lits.rs @@ -65,10 +65,7 @@ fn main() { fn matcher() { TagTerminatedByteMatcher { - matcher: ByteMatcher { - pattern: b"