Skip to content

Commit fc44837

Browse files
committed
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.
1 parent 54d2620 commit fc44837

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/items.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Formatting top-level items - functions, structs, enums, traits, impls.
1212

1313
use {ReturnIndent, BraceStyle};
14-
use utils::{format_visibility, make_indent, contains_skip, span_after};
14+
use utils::{format_visibility, make_indent, contains_skip, span_after, end_typaram};
1515
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
1616
use comment::FindUncommented;
1717
use visitor::FmtVisitor;
@@ -160,13 +160,20 @@ impl<'a> FmtVisitor<'a> {
160160
result.push('(');
161161
}
162162

163+
// A conservative estimation, to goal is to be over all parens in generics
164+
let args_start = generics.ty_params
165+
.last()
166+
.map(|tp| end_typaram(tp))
167+
.unwrap_or(span.lo);
168+
let args_span = codemap::mk_sp(
169+
span_after(codemap::mk_sp(args_start, span.hi), "(", self.codemap),
170+
span_for_return(&fd.output).lo);
163171
result.push_str(&self.rewrite_args(&fd.inputs,
164172
explicit_self,
165173
one_line_budget,
166174
multi_line_budget,
167175
arg_indent,
168-
codemap::mk_sp(span_after(span, "(", self.codemap),
169-
span_for_return(&fd.output).lo)));
176+
args_span));
170177
result.push(')');
171178

172179
// Return type.

src/utils.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::ast::{Visibility, Attribute, MetaItem, MetaItem_};
11+
use syntax::ast::{self, Visibility, Attribute, MetaItem, MetaItem_};
1212
use syntax::codemap::{CodeMap, Span, BytePos};
1313

1414
use comment::FindUncommented;
@@ -72,6 +72,14 @@ pub fn contains_skip(attrs: &[Attribute]) -> bool {
7272
attrs.iter().any(|a| is_skip(&a.node.value))
7373
}
7474

75+
// Find the end of a TyParam
76+
pub fn end_typaram(typaram: &ast::TyParam) -> BytePos {
77+
typaram.bounds.last().map(|bound| match *bound {
78+
ast::RegionTyParamBound(ref lt) => lt.span,
79+
ast::TraitTyParamBound(ref prt, _) => prt.span,
80+
}).unwrap_or(typaram.span).hi
81+
}
82+
7583
#[inline]
7684
#[cfg(target_pointer_width="64")]
7785
// Based on the trick layed out at

tests/target/fn.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ fn foo<U, T>(a: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
3434

3535
}
3636

37+
fn foo<U: Fn(A) -> B /* paren inside generics */>() {
38+
}
39+
3740
impl Foo {
3841
fn with_no_errors<T, F>(&mut self, f: F) -> T
3942
where F: FnOnce(&mut Resolver) -> T

0 commit comments

Comments
 (0)