From b748fe8bec300afb4eb6b7923c74152c588a5eb8 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 26 May 2017 16:34:58 +0900 Subject: [PATCH 1/3] Set inside_macro to false when rewriting args of macro --- src/expr.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 318ec8e59b5..808cd06c2d2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1678,12 +1678,14 @@ fn rewrite_call_args(context: &RewriteContext, one_line_width: usize, force_trailing_comma: bool) -> Option<(bool, String)> { + let mut item_context = context.clone(); + item_context.inside_macro = false; let items = itemize_list(context.codemap, args.iter(), ")", |item| item.span.lo, |item| item.span.hi, - |item| item.rewrite(context, shape), + |item| item.rewrite(&item_context, shape), span.lo, span.hi); let mut item_vec: Vec<_> = items.collect(); @@ -1691,7 +1693,7 @@ fn rewrite_call_args(context: &RewriteContext, // Try letting the last argument overflow to the next line with block // indentation. If its first line fits on one line with the other arguments, // we format the function arguments horizontally. - let overflow_last = can_be_overflowed(context, args); + let overflow_last = can_be_overflowed(&item_context, args); let mut orig_last = None; let mut placeholder = None; @@ -1709,7 +1711,7 @@ fn rewrite_call_args(context: &RewriteContext, } else { shape.block() }; - let rewrite = args.last().unwrap().rewrite(context, arg_shape); + let rewrite = args.last().unwrap().rewrite(&item_context, arg_shape); swap(&mut item_vec[args.len() - 1].item, &mut orig_last); if let Some(rewrite) = rewrite { From 15b988aed3d2d4ec761452840ed96b84080c2abc Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 26 May 2017 16:35:34 +0900 Subject: [PATCH 2/3] Allow macro to nested and overflowed like function call --- src/expr.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 808cd06c2d2..0abeafaefc2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1772,13 +1772,17 @@ fn rewrite_call_args(context: &RewriteContext, acc + item.item.as_ref().map_or(0, |s| 2 + first_line_width(s)) }) <= one_line_budget + 2; - match write_list(&item_vec, &fmt) { + let result = write_list(&item_vec, &fmt); + let last_char_is_not_comma = result + .as_ref() + .map_or(false, |r| r.chars().last().unwrap_or(' ') != ','); + match result { // If arguments do not fit in a single line and do not contain newline, // try to put it on the next line. Try this only when we are in block mode // and not rewriting macro. Some(ref s) if context.config.fn_call_style() == IndentStyle::Block && !context.inside_macro && - ((!can_be_overflowed(context, args) && args.len() == 1 && + ((!can_be_overflowed(&context, args) && last_char_is_not_comma && s.contains('\n')) || first_line_width(s) > one_line_budget) => { fmt.trailing_separator = SeparatorTactic::Vertical; @@ -1786,13 +1790,7 @@ fn rewrite_call_args(context: &RewriteContext, write_list(&item_vec, &fmt).map(|rw| (false, rw)) } rewrite @ _ => { - rewrite.map(|rw| { - (extendable && - rw.chars() - .last() - .map_or(true, |c| force_trailing_comma || c != ','), - rw) - }) + rewrite.map(|rw| (extendable && (last_char_is_not_comma || force_trailing_comma), rw)) } } } @@ -1809,6 +1807,7 @@ fn can_be_overflowed(context: &RewriteContext, args: &[ptr::P]) -> bo context.config.fn_call_style() == IndentStyle::Visual && args.len() > 1 } Some(&ast::ExprKind::Call(..)) | + Some(&ast::ExprKind::Mac(..)) | Some(&ast::ExprKind::Struct(..)) => { context.config.fn_call_style() == IndentStyle::Block && args.len() == 1 } @@ -1824,6 +1823,7 @@ fn is_extendable(args: &[ptr::P]) -> bool { ast::ExprKind::Call(..) | ast::ExprKind::Closure(..) | ast::ExprKind::Match(..) | + ast::ExprKind::Mac(..) | ast::ExprKind::Struct(..) | ast::ExprKind::Tup(..) => true, _ => false, From 554605d47cc2546f32871de5f41ef4008f5d5ecf Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 26 May 2017 16:36:15 +0900 Subject: [PATCH 3/3] Format source codes and add tests --- src/chains.rs | 7 +++---- src/expr.rs | 2 +- src/items.rs | 4 +--- tests/source/configs-fn_call_style-block.rs | 4 ++++ tests/target/closure-block-inside-macro.rs | 14 ++++++-------- tests/target/configs-fn_call_style-block.rs | 10 ++++++++++ 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 9a8a6914440..1c66306022d 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -168,10 +168,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - .into_iter() .chain(::std::iter::repeat(other_child_shape).take(subexpr_list.len() - 1)); let iter = subexpr_list.iter().rev().zip(child_shape_iter); - let mut rewrites = try_opt!(iter.map(|(e, shape)| { - rewrite_chain_subexpr(e, total_span, context, shape) - }) - .collect::>>()); + let mut rewrites = + try_opt!(iter.map(|(e, shape)| rewrite_chain_subexpr(e, total_span, context, shape)) + .collect::>>()); // Total of all items excluding the last. let last_non_try_index = rewrites.len() - (1 + trailing_try_num); diff --git a/src/expr.rs b/src/expr.rs index 0abeafaefc2..21a872489d8 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1782,7 +1782,7 @@ fn rewrite_call_args(context: &RewriteContext, // and not rewriting macro. Some(ref s) if context.config.fn_call_style() == IndentStyle::Block && !context.inside_macro && - ((!can_be_overflowed(&context, args) && last_char_is_not_comma && + ((!can_be_overflowed(context, args) && last_char_is_not_comma && s.contains('\n')) || first_line_width(s) > one_line_budget) => { fmt.trailing_separator = SeparatorTactic::Vertical; diff --git a/src/items.rs b/src/items.rs index 34d2b6ea530..2d9499445cc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1783,9 +1783,7 @@ fn rewrite_args(context: &RewriteContext, -> Option { let mut arg_item_strs = try_opt!(args.iter() - .map(|arg| { - arg.rewrite(&context, Shape::legacy(multi_line_budget, arg_indent)) - }) + .map(|arg| arg.rewrite(&context, Shape::legacy(multi_line_budget, arg_indent))) .collect::>>()); // Account for sugary self. diff --git a/tests/source/configs-fn_call_style-block.rs b/tests/source/configs-fn_call_style-block.rs index aae6693f43f..2068216d18e 100644 --- a/tests/source/configs-fn_call_style-block.rs +++ b/tests/source/configs-fn_call_style-block.rs @@ -16,6 +16,10 @@ fn main() { .client_credentials(&self.config.auth.oauth2.id, &self.config.auth.oauth2.secret)?; } } + + // nesting macro and function call + try!(foo(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)); + try!(foo(try!(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx))); } // #1521 diff --git a/tests/target/closure-block-inside-macro.rs b/tests/target/closure-block-inside-macro.rs index 5d1e1de0f68..d567008c2ad 100644 --- a/tests/target/closure-block-inside-macro.rs +++ b/tests/target/closure-block-inside-macro.rs @@ -1,13 +1,11 @@ // rustfmt-fn_call_style: Block // #1547 -fuzz_target!(|data: &[u8]| { - if let Some(first) = data.first() { - let index = *first as usize; - if index >= ENCODINGS.len() { - return; - } - let encoding = ENCODINGS[index]; - dispatch_test(encoding, &data[1..]); +fuzz_target!(|data: &[u8]| if let Some(first) = data.first() { + let index = *first as usize; + if index >= ENCODINGS.len() { + return; } + let encoding = ENCODINGS[index]; + dispatch_test(encoding, &data[1..]); }); diff --git a/tests/target/configs-fn_call_style-block.rs b/tests/target/configs-fn_call_style-block.rs index 2ac5340059c..3da4f648527 100644 --- a/tests/target/configs-fn_call_style-block.rs +++ b/tests/target/configs-fn_call_style-block.rs @@ -32,6 +32,16 @@ fn main() { )?; } } + + // nesting macro and function call + try!(foo( + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + )); + try!(foo(try!( + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ))); } // #1521