mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 20:54:13 +00:00
Rewrite types in function arguments
This commit is contained in:
parent
0b7b3c8725
commit
623d6c972a
12
src/expr.rs
12
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);
|
||||
|
||||
|
37
src/items.rs
37
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<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.
|
||||
// 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<String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,4 +18,7 @@ fn generic<T>(arg: T) -> &SomeType
|
||||
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){}
|
||||
|
@ -28,5 +28,9 @@ fn generic<T>(arg: T) -> &SomeType
|
||||
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) {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user