diff --git a/src/chains.rs b/src/chains.rs index a2976bbe92a..88efe1736b4 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -66,6 +66,7 @@ use crate::config::{IndentStyle, Version}; use crate::expr::rewrite_call; use crate::lists::extract_pre_comment; use crate::macros::convert_try_mac; +use crate::overflow::rewrite_with_angle_brackets; use crate::rewrite::{Rewrite, RewriteContext}; use crate::shape::Shape; use crate::source_map::SpanUtils; @@ -296,21 +297,29 @@ impl ChainItem { context: &RewriteContext<'_>, shape: Shape, ) -> Option { + let callee_str = rewrite_ident(context, method_name); let type_str = if types.is_empty() { String::new() } else { - let type_list = types - .iter() - .map(|ty| ty.rewrite(context, shape)) - .collect::>>()?; - - format!("::<{}>", type_list.join(", ")) + // subtract `{callee_str}::<>()` from the shape. We can only place everything on a + // single line if there's enough room for the callee, turbofish, generic args, and () + let shape = shape.sub_width(callee_str.len() + 6)?; + rewrite_generic_args(types, span, context, shape)? }; - let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str); + let callee_str = format!(".{}{}", callee_str, type_str); rewrite_call(context, &callee_str, &args[1..], span, shape) } } +fn rewrite_generic_args( + types: &[ast::GenericArg], + span: Span, + context: &RewriteContext<'_>, + shape: Shape, +) -> Option { + rewrite_with_angle_brackets(context, "::", types.iter(), shape, span) +} + #[derive(Debug)] struct Chain { parent: ChainItem, diff --git a/src/overflow.rs b/src/overflow.rs index 6bf8cd0c70b..6fa3d3a4e81 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -69,6 +69,7 @@ const SPECIAL_CASE_ATTR: &[(&str, usize)] = &[ pub(crate) enum OverflowableItem<'a> { Expr(&'a ast::Expr), GenericParam(&'a ast::GenericParam), + GenericArg(&'a ast::GenericArg), MacroArg(&'a MacroArg), NestedMetaItem(&'a ast::NestedMetaItem), SegmentParam(&'a SegmentParam<'a>), @@ -108,6 +109,7 @@ impl<'a> OverflowableItem<'a> { { match self { OverflowableItem::Expr(expr) => f(*expr), + OverflowableItem::GenericArg(ga) => f(*ga), OverflowableItem::GenericParam(gp) => f(*gp), OverflowableItem::MacroArg(macro_arg) => f(*macro_arg), OverflowableItem::NestedMetaItem(nmi) => f(*nmi), @@ -232,7 +234,15 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types { } } -impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat); +impl_into_overflowable_item_for_ast_node!( + Expr, + GenericArg, + GenericParam, + NestedMetaItem, + FieldDef, + Ty, + Pat +); impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]); pub(crate) fn into_overflowable_list<'a, T>( diff --git a/tests/source/issue-5249/issue_5249.rs b/tests/source/issue-5249/issue_5249.rs new file mode 100644 index 00000000000..f923d102adb --- /dev/null +++ b/tests/source/issue-5249/issue_5249.rs @@ -0,0 +1,14 @@ +fn long_generic_args_list() { + x.f::< + A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, + A, A, A, A, A, A, A, A, A, A, A, A, + >(); +} + +fn long_items_in_generic_args_list() { + x.f::< + AAAA, + AAAA, + AAAA + >(); +} diff --git a/tests/source/issue-5249/multi_chain_method_call.rs b/tests/source/issue-5249/multi_chain_method_call.rs new file mode 100644 index 00000000000..f40cfdce7b2 --- /dev/null +++ b/tests/source/issue-5249/multi_chain_method_call.rs @@ -0,0 +1,53 @@ +fn chained_method_calls_short_generic_args_list_long_method_args_list() { + x.f::(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE).g::(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, + C, + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE).g::< + A, // Something about arg A + B, + C, + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_inline_comments_in_both_lists( +) { + x.f::< + A, // Something about arg A + B, + C, + >( + AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ).g::< + A, // Something about arg A + B, + C, + >( + AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_block_comments() { + x.f::( + AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ).g::( + AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_block_comments_in_both_lists( +) { + x.f::( + AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, + EEEEEEEEEE, + ).g::( + AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, + EEEEEEEEEE, + ); +} diff --git a/tests/source/issue-5249/single_line_generic_args.rs b/tests/source/issue-5249/single_line_generic_args.rs new file mode 100644 index 00000000000..8f1c7a65273 --- /dev/null +++ b/tests/source/issue-5249/single_line_generic_args.rs @@ -0,0 +1,20 @@ +fn short_generic_args_list() { + x.f::< + A, + B, C + >(); +} + +fn short_generic_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, C + >(); +} + +fn short_generic_args_list_with_block_comments() { + x.f::< + A, /* Something about arg A */ + B, C + >(); +} diff --git a/tests/source/issue-5249/with_comments.rs b/tests/source/issue-5249/with_comments.rs new file mode 100644 index 00000000000..42bddbe1737 --- /dev/null +++ b/tests/source/issue-5249/with_comments.rs @@ -0,0 +1,54 @@ +fn long_items_in_generic_args_list_inline_comment() { + x.f::< + // Pre comment + AAAA, // Inline + AAAA, + AAAA, + // Post Comment + >(); +} + +fn long_items_in_generic_args_list_multi_line_inline_comment() { + x.f::< + // Pre comment + // Pre comment + // Pre comment + AAAA, // Inline + // Inline + // Inline + AAAA, + AAAA, + // Post Comment + // Post Comment + // Post Comment + >(); +} + +fn long_items_in_generic_args_list_block_comment() { + x.f::< + /* Pre comment */ + AAAA, /* Inline */ + AAAA, + AAAA, + /* Post Comment */ + >(); +} + +fn long_items_in_generic_args_list_multi_line_block_comment() { + x.f::< + /* Pre comment + * Pre comment + * Pre comment + */ + AAAA, /* Inline + * Inline + * Inline + */ + AAAA, + AAAA, + /* Post Comment + * Post Comment + * Post Comment + */ + >(); +} diff --git a/tests/source/issue-5249/with_method_arguments.rs b/tests/source/issue-5249/with_method_arguments.rs new file mode 100644 index 00000000000..d0c3428692f --- /dev/null +++ b/tests/source/issue-5249/with_method_arguments.rs @@ -0,0 +1,35 @@ +fn short_generic_args_list_long_method_args_list() { + x.f::< + A, + B, C + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, C + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_inline_comments_in_both_lists() { + x.f::< + A, // Something about arg A + B, C + >(AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_block_comments() { + x.f::< + A, /* Something about arg A */ + B, C + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_block_comments_in_both_lists() { + x.f::< + A, /* Something about arg A */ + B, C + >(AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} diff --git a/tests/source/issue_3191.rs b/tests/source/issue_3191.rs new file mode 100644 index 00000000000..fa0e97ba186 --- /dev/null +++ b/tests/source/issue_3191.rs @@ -0,0 +1,3 @@ +fn foo() { + unprivileged_content.start_all::(true); +} diff --git a/tests/source/issue_5657.rs b/tests/source/issue_5657.rs new file mode 100644 index 00000000000..4e11d2a486f --- /dev/null +++ b/tests/source/issue_5657.rs @@ -0,0 +1,13 @@ +// rustfmt-max_width: 80 + +fn omg() { +fn bar() { +self.core +.exchange::, _, LeaseBufWriter<_, BUFSIZ>, _>( +device_index, +src, +dest, +) +.map_err(RequestError::from) +} +} diff --git a/tests/target/issue-5249/issue_5249.rs b/tests/target/issue-5249/issue_5249.rs new file mode 100644 index 00000000000..0661448690e --- /dev/null +++ b/tests/target/issue-5249/issue_5249.rs @@ -0,0 +1,42 @@ +fn long_generic_args_list() { + x.f::< + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + A, + >(); +} + +fn long_items_in_generic_args_list() { + x.f::< + AAAA, + AAAA, + AAAA, + >(); +} diff --git a/tests/target/issue-5249/multi_chain_method_call.rs b/tests/target/issue-5249/multi_chain_method_call.rs new file mode 100644 index 00000000000..04d103686e8 --- /dev/null +++ b/tests/target/issue-5249/multi_chain_method_call.rs @@ -0,0 +1,58 @@ +fn chained_method_calls_short_generic_args_list_long_method_args_list() { + x.f::(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE) + .g::(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, + C, + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE) + .g::< + A, // Something about arg A + B, + C, + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_inline_comments_in_both_lists( +) { + x.f::< + A, // Something about arg A + B, + C, + >( + AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ) + .g::< + A, // Something about arg A + B, + C, + >( + AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_block_comments() { + x.f::( + AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ) + .g::( + AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn chained_method_calls_short_generic_args_list_long_method_args_list_with_block_comments_in_both_lists( +) { + x.f::( + AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, + EEEEEEEEEE, + ) + .g::( + AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, + EEEEEEEEEE, + ); +} diff --git a/tests/target/issue-5249/single_line_generic_args.rs b/tests/target/issue-5249/single_line_generic_args.rs new file mode 100644 index 00000000000..454cf2ce575 --- /dev/null +++ b/tests/target/issue-5249/single_line_generic_args.rs @@ -0,0 +1,15 @@ +fn short_generic_args_list() { + x.f::(); +} + +fn short_generic_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, + C, + >(); +} + +fn short_generic_args_list_with_block_comments() { + x.f::(); +} diff --git a/tests/target/issue-5249/with_comments.rs b/tests/target/issue-5249/with_comments.rs new file mode 100644 index 00000000000..1513be42ea7 --- /dev/null +++ b/tests/target/issue-5249/with_comments.rs @@ -0,0 +1,54 @@ +fn long_items_in_generic_args_list_inline_comment() { + x.f::< + // Pre comment + AAAA, // Inline + AAAA, + AAAA, + // Post Comment + >(); +} + +fn long_items_in_generic_args_list_multi_line_inline_comment() { + x.f::< + // Pre comment + // Pre comment + // Pre comment + AAAA, // Inline + // Inline + // Inline + AAAA, + AAAA, + // Post Comment + // Post Comment + // Post Comment + >(); +} + +fn long_items_in_generic_args_list_block_comment() { + x.f::< + /* Pre comment */ + AAAA, /* Inline */ + AAAA, + AAAA, + /* Post Comment */ + >(); +} + +fn long_items_in_generic_args_list_multi_line_block_comment() { + x.f::< + /* Pre comment + * Pre comment + * Pre comment + */ + AAAA, /* Inline + * Inline + * Inline + */ + AAAA, + AAAA, + /* Post Comment + * Post Comment + * Post Comment + */ + >(); +} diff --git a/tests/target/issue-5249/with_method_arguments.rs b/tests/target/issue-5249/with_method_arguments.rs new file mode 100644 index 00000000000..70d0dac375c --- /dev/null +++ b/tests/target/issue-5249/with_method_arguments.rs @@ -0,0 +1,35 @@ +fn short_generic_args_list_long_method_args_list() { + x.f::(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_inline_comments() { + x.f::< + A, // Something about arg A + B, + C, + >(AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE); +} + +fn short_generic_args_list_long_method_args_list_with_inline_comments_in_both_lists() { + x.f::< + A, // Something about arg A + B, + C, + >( + AAAAAAAA, BBBBBBBBB, // Something about arg BBBBBBBBB + CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn short_generic_args_list_long_method_args_list_with_block_comments() { + x.f::( + AAAAAAAA, BBBBBBBBB, CCCCCCCCC, DDDDDDDDDD, EEEEEEEEEE, + ); +} + +fn short_generic_args_list_long_method_args_list_with_block_comments_in_both_lists() { + x.f::( + AAAAAAAA, BBBBBBBBB, /* Something about arg BBBBBBBBB */ CCCCCCCCC, DDDDDDDDDD, + EEEEEEEEEE, + ); +} diff --git a/tests/target/issue_3191.rs b/tests/target/issue_3191.rs new file mode 100644 index 00000000000..dc85c59fd5b --- /dev/null +++ b/tests/target/issue_3191.rs @@ -0,0 +1,7 @@ +fn foo() { + unprivileged_content.start_all::< + script_layout_interface::message::Msg, + layout_thread::LayoutThread, + script::script_thread::ScriptThread, + >(true); +} diff --git a/tests/target/issue_5657.rs b/tests/target/issue_5657.rs new file mode 100644 index 00000000000..f7ea0a0a3e4 --- /dev/null +++ b/tests/target/issue_5657.rs @@ -0,0 +1,14 @@ +// rustfmt-max_width: 80 + +fn omg() { + fn bar() { + self.core + .exchange::< + LeaseBufReader<_, BUFSIZ>, + _, + LeaseBufWriter<_, BUFSIZ>, + _, + >(device_index, src, dest) + .map_err(RequestError::from) + } +}