mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-10 19:16:51 +00:00
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:
parent
54d2620ead
commit
fc4483748c
13
src/items.rs
13
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.
|
||||
|
10
src/utils.rs
10
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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user