Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1dd54e6

Browse files
authored
Prefer to break arguments over putting output type on the next line (rust-lang#3190)
1 parent ef4176a commit 1dd54e6

File tree

5 files changed

+74
-49
lines changed

5 files changed

+74
-49
lines changed

src/types.rs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,19 @@ where
289289
{
290290
debug!("format_function_type {:#?}", shape);
291291

292+
let ty_shape = match context.config.indent_style() {
293+
// 4 = " -> "
294+
IndentStyle::Block => shape.offset_left(4)?,
295+
IndentStyle::Visual => shape.block_left(4)?,
296+
};
297+
let output = match *output {
298+
FunctionRetTy::Ty(ref ty) => {
299+
let type_str = ty.rewrite(context, ty_shape)?;
300+
format!(" -> {}", type_str)
301+
}
302+
FunctionRetTy::Default(..) => String::new(),
303+
};
304+
292305
// Code for handling variadics is somewhat duplicated for items, but they
293306
// are different enough to need some serious refactoring to share code.
294307
enum ArgumentKind<T>
@@ -307,19 +320,18 @@ where
307320
None
308321
};
309322

310-
// 2 for ()
311-
let budget = shape.width.checked_sub(2)?;
312-
// 1 for (
313-
let offset = match context.config.indent_style() {
314-
IndentStyle::Block => {
315-
shape
316-
.block()
317-
.block_indent(context.config.tab_spaces())
318-
.indent
319-
}
320-
IndentStyle::Visual => shape.indent + 1,
323+
let list_shape = if context.use_block_indent() {
324+
Shape::indented(
325+
shape.block().indent.block_indent(context.config),
326+
context.config,
327+
)
328+
} else {
329+
// 2 for ()
330+
let budget = shape.width.checked_sub(2)?;
331+
// 1 for (
332+
let offset = shape.indent + 1;
333+
Shape::legacy(budget, offset)
321334
};
322-
let list_shape = Shape::legacy(budget, offset);
323335
let list_lo = context.snippet_provider.span_after(span, "(");
324336
let items = itemize_list(
325337
context.snippet_provider,
@@ -345,12 +357,18 @@ where
345357

346358
let item_vec: Vec<_> = items.collect();
347359

348-
let tactic = definitive_tactic(
349-
&*item_vec,
350-
ListTactic::HorizontalVertical,
351-
Separator::Comma,
352-
budget,
353-
);
360+
// If the return type is multi-lined, then force to use multiple lines for
361+
// arguments as well.
362+
let tactic = if output.contains('\n') {
363+
DefinitiveListTactic::Vertical
364+
} else {
365+
definitive_tactic(
366+
&*item_vec,
367+
ListTactic::HorizontalVertical,
368+
Separator::Comma,
369+
shape.width.saturating_sub(2 + output.len()),
370+
)
371+
};
354372
let trailing_separator = if !context.use_block_indent() || variadic {
355373
SeparatorTactic::Never
356374
} else {
@@ -364,27 +382,12 @@ where
364382
.preserve_newline(true);
365383
let list_str = write_list(&item_vec, &fmt)?;
366384

367-
let ty_shape = match context.config.indent_style() {
368-
// 4 = " -> "
369-
IndentStyle::Block => shape.offset_left(4)?,
370-
IndentStyle::Visual => shape.block_left(4)?,
371-
};
372-
let output = match *output {
373-
FunctionRetTy::Ty(ref ty) => {
374-
let type_str = ty.rewrite(context, ty_shape)?;
375-
format!(" -> {}", type_str)
376-
}
377-
FunctionRetTy::Default(..) => String::new(),
378-
};
379-
380-
let args = if (!list_str.contains('\n') || list_str.is_empty()) && !output.contains('\n')
381-
|| !context.use_block_indent()
382-
{
385+
let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
383386
format!("({})", list_str)
384387
} else {
385388
format!(
386389
"({}{}{})",
387-
offset.to_string_with_newline(context.config),
390+
list_shape.indent.to_string_with_newline(context.config),
388391
list_str,
389392
shape.block().indent.to_string_with_newline(context.config),
390393
)
@@ -395,7 +398,7 @@ where
395398
Some(format!(
396399
"{}\n{}{}",
397400
args,
398-
offset.to_string(context.config),
401+
list_shape.indent.to_string(context.config),
399402
output.trim_left()
400403
))
401404
}

tests/source/type.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ fn issue3139() {
129129
json!( { "test": None :: <i32> } )
130130
);
131131
}
132+
133+
// #3180
134+
fn foo(a: SomeLongComplexType, b: SomeOtherLongComplexType) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>> {
135+
}
136+
137+
type MyFn = fn(a: SomeLongComplexType, b: SomeOtherLongComplexType,) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

tests/target/fn-simple.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ fn simple(
99
x: Typ,
1010
key: &[u8],
1111
upd: Box<
12-
Fn(Option<&memcache::Item>)
13-
-> (memcache::Status, Result<memcache::Item, Option<String>>),
12+
Fn(
13+
Option<&memcache::Item>,
14+
) -> (memcache::Status, Result<memcache::Item, Option<String>>),
1415
>,
1516
) -> MapResult {
1617
}

tests/target/issue-2164.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ pub struct emacs_env_25 {
5656
) -> emacs_value,
5757
>,
5858
pub intern: ::std::option::Option<
59-
unsafe extern "C" fn(env: *mut emacs_env, symbol_name: *const ::libc::c_char)
60-
-> emacs_value,
59+
unsafe extern "C" fn(
60+
env: *mut emacs_env,
61+
symbol_name: *const ::libc::c_char,
62+
) -> emacs_value,
6163
>,
6264
pub type_of: ::std::option::Option<
6365
unsafe extern "C" fn(env: *mut emacs_env, value: emacs_value) -> emacs_value,
@@ -87,15 +89,16 @@ pub struct emacs_env_25 {
8789
) -> bool,
8890
>,
8991
pub make_string: ::std::option::Option<
90-
unsafe extern "C" fn(env: *mut emacs_env, contents: *const ::libc::c_char, length: isize)
91-
-> emacs_value,
92+
unsafe extern "C" fn(
93+
env: *mut emacs_env,
94+
contents: *const ::libc::c_char,
95+
length: isize,
96+
) -> emacs_value,
9297
>,
9398
pub make_user_ptr: ::std::option::Option<
9499
unsafe extern "C" fn(
95100
env: *mut emacs_env,
96-
fin: ::std::option::Option<
97-
unsafe extern "C" fn(arg1: *mut ::libc::c_void),
98-
>,
101+
fin: ::std::option::Option<unsafe extern "C" fn(arg1: *mut ::libc::c_void)>,
99102
ptr: *mut ::libc::c_void,
100103
) -> emacs_value,
101104
>,
@@ -107,7 +110,9 @@ pub struct emacs_env_25 {
107110
>,
108111
pub get_user_finalizer: ::std::option::Option<
109112
unsafe extern "C" fn(
110-
arg1: *mut ::libc::c_void, env: *mut emacs_env, uptr: emacs_value
113+
arg1: *mut ::libc::c_void,
114+
env: *mut emacs_env,
115+
uptr: emacs_value,
111116
) -> ::std::option::Option<
112117
unsafe extern "C" fn(arg1: *mut ::libc::c_void, env: *mut emacs_env, uptr: emacs_value),
113118
>,
@@ -116,9 +121,7 @@ pub struct emacs_env_25 {
116121
unsafe extern "C" fn(
117122
env: *mut emacs_env,
118123
uptr: emacs_value,
119-
fin: ::std::option::Option<
120-
unsafe extern "C" fn(arg1: *mut ::libc::c_void),
121-
>,
124+
fin: ::std::option::Option<unsafe extern "C" fn(arg1: *mut ::libc::c_void)>,
122125
),
123126
>,
124127
pub vec_get: ::std::option::Option<

tests/target/type.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,15 @@ fn issue3139() {
128128
json!({ "test": None::<i32> })
129129
);
130130
}
131+
132+
// #3180
133+
fn foo(
134+
a: SomeLongComplexType,
135+
b: SomeOtherLongComplexType,
136+
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>> {
137+
}
138+
139+
type MyFn = fn(
140+
a: SomeLongComplexType,
141+
b: SomeOtherLongComplexType,
142+
) -> Box<Future<Item = AnotherLongType, Error = ALongErrorType>>;

0 commit comments

Comments
 (0)