Fix off by 2 error in function sigs

Fixes #1000

This is a little conservative in some cases, but better than being wrong in others.
This commit is contained in:
Nick Cameron 2016-05-27 11:49:26 +12:00
parent 775de8a62b
commit 882ef8cc82
3 changed files with 26 additions and 11 deletions

View file

@ -1301,7 +1301,7 @@ fn rewrite_fn_base(context: &RewriteContext,
// Args.
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =
compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace);
try_opt!(compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace));
if context.config.fn_args_layout == FnArgLayoutStyle::Block ||
context.config.fn_args_layout == FnArgLayoutStyle::BlockAlways {
@ -1617,7 +1617,7 @@ fn compute_budgets_for_args(context: &RewriteContext,
indent: Indent,
ret_str_len: usize,
newline_brace: bool)
-> (usize, usize, Indent) {
-> Option<((usize, usize, Indent))> {
// Try keeping everything on the same line.
if !result.contains("\n") {
// 3 = `() `, space is before ret_string.
@ -1628,23 +1628,23 @@ fn compute_budgets_for_args(context: &RewriteContext,
let one_line_budget = context.config.max_width.checked_sub(used_space).unwrap_or(0);
if one_line_budget > 0 {
let multi_line_budget = context.config.max_width -
(indent.width() + result.len() + "()".len());
// 4 = "() {".len()
let multi_line_budget =
try_opt!(context.config.max_width.checked_sub(indent.width() + result.len() + 4));
return (one_line_budget, multi_line_budget, indent + result.len() + 1);
return Some((one_line_budget, multi_line_budget, indent + result.len() + 1));
}
}
// Didn't work. we must force vertical layout and put args on a newline.
let new_indent = indent.block_indent(context.config);
let used_space = new_indent.width() + 2; // account for `(` and `)`
let used_space = new_indent.width() + 4; // Account for `(` and `)` and possibly ` {`.
let max_space = context.config.max_width;
if used_space <= max_space {
(0, max_space - used_space, new_indent)
Some((0, max_space - used_space, new_indent))
} else {
// Whoops! bankrupt.
// FIXME: take evasive action, perhaps kill the indent or something.
panic!("in compute_budgets_for_args");
None
}
}

View file

@ -2,8 +2,8 @@
// Test some of the ways function signatures can be customised.
// Test compressed layout of args.
fn foo(a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee) {
fn foo(a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee) {
foo();
}

View file

@ -88,3 +88,18 @@ fn ______________________baz(a: i32)
arg3: i32)
-> ()> {
}
pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
path: &hir::Path,
id: ast::NodeId,
cb: &mut FnMut(DefId, Span, &Option<&Stability>, &Option<Depecation>)) {
}
pub fn check_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
path: &hir::Path,
id: ast::NodeId,
cb: &mut FnMut(DefId,
Span,
&Option<&Stability>,
&Option<Deprecation>)) {
}