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 diff --git a/tests/target/fn.rs b/tests/target/fn.rs index dff8085136e..bc05efa5351 100644 --- a/tests/target/fn.rs +++ b/tests/target/fn.rs @@ -34,6 +34,9 @@ fn foo<U, T>(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, } +fn foo<U: Fn(A) -> B /* paren inside generics */>() { +} + impl Foo { fn with_no_errors<T, F>(&mut self, f: F) -> T where F: FnOnce(&mut Resolver) -> T