From 61e17a11d17fda7f119faea4d1930620f24cb0af Mon Sep 17 00:00:00 2001 From: Konnor Andrews Date: Fri, 14 Mar 2025 21:08:10 -0600 Subject: [PATCH 1/2] implemented fix for issue 5249 --- src/chains.rs | 41 ++++++++----- src/overflow.rs | 3 + tests/source/issue-5249/issue_5249.rs | 14 +++++ .../issue-5249/multi_chain_method_call.rs | 53 +++++++++++++++++ .../issue-5249/single_line_generic_args.rs | 20 +++++++ tests/source/issue-5249/with_comments.rs | 54 +++++++++++++++++ .../issue-5249/with_method_arguments.rs | 35 +++++++++++ tests/source/issue_3191.rs | 3 + tests/source/issue_5657.rs | 13 +++++ tests/target/issue-5249/issue_5249.rs | 42 ++++++++++++++ .../issue-5249/multi_chain_method_call.rs | 58 +++++++++++++++++++ .../issue-5249/single_line_generic_args.rs | 15 +++++ tests/target/issue-5249/with_comments.rs | 54 +++++++++++++++++ .../issue-5249/with_method_arguments.rs | 35 +++++++++++ tests/target/issue_3191.rs | 7 +++ tests/target/issue_5657.rs | 14 +++++ 16 files changed, 448 insertions(+), 13 deletions(-) create mode 100644 tests/source/issue-5249/issue_5249.rs create mode 100644 tests/source/issue-5249/multi_chain_method_call.rs create mode 100644 tests/source/issue-5249/single_line_generic_args.rs create mode 100644 tests/source/issue-5249/with_comments.rs create mode 100644 tests/source/issue-5249/with_method_arguments.rs create mode 100644 tests/source/issue_3191.rs create mode 100644 tests/source/issue_5657.rs create mode 100644 tests/target/issue-5249/issue_5249.rs create mode 100644 tests/target/issue-5249/multi_chain_method_call.rs create mode 100644 tests/target/issue-5249/single_line_generic_args.rs create mode 100644 tests/target/issue-5249/with_comments.rs create mode 100644 tests/target/issue-5249/with_method_arguments.rs create mode 100644 tests/target/issue_3191.rs create mode 100644 tests/target/issue_5657.rs diff --git a/src/chains.rs b/src/chains.rs index 50a129b695f..627c7ac9598 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -67,14 +67,16 @@ use crate::config::{IndentStyle, StyleEdition}; use crate::expr::rewrite_call; use crate::lists::extract_pre_comment; use crate::macros::convert_try_mac; +use crate::overflow; use crate::rewrite::{ ExceedsMaxWidthError, Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult, }; use crate::shape::Shape; use crate::source_map::SpanUtils; +use crate::types::SegmentParam; use crate::utils::{ - self, filtered_str_fits, first_line_width, last_line_extendable, last_line_width, mk_sp, - rewrite_ident, trimmed_last_line_width, wrap_str, + self, extra_offset, filtered_str_fits, first_line_width, last_line_extendable, last_line_width, + mk_sp, rewrite_ident, trimmed_last_line_width, wrap_str, }; use thin_vec::ThinVec; @@ -342,18 +344,31 @@ impl ChainItem { context: &RewriteContext<'_>, shape: Shape, ) -> RewriteResult { - let type_str = if types.is_empty() { - String::new() - } else { - let type_list = types - .iter() - .map(|ty| ty.rewrite_result(context, shape)) - .collect::, RewriteError>>()?; + let mut callee_str = String::with_capacity(128); - format!("::<{}>", type_list.join(", ")) - }; - let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str); - rewrite_call(context, &callee_str, &args, span, shape) + // Add the method name to get the remaining space. + callee_str.push('.'); + callee_str.push_str(rewrite_ident(context, method_name)); + + // issue 5249: Since function calls wrap their generics already, this + // minimcs the code found in `types::rewrite_generic_args`. + if !types.is_empty() { + callee_str.push_str("::"); + + // An extra offset for the opening '('. + let extra_offset = extra_offset(&callee_str, shape) + 1; + let new_shape = shape.shrink_left(extra_offset, span)?; + + // This will break up the generics if needed. + let generics = + overflow::rewrite_with_angle_brackets(context, "", types.iter(), new_shape, span); + + debug!("generics: {:?}", generics); + + callee_str.push_str(&generics?); + } + + rewrite_call(context, &callee_str, args, span, shape) } } diff --git a/src/overflow.rs b/src/overflow.rs index 19f7b06f8a3..75435f5f88c 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -77,6 +77,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), MetaItemInner(&'a ast::MetaItemInner), SegmentParam(&'a SegmentParam<'a>), @@ -122,6 +123,7 @@ impl<'a> OverflowableItem<'a> { match self { OverflowableItem::Expr(expr) => f(*expr), OverflowableItem::GenericParam(gp) => f(*gp), + OverflowableItem::GenericArg(gp) => f(*gp), OverflowableItem::MacroArg(macro_arg) => f(*macro_arg), OverflowableItem::MetaItemInner(nmi) => f(*nmi), OverflowableItem::SegmentParam(sp) => f(*sp), @@ -259,6 +261,7 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types { impl_into_overflowable_item_for_ast_node!( Expr, GenericParam, + GenericArg, MetaItemInner, FieldDef, Ty, 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) + } +} From 6de666d4813473c78a31158c4e2a10e68b4cdd36 Mon Sep 17 00:00:00 2001 From: Konnor Andrews Date: Fri, 14 Mar 2025 21:14:01 -0600 Subject: [PATCH 2/2] removed extra use statement --- src/chains.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/chains.rs b/src/chains.rs index 627c7ac9598..d87075e4a8f 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -73,7 +73,6 @@ use crate::rewrite::{ }; use crate::shape::Shape; use crate::source_map::SpanUtils; -use crate::types::SegmentParam; use crate::utils::{ self, extra_offset, filtered_str_fits, first_line_width, last_line_extendable, last_line_width, mk_sp, rewrite_ident, trimmed_last_line_width, wrap_str,