diff --git a/.gitignore b/.gitignore index 71cf88f79e6..2e82235cf5f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ tests/cargo-fmt/**/target .idea/ .vscode/ *~ + +# nvim local dap settings +.nvim-dap.lua diff --git a/rust-toolchain b/rust-toolchain index 7b7081bfe81..2dec200a043 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2024-08-17" +channel = "nightly-2024-08-08" components = ["llvm-tools", "rustc-dev"] diff --git a/src/expr.rs b/src/expr.rs index 138689bc132..25588a3f0f9 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2057,12 +2057,38 @@ fn rewrite_assignment( let lhs_shape = shape.sub_width(operator_str.len() + 1)?; let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str); + let lhs_lines: Vec<&str> = lhs_str.split("\n").collect(); + + let mut rhs_shape = shape.clone(); + + for line in lhs_lines.into_iter().rev() { + let mut indent_width = 0; + let mut first_char = ' '; + for char in line.chars() { + if char != ' ' { + first_char = char; + break; + } else { + indent_width += 1; + } + } + + if first_char != '/' { + let indent = Indent::from_width(&context.config, indent_width); + rhs_shape = Shape::indented(indent, &context.config); + break; + } + } + + println!("config={:?}", context.config.max_width()); + println!("old shape={shape:?}"); + println!("new shape={rhs_shape:?}"); rewrite_assign_rhs( context, lhs_str, rhs, &RhsAssignKind::Expr(&rhs.kind, rhs.span), - shape, + rhs_shape, ) } diff --git a/src/items.rs b/src/items.rs index 35591df0fd8..4b9a8d42875 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1768,10 +1768,27 @@ fn rewrite_ty( if let Some(bounds) = generic_bounds_opt { if !bounds.is_empty() { - // 2 = `: ` - let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?; - let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?; - result.push_str(&type_bounds); + match context.config.style_edition() { + style_edition if style_edition < StyleEdition::Edition2024 => { + // 2 = `: ` + let shape = + Shape::indented(indent, context.config).offset_left(result.len() + 2)?; + let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?; + result.push_str(&type_bounds); + } + _ => { + let shape = + Shape::indented(indent, context.config).offset_left(result.len())?; + result = rewrite_assign_rhs_with( + context, + result.clone() + ":", + bounds, + shape, + &RhsAssignKind::Bounds, + RhsTactics::ForceNextLineWithoutIndent, + )?; + } + } } } diff --git a/src/types.rs b/src/types.rs index f9dafe7d625..4331b57e8e2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1211,7 +1211,7 @@ fn join_bounds_inner( // or the single item is of type `Trait`, // and any of the internal arrays contains more than one item; let retry_with_force_newline = match context.config.style_edition() { - style_edition @ _ if style_edition <= StyleEdition::Edition2021 => { + style_edition if style_edition <= StyleEdition::Edition2021 => { !force_newline && items.len() > 1 && (result.0.contains('\n') || result.0.len() > shape.width) diff --git a/tests/source/issue-189.rs b/tests/source/issue-189.rs new file mode 100644 index 00000000000..607464aff2b --- /dev/null +++ b/tests/source/issue-189.rs @@ -0,0 +1,14 @@ +// rustfmt-style_edition: 2024 + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = Some(ExtraInfo { + parent, + count: count as u16, + children: children.into_boxed_slice(), + }); + } +} diff --git a/tests/source/issue-5892.rs b/tests/source/issue-5892.rs new file mode 100644 index 00000000000..2be61afb5fb --- /dev/null +++ b/tests/source/issue-5892.rs @@ -0,0 +1,33 @@ +// rustfmt-style_edition: 2024 + +type AAAAAAAAAAAAA: + BBBBBBBBBBBBBBB< + CCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDD, + EEEEEEEEEEEEEEEEE, + FFFFFFFFFFFFFFFFF, + GGGGGGGGGGGGGGGGG, + HHHHHHHHHHHHHHHHH, + IIIIIIIIIIIIIIIII, + >; + +type AAAAAAAAAAAAA: + BBBBBBBBBBBBBBB< + CCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDD, + EEEEEEEEEEEEEEEEE, + FFFFFFFFFFFFFFFFF, + GGGGGGGGGGGGGGGGG, + HHHHHHHHHHHHHHHHH, + IIIIIIIIIIIIIIIII, + > + Eq + + PartialEq; + +// previous error: maximum length exceeded +type SomeType: + BBBBBBBBBBBBBBB + + AAAAAAAAAAAAA; + +// previous error: maximum length exceeded +type SomeType: + BBBBBBBBBBBBBBB; diff --git a/tests/target/issue-189.rs b/tests/target/issue-189.rs new file mode 100644 index 00000000000..51c794aba14 --- /dev/null +++ b/tests/target/issue-189.rs @@ -0,0 +1,24 @@ +// rustfmt-style_edition: 2024 + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = Some(ExtraInfo { + parent, + count: count as u16, + children: children.into_boxed_slice(), + }); + } +} + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = + long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; + } +} diff --git a/tests/target/issue-5892.rs b/tests/target/issue-5892.rs new file mode 100644 index 00000000000..2be61afb5fb --- /dev/null +++ b/tests/target/issue-5892.rs @@ -0,0 +1,33 @@ +// rustfmt-style_edition: 2024 + +type AAAAAAAAAAAAA: + BBBBBBBBBBBBBBB< + CCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDD, + EEEEEEEEEEEEEEEEE, + FFFFFFFFFFFFFFFFF, + GGGGGGGGGGGGGGGGG, + HHHHHHHHHHHHHHHHH, + IIIIIIIIIIIIIIIII, + >; + +type AAAAAAAAAAAAA: + BBBBBBBBBBBBBBB< + CCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDD, + EEEEEEEEEEEEEEEEE, + FFFFFFFFFFFFFFFFF, + GGGGGGGGGGGGGGGGG, + HHHHHHHHHHHHHHHHH, + IIIIIIIIIIIIIIIII, + > + Eq + + PartialEq; + +// previous error: maximum length exceeded +type SomeType: + BBBBBBBBBBBBBBB + + AAAAAAAAAAAAA; + +// previous error: maximum length exceeded +type SomeType: + BBBBBBBBBBBBBBB;