1
Fork 0

Merge pull request #678 from marcusklaas/length-one-tuplez

Fixed named arguments in bare function types
This commit is contained in:
Nick Cameron 2015-12-07 09:01:38 +13:00
commit 585b071aa1
7 changed files with 64 additions and 18 deletions

View file

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

View file

@ -966,7 +966,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 { if let ast::Pat_::PatIdent(_, ident, _) = arg.pat.node {
ident.node != token::special_idents::invalid ident.node != token::special_idents::invalid
} else { } else {

View file

@ -26,7 +26,7 @@ extern crate diff;
extern crate term; extern crate term;
use syntax::ast; use syntax::ast;
use syntax::codemap::Span; use syntax::codemap::{mk_sp, Span};
use syntax::diagnostic::{EmitterWriter, Handler}; use syntax::diagnostic::{EmitterWriter, Handler};
use syntax::parse::{self, ParseSess}; 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)] #[derive(Copy, Clone, Debug)]
pub struct Indent { pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of // 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)); let prefix = format!("&{}", format_mutability(mutability));
rewrite_unary_prefix(context, &prefix, &**pat, width, offset) 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)) => { Pat_::PatEnum(ref path, Some(ref pat_vec)) => {
let path_str = try_opt!(::types::rewrite_path(context, let path_str = try_opt!(::types::rewrite_path(context,
true, true,

View file

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

View file

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