From 623d6c972a3765cf8e297ccd0570011c6807e0c0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Fri, 11 Sep 2015 12:24:13 +0200 Subject: [PATCH] Rewrite types in function arguments --- src/expr.rs | 12 ++++++++++-- src/items.rs | 37 +++++++++++++++++++++++-------------- tests/source/fn-simple.rs | 3 +++ tests/target/fn-simple.rs | 4 ++++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index c7dad3d2ccf..530cf8eda47 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -21,7 +21,7 @@ use visitor::FmtVisitor; use config::MultilineStyle; use comment::{FindUncommented, rewrite_comment, contains_comment}; 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 syntax::{ast, ptr}; @@ -182,7 +182,15 @@ fn rewrite_closure(capture: ast::CaptureClause, "|", |arg| span_lo_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), body.span.lo); diff --git a/src/items.rs b/src/items.rs index 958dbb20df0..842f2699c2a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -12,7 +12,7 @@ use {ReturnIndent, BraceStyle, StructLitStyle}; 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 expr::rewrite_assign_rhs; use comment::FindUncommented; @@ -329,7 +329,13 @@ impl<'a> FmtVisitor<'a> { arg_indent: usize, span: Span) -> Option { - 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::>>()); + // Account for sugary self. // FIXME: the comment for the self argument is dropped. This is blocked // on rust issue #27522. @@ -342,7 +348,7 @@ impl<'a> FmtVisitor<'a> { }) .unwrap_or(1); - // Comments between args + // Comments between args. let mut arg_items = Vec::new(); if min_args == 2 { 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 -// ought to handle it properly. -pub fn rewrite_fn_input(arg: &ast::Arg) -> String { - if is_named_arg(arg) { - if let ast::Ty_::TyInfer = arg.ty.node { - pprust::pat_to_string(&arg.pat) +impl Rewrite for ast::Arg { + fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option { + if is_named_arg(self) { + if let ast::Ty_::TyInfer = self.ty.node { + wrap_str(pprust::pat_to_string(&self.pat), context.config.max_width, width, offset) + } 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 { - format!("{}: {}", - pprust::pat_to_string(&arg.pat), - pprust::ty_to_string(&arg.ty)) + self.ty.rewrite(context, width, offset) } - } else { - pprust::ty_to_string(&arg.ty) } } diff --git a/tests/source/fn-simple.rs b/tests/source/fn-simple.rs index b71fdc47875..30d11dbc71e 100644 --- a/tests/source/fn-simple.rs +++ b/tests/source/fn-simple.rs @@ -18,4 +18,7 @@ fn generic(arg: T) -> &SomeType arg(a, b, c, d, e) } +pub fn http_fetch_async(listener:Box< AsyncCORSResponseListener+Send >, script_chan: Box) { +} + fn some_func>(val:T){} diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index 899a73d5157..f01e85193f5 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -28,5 +28,9 @@ fn generic(arg: T) -> &SomeType arg(a, b, c, d, e) } +pub fn http_fetch_async(listener: Box, + script_chan: Box) { +} + fn some_func>(val: T) { }