]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #1588 from topecongiro/nesting-macro
authorNick Cameron <nrc@ncameron.org>
Sat, 27 May 2017 01:28:47 +0000 (13:28 +1200)
committerGitHub <noreply@github.com>
Sat, 27 May 2017 01:28:47 +0000 (13:28 +1200)
Allow macro to be nested like function call

src/items.rs
src/visitor.rs
tests/target/associated_type_defaults.rs [new file with mode: 0644]
tests/target/issue-1587.rs [new file with mode: 0644]

index 2d9499445ccbf05c796ed0995e76e00df45fa714..460694b7b097ee5e24c3b67ed09f7e100730a7b7 100644 (file)
@@ -1634,6 +1634,7 @@ fn rewrite_fn_base(context: &RewriteContext,
         _ => false,
     } && !fd.inputs.is_empty();
 
+    let mut args_last_line_contains_comment = false;
     if put_args_in_block {
         arg_indent = indent.block_indent(context.config);
         result.push('\n');
@@ -1647,6 +1648,16 @@ fn rewrite_fn_base(context: &RewriteContext,
         if context.config.spaces_within_parens() && fd.inputs.len() > 0 {
             result.push(' ')
         }
+        // If the last line of args contains comment, we cannot put the closing paren
+        // on the same line.
+        if arg_str
+               .lines()
+               .last()
+               .map_or(false, |last_line| last_line.contains("//")) {
+            args_last_line_contains_comment = true;
+            result.push('\n');
+            result.push_str(&arg_indent.to_string(context.config));
+        }
         result.push(')');
     }
 
@@ -1670,7 +1681,8 @@ fn rewrite_fn_base(context: &RewriteContext,
 
                 let overlong_sig = sig_length > context.config.max_width();
 
-                result.contains('\n') || multi_line_ret_str || overlong_sig
+                (!args_last_line_contains_comment) &&
+                (result.contains('\n') || multi_line_ret_str || overlong_sig)
             }
         };
         let ret_indent = if ret_should_indent {
index 96327ef96a75db02ad0563218d7da00af7a064eb..650bb40ca1769e13af419a2cd4c27378870a6a3b 100644 (file)
@@ -389,9 +389,9 @@ pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
                               ti.id,
                               ast::Defaultness::Final);
             }
-            ast::TraitItemKind::Type(ref type_param_bounds, _) => {
+            ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
                 let rewrite = rewrite_associated_type(ti.ident,
-                                                      None,
+                                                      type_default.as_ref(),
                                                       Some(type_param_bounds),
                                                       &self.get_context(),
                                                       self.block_indent);
diff --git a/tests/target/associated_type_defaults.rs b/tests/target/associated_type_defaults.rs
new file mode 100644 (file)
index 0000000..d0a0813
--- /dev/null
@@ -0,0 +1,4 @@
+#![feature(associated_type_defaults)]
+trait Foo {
+    type Bar = ();
+}
diff --git a/tests/target/issue-1587.rs b/tests/target/issue-1587.rs
new file mode 100644 (file)
index 0000000..b2796ef
--- /dev/null
@@ -0,0 +1,8 @@
+pub trait X {
+    fn a(&self) -> &'static str;
+    fn bcd(&self,
+           c: &str, // comment on this arg
+           d: u16, // comment on this arg
+           e: &Vec<String> // comment on this arg
+           ) -> Box<Q>;
+}