about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGaëtan Cassiers <gaetan.cassiers@gmail.com>2015-07-02 01:20:07 +0200
committerGaëtan Cassiers <gaetan.cassiers@gmail.com>2015-07-03 10:30:42 +0200
commitfc4483748c83e972a76dd3f2c215e2d8a480cb98 (patch)
tree6e67ad1688ce8941f69a2ef93596b93fcd7cff09 /src
parent54d2620ead1dd8ace78f5877e58971b96cd14767 (diff)
Fix fn decl rewriting in case of generics
An opening paren in generics caused a false-positive detection of args
beginning. The result was the creation of comments with some code into
it.
Diffstat (limited to 'src')
-rw-r--r--src/items.rs13
-rw-r--r--src/utils.rs10
2 files changed, 19 insertions, 4 deletions
diff --git a/src/items.rs b/src/items.rs
index 17941dcd242..396778352d1 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -11,7 +11,7 @@
 // Formatting top-level items - functions, structs, enums, traits, impls.
 
 use {ReturnIndent, BraceStyle};
-use utils::{format_visibility, make_indent, contains_skip, span_after};
+use utils::{format_visibility, make_indent, contains_skip, span_after, end_typaram};
 use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
 use comment::FindUncommented;
 use visitor::FmtVisitor;
@@ -160,13 +160,20 @@ impl<'a> FmtVisitor<'a> {
             result.push('(');
         }
 
+        // A conservative estimation, to goal is to be over all parens in generics
+        let args_start = generics.ty_params
+                                 .last()
+                                 .map(|tp| end_typaram(tp))
+                                 .unwrap_or(span.lo);
+        let args_span = codemap::mk_sp(
+            span_after(codemap::mk_sp(args_start, span.hi), "(", self.codemap),
+            span_for_return(&fd.output).lo);
         result.push_str(&self.rewrite_args(&fd.inputs,
                                            explicit_self,
                                            one_line_budget,
                                            multi_line_budget,
                                            arg_indent,
-                                           codemap::mk_sp(span_after(span, "(", self.codemap),
-                                                          span_for_return(&fd.output).lo)));
+                                           args_span));
         result.push(')');
 
         // Return type.
diff --git a/src/utils.rs b/src/utils.rs
index de17f989f7a..47203a0754f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use syntax::ast::{Visibility, Attribute, MetaItem, MetaItem_};
+use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItem_};
 use syntax::codemap::{CodeMap, Span, BytePos};
 
 use comment::FindUncommented;
@@ -72,6 +72,14 @@ pub fn contains_skip(attrs: &[Attribute]) -> bool {
     attrs.iter().any(|a| is_skip(&a.node.value))
 }
 
+// Find the end of a TyParam
+pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
+    typaram.bounds.last().map(|bound| match *bound {
+        ast::RegionTyParamBound(ref lt) => lt.span,
+        ast::TraitTyParamBound(ref prt, _) => prt.span,
+    }).unwrap_or(typaram.span).hi
+}
+
 #[inline]
 #[cfg(target_pointer_width="64")]
 // Based on the trick layed out at