Fixed named arguments in bare function types

This commit is contained in:
Marcus Klaas 2015-12-06 01:11:26 +01:00
parent 22353ca8c7
commit 97e4e7e5ba
7 changed files with 64 additions and 18 deletions

View File

@ -11,6 +11,8 @@
use std::cmp::Ordering;
use std::borrow::Borrow;
use std::mem::swap;
use std::ops::Deref;
use std::iter::ExactSizeIterator;
use {Indent, Spanned};
use rewrite::{Rewrite, RewriteContext};
@ -75,7 +77,11 @@ impl Rewrite for ast::Expr {
offset)
}
ast::Expr_::ExprTup(ref items) => {
rewrite_tuple(context, items, self.span, width, offset)
rewrite_tuple(context,
items.iter().map(|x| &**x),
self.span,
width,
offset)
}
ast::Expr_::ExprWhile(ref cond, ref block, label) => {
Loop::new_while(None, cond, block, label).rewrite(context, width, offset)
@ -960,7 +966,7 @@ impl Rewrite for ast::Arm {
let budget = context.config.max_width - line_start - comma.len() - 4;
let offset = Indent::new(offset.block_indent, line_start + 4 - offset.block_indent);
let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
let is_block = if let ast::ExprBlock(ref block) = body.node {
let is_block = if let ast::ExprBlock(..) = body.node {
true
} else {
false
@ -1431,25 +1437,27 @@ fn rewrite_field(context: &RewriteContext,
expr.map(|s| format!("{}: {}", name, s))
}
pub fn rewrite_tuple<'a, R>(context: &RewriteContext,
items: &'a [ptr::P<R>],
pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
mut items: I,
span: Span,
width: usize,
offset: Indent)
-> Option<String>
where R: Rewrite + Spanned + 'a
where I: ExactSizeIterator,
<I as Iterator>::Item: Deref,
<I::Item as Deref>::Target: Rewrite + Spanned + 'a
{
debug!("rewrite_tuple_lit: width: {}, offset: {:?}", width, offset);
let indent = offset + 1;
// In case of length 1, need a trailing comma
if items.len() == 1 {
// 3 = "(" + ",)"
let budget = try_opt!(width.checked_sub(3));
return items[0].rewrite(context, budget, indent).map(|s| format!("({},)", s));
return items.next().unwrap().rewrite(context, budget, indent).map(|s| format!("({},)", s));
}
let list_lo = span_after(span, "(", context.codemap);
let items = itemize_list(context.codemap,
items.iter(),
items,
")",
|item| item.span().lo,
|item| item.span().hi,
@ -1460,7 +1468,7 @@ pub fn rewrite_tuple<'a, R>(context: &RewriteContext,
1));
item.rewrite(context, inner_width, indent)
},
span.lo + BytePos(1), // Remove parens
list_lo,
span.hi - BytePos(1));
let budget = try_opt!(width.checked_sub(2));
let list_str = try_opt!(format_fn_args(items, budget, indent, context.config));

View File

@ -968,7 +968,7 @@ pub fn span_hi_for_arg(arg: &ast::Arg) -> BytePos {
}
}
fn is_named_arg(arg: &ast::Arg) -> bool {
pub fn is_named_arg(arg: &ast::Arg) -> bool {
if let ast::Pat_::PatIdent(_, ident, _) = arg.pat.node {
ident.node != token::special_idents::invalid
} else {

View File

@ -26,7 +26,7 @@ extern crate diff;
extern crate term;
use syntax::ast;
use syntax::codemap::Span;
use syntax::codemap::{mk_sp, Span};
use syntax::diagnostic::{EmitterWriter, Handler};
use syntax::parse::{self, ParseSess};
@ -88,6 +88,16 @@ impl Spanned for ast::Ty {
}
}
impl Spanned for ast::Arg {
fn span(&self) -> Span {
if items::is_named_arg(self) {
mk_sp(self.pat.span.lo, self.ty.span.hi)
} else {
self.ty.span
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of

View File

@ -48,7 +48,13 @@ impl Rewrite for Pat {
let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, width, offset)
}
Pat_::PatTup(ref items) => rewrite_tuple(context, items, self.span, width, offset),
Pat_::PatTup(ref items) => {
rewrite_tuple(context,
items.iter().map(|x| &**x),
self.span,
width,
offset)
}
Pat_::PatEnum(ref path, Some(ref pat_vec)) => {
let path_str = try_opt!(::types::rewrite_path(context,
true,

View File

@ -8,12 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::ops::Deref;
use std::iter::ExactSizeIterator;
use syntax::ast::{self, Mutability, FunctionRetTy};
use syntax::print::pprust;
use syntax::codemap::{self, Span, BytePos};
use syntax::abi;
use Indent;
use {Indent, Spanned};
use lists::{format_item_list, itemize_list, format_fn_args};
use rewrite::{Rewrite, RewriteContext};
use utils::{extra_offset, span_after, format_mutability, wrap_str};
@ -239,7 +242,9 @@ fn format_function_type<'a, I>(inputs: I,
width: usize,
offset: Indent)
-> Option<String>
where I: Iterator<Item = &'a ast::Ty>
where I: ExactSizeIterator,
<I as Iterator>::Item: Deref,
<I::Item as Deref>::Target: Rewrite + Spanned + 'a
{
// 2 for ()
let budget = try_opt!(width.checked_sub(2));
@ -249,8 +254,8 @@ fn format_function_type<'a, I>(inputs: I,
let items = itemize_list(context.codemap,
inputs,
")",
|ty| ty.span.lo,
|ty| ty.span.hi,
|ty| ty.span().lo,
|ty| ty.span().hi,
|ty| ty.rewrite(context, budget, offset),
list_lo,
span.hi);
@ -506,7 +511,13 @@ impl Rewrite for ast::Ty {
let budget = try_opt!(width.checked_sub(2));
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
}
ast::TyTup(ref items) => rewrite_tuple(context, items, self.span, width, offset),
ast::TyTup(ref items) => {
rewrite_tuple(context,
items.iter().map(|x| &**x),
self.span,
width,
offset)
}
ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset),
ast::TyPath(ref q_self, ref path) => {
rewrite_path(context, false, q_self.as_ref(), path, width, offset)
@ -548,7 +559,7 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
let budget = try_opt!(width.checked_sub(result.len()));
let indent = offset + result.len();
let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter().map(|x| &*(x.ty)),
let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter(),
&bare_fn.decl.output,
span,
context,

View File

@ -145,3 +145,9 @@ mod m {
struct Foo<T>(TTTTTTTTTTTTTTTTTTT,
/// Qux
UUUUUUUUUUUUUUUUUUU);
struct Issue677 {
pub ptr: *const libc::c_void,
pub trace: fn( obj:
*const libc::c_void, tracer : *mut JSTracer ),
}

View File

@ -153,3 +153,8 @@ mod m {
struct Foo<T>(TTTTTTTTTTTTTTTTTTT,
/// Qux
UUUUUUUUUUUUUUUUUUU);
struct Issue677 {
pub ptr: *const libc::c_void,
pub trace: fn(obj: *const libc::c_void, tracer: *mut JSTracer),
}