Merge pull request #307 from marcusklaas/function-arg-ty

Rewrite types in function arguments
This commit is contained in:
Nick Cameron 2015-09-12 10:01:53 +12:00
commit 764793b2e6
4 changed files with 40 additions and 16 deletions

View File

@ -21,7 +21,7 @@ use visitor::FmtVisitor;
use config::MultilineStyle; use config::MultilineStyle;
use comment::{FindUncommented, rewrite_comment, contains_comment}; use comment::{FindUncommented, rewrite_comment, contains_comment};
use types::rewrite_path; use types::rewrite_path;
use items::{span_lo_for_arg, span_hi_for_arg, rewrite_fn_input}; use items::{span_lo_for_arg, span_hi_for_arg};
use chains::rewrite_chain; use chains::rewrite_chain;
use syntax::{ast, ptr}; use syntax::{ast, ptr};
@ -182,7 +182,15 @@ fn rewrite_closure(capture: ast::CaptureClause,
"|", "|",
|arg| span_lo_for_arg(arg), |arg| span_lo_for_arg(arg),
|arg| span_hi_for_arg(arg), |arg| span_hi_for_arg(arg),
|arg| rewrite_fn_input(arg), |arg| {
// FIXME: we should just escalate failure
// here, but itemize_list doesn't allow it.
arg.rewrite(context, budget, argument_offset)
.unwrap_or_else(|| {
context.snippet(mk_sp(span_lo_for_arg(arg),
span_hi_for_arg(arg)))
})
},
span_after(span, "|", context.codemap), span_after(span, "|", context.codemap),
body.span.lo); body.span.lo);

View File

@ -12,7 +12,7 @@
use {ReturnIndent, BraceStyle, StructLitStyle}; use {ReturnIndent, BraceStyle, StructLitStyle};
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after, use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after,
end_typaram}; end_typaram, wrap_str};
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic}; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
use expr::rewrite_assign_rhs; use expr::rewrite_assign_rhs;
use comment::FindUncommented; use comment::FindUncommented;
@ -329,7 +329,13 @@ impl<'a> FmtVisitor<'a> {
arg_indent: usize, arg_indent: usize,
span: Span) span: Span)
-> Option<String> { -> Option<String> {
let mut arg_item_strs: Vec<_> = args.iter().map(rewrite_fn_input).collect(); let context = self.get_context();
let mut arg_item_strs = try_opt!(args.iter()
.map(|arg| {
arg.rewrite(&context, multi_line_budget, indent)
})
.collect::<Option<Vec<_>>>());
// Account for sugary self. // Account for sugary self.
// FIXME: the comment for the self argument is dropped. This is blocked // FIXME: the comment for the self argument is dropped. This is blocked
// on rust issue #27522. // on rust issue #27522.
@ -342,7 +348,7 @@ impl<'a> FmtVisitor<'a> {
}) })
.unwrap_or(1); .unwrap_or(1);
// Comments between args // Comments between args.
let mut arg_items = Vec::new(); let mut arg_items = Vec::new();
if min_args == 2 { if min_args == 2 {
arg_items.push(ListItem::from_str("")); arg_items.push(ListItem::from_str(""));
@ -925,19 +931,22 @@ impl Rewrite for ast::FunctionRetTy {
} }
} }
// TODO we farm this out, but this could spill over the column limit, so we impl Rewrite for ast::Arg {
// ought to handle it properly. fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
pub fn rewrite_fn_input(arg: &ast::Arg) -> String { if is_named_arg(self) {
if is_named_arg(arg) { if let ast::Ty_::TyInfer = self.ty.node {
if let ast::Ty_::TyInfer = arg.ty.node { wrap_str(pprust::pat_to_string(&self.pat), context.config.max_width, width, offset)
pprust::pat_to_string(&arg.pat) } else {
let mut result = pprust::pat_to_string(&self.pat);
result.push_str(": ");
let max_width = try_opt!(width.checked_sub(result.len()));
let ty_str = try_opt!(self.ty.rewrite(context, max_width, offset + result.len()));
result.push_str(&ty_str);
Some(result)
}
} else { } else {
format!("{}: {}", self.ty.rewrite(context, width, offset)
pprust::pat_to_string(&arg.pat),
pprust::ty_to_string(&arg.ty))
} }
} else {
pprust::ty_to_string(&arg.ty)
} }
} }

View File

@ -18,4 +18,7 @@ fn generic<T>(arg: T) -> &SomeType
arg(a, b, c, d, e) arg(a, b, c, d, e)
} }
pub fn http_fetch_async(listener:Box< AsyncCORSResponseListener+Send >, script_chan: Box<ScriptChan+Send>) {
}
fn some_func<T:Box<Trait+Bound>>(val:T){} fn some_func<T:Box<Trait+Bound>>(val:T){}

View File

@ -28,5 +28,9 @@ fn generic<T>(arg: T) -> &SomeType
arg(a, b, c, d, e) arg(a, b, c, d, e)
} }
pub fn http_fetch_async(listener: Box<AsyncCORSResponseListener + Send>,
script_chan: Box<ScriptChan + Send>) {
}
fn some_func<T: Box<Trait + Bound>>(val: T) { fn some_func<T: Box<Trait + Bound>>(val: T) {
} }