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.
This commit is contained in:
Gaëtan Cassiers 2015-07-02 01:20:07 +02:00
parent 54d2620ead
commit fc4483748c
3 changed files with 22 additions and 4 deletions

View File

@ -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.

View File

@ -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

View File

@ -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