]> git.lizzy.rs Git - rust.git/commitdiff
Prevent rewriting closure block to expr inside macro
authorSeiichi Uchida <topecongiro@localhost.localdomain>
Sat, 13 May 2017 09:20:18 +0000 (18:20 +0900)
committerSeiichi Uchida <topecongiro@localhost.localdomain>
Sat, 13 May 2017 09:32:25 +0000 (18:32 +0900)
src/chains.rs
src/expr.rs
src/macros.rs
src/rewrite.rs
src/visitor.rs
tests/target/closure-block-inside-macro.rs [new file with mode: 0644]

index 3d148d19a55fd003f63d8f157a23deb7aef78d53..858103489fd7e4cae14edbbda1a1becb1173a863 100644 (file)
@@ -440,5 +440,5 @@ fn rewrite_method_call(method_name: ast::Ident,
     let callee_str = format!(".{}{}", method_name, type_str);
     let span = mk_sp(lo, span.hi);
 
-    rewrite_call(context, &callee_str, &args[1..], span, shape, false)
+    rewrite_call(context, &callee_str, &args[1..], span, shape)
 }
index 0a2fab9cd27943f4b18c970e8d0d22b708db18bd..a52e163ac7abd3b9cd23d300d33c6ecaddf1a7f3 100644 (file)
@@ -70,7 +70,7 @@ fn format_expr(expr: &ast::Expr,
         }
         ast::ExprKind::Call(ref callee, ref args) => {
             let inner_span = mk_sp(callee.span.hi, expr.span.hi);
-            rewrite_call(context, &**callee, args, inner_span, shape, false)
+            rewrite_call(context, &**callee, args, inner_span, shape)
         }
         ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape),
         ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
@@ -512,7 +512,8 @@ fn rewrite_closure(capture: ast::CaptureBy,
         }
 
         // Figure out if the block is necessary.
-        let needs_block = block.rules != ast::BlockCheckMode::Default || block.stmts.len() > 1 ||
+        let needs_block = block.rules != ast::BlockCheckMode::Default ||
+                          block.stmts.len() > 1 || context.inside_macro ||
                           block_contains_comment(block, context.codemap) ||
                           prefix.contains('\n');
 
@@ -1599,20 +1600,12 @@ pub fn rewrite_call<R>(context: &RewriteContext,
                        callee: &R,
                        args: &[ptr::P<ast::Expr>],
                        span: Span,
-                       shape: Shape,
-                       force_no_trailing_comma: bool)
+                       shape: Shape)
                        -> Option<String>
     where R: Rewrite
 {
-    let closure = |callee_max_width| {
-        rewrite_call_inner(context,
-                           callee,
-                           callee_max_width,
-                           args,
-                           span,
-                           shape,
-                           force_no_trailing_comma)
-    };
+    let closure =
+        |callee_max_width| rewrite_call_inner(context, callee, callee_max_width, args, span, shape);
 
     // 2 is for parens
     let max_width = try_opt!(shape.width.checked_sub(2));
@@ -1624,8 +1617,7 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
                          max_callee_width: usize,
                          args: &[ptr::P<ast::Expr>],
                          span: Span,
-                         shape: Shape,
-                         force_no_trailing_comma: bool)
+                         shape: Shape)
                          -> Result<String, Ordering>
     where R: Rewrite
 {
@@ -1665,13 +1657,8 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
     let span_lo = context.codemap.span_after(span, "(");
     let span = mk_sp(span_lo, span.hi);
 
-    let list_str = rewrite_call_args(context,
-                                     args,
-                                     span,
-                                     nested_shape,
-                                     one_line_width,
-                                     force_no_trailing_comma)
-            .ok_or(Ordering::Less)?;
+    let list_str = rewrite_call_args(context, args, span, nested_shape, one_line_width)
+        .ok_or(Ordering::Less)?;
 
     let result = if context.config.fn_call_style == IndentStyle::Visual ||
                     (!list_str.contains('\n') && list_str.chars().last().unwrap_or(' ') != ',') {
@@ -1695,8 +1682,7 @@ fn rewrite_call_args(context: &RewriteContext,
                      args: &[ptr::P<ast::Expr>],
                      span: Span,
                      shape: Shape,
-                     one_line_width: usize,
-                     force_no_trailing_comma: bool)
+                     one_line_width: usize)
                      -> Option<String> {
     let arg_count = args.len();
 
@@ -1766,7 +1752,7 @@ fn rewrite_call_args(context: &RewriteContext,
     let mut fmt = ListFormatting {
         tactic: tactic,
         separator: ",",
-        trailing_separator: if force_no_trailing_comma ||
+        trailing_separator: if context.inside_macro ||
                                context.config.fn_call_style == IndentStyle::Visual ||
                                arg_count <= 1 {
             SeparatorTactic::Never
@@ -1783,7 +1769,7 @@ fn rewrite_call_args(context: &RewriteContext,
         // 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 &&
-                       !force_no_trailing_comma &&
+                       !context.inside_macro &&
                        (!s.contains('\n') &&
                         (s.len() > one_line_width || s.len() > context.config.fn_call_width)) => {
             fmt.trailing_separator = SeparatorTactic::Vertical;
index cd3e638d97cfd2a42c4dea2b6ee624b57eba66d2..d644e32bb6d595fe8b9a1b8089bb8bbe230394c6 100644 (file)
@@ -65,6 +65,8 @@ pub fn rewrite_macro(mac: &ast::Mac,
                      shape: Shape,
                      position: MacroPosition)
                      -> Option<String> {
+    let mut context = &mut context.clone();
+    context.inside_macro = true;
     if context.config.use_try_shorthand {
         if let Some(expr) = convert_try_mac(mac, context) {
             return expr.rewrite(context, shape);
@@ -146,11 +148,12 @@ pub fn rewrite_macro(mac: &ast::Mac,
         MacroStyle::Parens => {
             // Format macro invocation as function call, forcing no trailing
             // comma because not all macros support them.
-            rewrite_call(context, &macro_name, &expr_vec, mac.span, shape, true)
-                .map(|rw| match position {
-                         MacroPosition::Item => format!("{};", rw),
-                         _ => rw,
-                     })
+            rewrite_call(context, &macro_name, &expr_vec, mac.span, shape).map(|rw| {
+                match position {
+                    MacroPosition::Item => format!("{};", rw),
+                    _ => rw,
+                }
+            })
         }
         MacroStyle::Brackets => {
             // Format macro invocation as array literal.
index bb75a6f4db7992a0faa13ded075dadb712cb4af9..c1047e41b33c2332f0571f4503aa55b625bbff89 100644 (file)
@@ -26,6 +26,7 @@ pub struct RewriteContext<'a> {
     pub parse_session: &'a ParseSess,
     pub codemap: &'a CodeMap,
     pub config: &'a Config,
+    pub inside_macro: bool,
 }
 
 impl<'a> RewriteContext<'a> {
index 86dd5c239cc72edc9805c650da19593cbd2e3017..e813f3a181304245dad226a275170e22492bd5e4 100644 (file)
@@ -574,6 +574,7 @@ pub fn get_context(&self) -> RewriteContext {
             parse_session: self.parse_session,
             codemap: self.codemap,
             config: self.config,
+            inside_macro: false,
         }
     }
 }
diff --git a/tests/target/closure-block-inside-macro.rs b/tests/target/closure-block-inside-macro.rs
new file mode 100644 (file)
index 0000000..b58527e
--- /dev/null
@@ -0,0 +1,15 @@
+// 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..]);
+        }
+    }
+);