From 9255320f4e70e60d4fa0dae998125c1b3938ff10 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 12 Feb 2025 23:30:49 +0700 Subject: [PATCH 01/16] add change, and testcase --- src/expr.rs | 2 +- src/items.rs | 42 +++++++++++++++++++++++-------- src/types.rs | 2 +- src/utils.rs | 4 +-- tests/source/issue-6470/case-1.rs | 15 +++++++++++ tests/target/issue-6470/case-1.rs | 15 +++++++++++ 6 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 tests/source/issue-6470/case-1.rs create mode 100644 tests/target/issue-6470/case-1.rs diff --git a/src/expr.rs b/src/expr.rs index 8031ab68290..ab681dfeb71 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1786,7 +1786,7 @@ pub(crate) fn wrap_struct_field( } pub(crate) fn struct_lit_field_separator(config: &Config) -> &str { - colon_spaces(config) + colon_spaces(config, false) } pub(crate) fn rewrite_field( diff --git a/src/items.rs b/src/items.rs index 901ad44edab..ed1cbb98a50 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; use std::cmp::{Ordering, max, min}; +use std::backtrace::Backtrace; use regex::Regex; use rustc_ast::visit; @@ -42,8 +43,8 @@ const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility { tokens: None, }; -fn type_annotation_separator(config: &Config) -> &str { - colon_spaces(config) +fn type_annotation_separator(config: &Config, force_space_after_colon: bool) -> &str { + colon_spaces(config, force_space_after_colon) } // Statements of the form @@ -96,7 +97,13 @@ impl Rewrite for ast::Local { let mut infix = String::with_capacity(32); if let Some(ref ty) = self.ty { - let separator = type_annotation_separator(context.config); + + let force_space_after_colon = match ty.clone().into_inner().kind { + ast::TyKind::Path(None, _) => true, + _ => false, + }; + + let separator = type_annotation_separator(context.config, force_space_after_colon); let ty_shape = if pat_str.contains('\n') { shape.with_max_width(context.config) } else { @@ -1890,10 +1897,10 @@ fn rewrite_ty( Ok(result) } -fn type_annotation_spacing(config: &Config) -> (&str, &str) { +fn type_annotation_spacing(config: &Config, force_space_after_colon: bool) -> (&str, &str) { ( if config.space_before_colon() { " " } else { "" }, - if config.space_after_colon() { " " } else { "" }, + if force_space_after_colon || config.space_after_colon() { " " } else { "" }, ) } @@ -1903,7 +1910,7 @@ pub(crate) fn rewrite_struct_field_prefix( ) -> RewriteResult { let vis = format_visibility(context, &field.vis); let safety = format_safety(field.safety); - let type_annotation_spacing = type_annotation_spacing(context.config); + let type_annotation_spacing = type_annotation_spacing(context.config, false); Ok(match field.ident { Some(name) => format!( "{vis}{safety}{}{}:", @@ -1924,6 +1931,10 @@ impl Rewrite for ast::FieldDef { } } +use std::sync::atomic::{AtomicUsize}; + +static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); + pub(crate) fn rewrite_struct_field( context: &RewriteContext<'_>, field: &ast::FieldDef, @@ -1939,7 +1950,11 @@ pub(crate) fn rewrite_struct_field( return Ok(context.snippet(field.span()).to_owned()); } - let type_annotation_spacing = type_annotation_spacing(context.config); + let force_space_after_colon = match field.ty.clone().into_inner().kind { + ast::TyKind::Path(None, _) => true, + _ => false, + }; + let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); let prefix = rewrite_struct_field_prefix(context, field)?; let attrs_str = field.attrs.rewrite_result(context, shape)?; @@ -2091,7 +2106,14 @@ fn rewrite_static( return None; } - let colon = colon_spaces(context.config); + // if after a semicolon is absolute path declaration (::) need to force + // space after colon, because ::: syntax cannot compile + let force_space_after_colon = match static_parts.ty.kind { + ast::TyKind::Path(None, _) => true, + _ => false, + }; + let colon = colon_spaces(context.config, force_space_after_colon); + let mut prefix = format!( "{}{}{}{} {}{}{}", format_visibility(context, static_parts.vis), @@ -2294,7 +2316,7 @@ impl Rewrite for ast::Param { let (before_comment, after_comment) = get_missing_param_comments(context, self.pat.span, self.ty.span, shape); result.push_str(&before_comment); - result.push_str(colon_spaces(context.config)); + result.push_str(colon_spaces(context.config, false)); result.push_str(&after_comment); let overhead = last_line_width(&result); let max_width = shape @@ -2322,7 +2344,7 @@ impl Rewrite for ast::Param { !has_multiple_attr_lines, )?; result.push_str(&before_comment); - result.push_str(colon_spaces(context.config)); + result.push_str(colon_spaces(context.config, false)); result.push_str(&after_comment); let overhead = last_line_width(&result); let max_width = shape diff --git a/src/types.rs b/src/types.rs index 6180a4dac13..729a78137cd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -435,7 +435,7 @@ where } fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str { - colon_spaces(context.config) + colon_spaces(context.config, false) } // If the return type is multi-lined, then force to use multiple lines for diff --git a/src/utils.rs b/src/utils.rs index ba4a4c045f1..f013a829d28 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -423,9 +423,9 @@ pub(crate) fn filtered_str_fits(snippet: &str, max_width: usize, shape: Shape) - } #[inline] -pub(crate) fn colon_spaces(config: &Config) -> &'static str { +pub(crate) fn colon_spaces(config: &Config, force_space_after_colon: bool) -> &'static str { let before = config.space_before_colon(); - let after = config.space_after_colon(); + let after = force_space_after_colon || config.space_after_colon(); match (before, after) { (true, true) => " : ", (true, false) => " :", diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs new file mode 100644 index 00000000000..e95dec44c16 --- /dev/null +++ b/tests/source/issue-6470/case-1.rs @@ -0,0 +1,15 @@ +// rustfmt-space_after_colon: false + +struct SomeStruct { + some_field: ::some_crate::Thing, +} + +const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +fn main() { + let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let a1: int; + let a2:int; + let a3 :int; + let a4 : int; +} diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs new file mode 100644 index 00000000000..31b4361fac9 --- /dev/null +++ b/tests/target/issue-6470/case-1.rs @@ -0,0 +1,15 @@ +// rustfmt-space_after_colon: false + +struct SomeStruct { + some_field: ::some_crate::Thing, +} + +const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +fn main() { + let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let a1: int; + let a2: int; + let a3: int; + let a4: int; +} From 6befafcfd95e722f73bd8888475b0977147b7feb Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 12 Feb 2025 23:33:58 +0700 Subject: [PATCH 02/16] add change, and testcase --- src/items.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/items.rs b/src/items.rs index ed1cbb98a50..c708c1390d4 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2,7 +2,6 @@ use std::borrow::Cow; use std::cmp::{Ordering, max, min}; -use std::backtrace::Backtrace; use regex::Regex; use rustc_ast::visit; @@ -1931,10 +1930,6 @@ impl Rewrite for ast::FieldDef { } } -use std::sync::atomic::{AtomicUsize}; - -static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); - pub(crate) fn rewrite_struct_field( context: &RewriteContext<'_>, field: &ast::FieldDef, From 2c8d6bdc5b11c45e996ca6b069e848e7da380bae Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Thu, 13 Feb 2025 09:22:22 +0700 Subject: [PATCH 03/16] make function to check absolute path declaration and fix logic error regarding to it --- src/items.rs | 31 +++++++++++++++++-------------- tests/target/issue-6470/case-1.rs | 8 ++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/items.rs b/src/items.rs index c708c1390d4..41ac1e7247a 100644 --- a/src/items.rs +++ b/src/items.rs @@ -96,13 +96,9 @@ impl Rewrite for ast::Local { let mut infix = String::with_capacity(32); if let Some(ref ty) = self.ty { - - let force_space_after_colon = match ty.clone().into_inner().kind { - ast::TyKind::Path(None, _) => true, - _ => false, - }; - + let force_space_after_colon = is_ty_kind_with_global_decl(&ty.clone().into_inner().kind); let separator = type_annotation_separator(context.config, force_space_after_colon); + let ty_shape = if pat_str.contains('\n') { shape.with_max_width(context.config) } else { @@ -1945,10 +1941,7 @@ pub(crate) fn rewrite_struct_field( return Ok(context.snippet(field.span()).to_owned()); } - let force_space_after_colon = match field.ty.clone().into_inner().kind { - ast::TyKind::Path(None, _) => true, - _ => false, - }; + let force_space_after_colon = is_ty_kind_with_global_decl(&field.ty.clone().into_inner().kind); let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); let prefix = rewrite_struct_field_prefix(context, field)?; @@ -2088,6 +2081,19 @@ impl<'a> StaticParts<'a> { } } +fn is_ty_kind_with_global_decl(ty_kind: &ast::TyKind) -> bool { + match ty_kind { + ast::TyKind::Path(None, ast_path) => { + let segments = &ast_path.segments; + match segments.first() { + Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, + None => false, + } + }, + _ => false, + } +} + fn rewrite_static( context: &RewriteContext<'_>, static_parts: &StaticParts<'_>, @@ -2103,10 +2109,7 @@ fn rewrite_static( // if after a semicolon is absolute path declaration (::) need to force // space after colon, because ::: syntax cannot compile - let force_space_after_colon = match static_parts.ty.kind { - ast::TyKind::Path(None, _) => true, - _ => false, - }; + let force_space_after_colon = is_ty_kind_with_global_decl(&static_parts.ty.kind); let colon = colon_spaces(context.config, force_space_after_colon); let mut prefix = format!( diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs index 31b4361fac9..58fc23cb7fb 100644 --- a/tests/target/issue-6470/case-1.rs +++ b/tests/target/issue-6470/case-1.rs @@ -8,8 +8,8 @@ const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); fn main() { let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let a1: int; - let a2: int; - let a3: int; - let a4: int; + let a1:int; + let a2:int; + let a3:int; + let a4:int; } From e8ec19f6fa2e898fd812c3e85e5a23e8a78da629 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Thu, 13 Feb 2025 09:39:59 +0700 Subject: [PATCH 04/16] add more testcase and fix linter in code --- src/items.rs | 18 ++++++++++++------ tests/source/issue-6470/case-2.rs | 15 +++++++++++++++ tests/target/issue-6470/case-2.rs | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 tests/source/issue-6470/case-2.rs create mode 100644 tests/target/issue-6470/case-2.rs diff --git a/src/items.rs b/src/items.rs index 41ac1e7247a..44441d2e527 100644 --- a/src/items.rs +++ b/src/items.rs @@ -96,7 +96,8 @@ impl Rewrite for ast::Local { let mut infix = String::with_capacity(32); if let Some(ref ty) = self.ty { - let force_space_after_colon = is_ty_kind_with_global_decl(&ty.clone().into_inner().kind); + let force_space_after_colon = + is_ty_kind_with_absolute_decl(&ty.clone().into_inner().kind); let separator = type_annotation_separator(context.config, force_space_after_colon); let ty_shape = if pat_str.contains('\n') { @@ -1895,7 +1896,11 @@ fn rewrite_ty( fn type_annotation_spacing(config: &Config, force_space_after_colon: bool) -> (&str, &str) { ( if config.space_before_colon() { " " } else { "" }, - if force_space_after_colon || config.space_after_colon() { " " } else { "" }, + if force_space_after_colon || config.space_after_colon() { + " " + } else { + "" + }, ) } @@ -1941,7 +1946,8 @@ pub(crate) fn rewrite_struct_field( return Ok(context.snippet(field.span()).to_owned()); } - let force_space_after_colon = is_ty_kind_with_global_decl(&field.ty.clone().into_inner().kind); + let force_space_after_colon = + is_ty_kind_with_absolute_decl(&field.ty.clone().into_inner().kind); let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); let prefix = rewrite_struct_field_prefix(context, field)?; @@ -2081,7 +2087,7 @@ impl<'a> StaticParts<'a> { } } -fn is_ty_kind_with_global_decl(ty_kind: &ast::TyKind) -> bool { +fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { match ty_kind { ast::TyKind::Path(None, ast_path) => { let segments = &ast_path.segments; @@ -2089,7 +2095,7 @@ fn is_ty_kind_with_global_decl(ty_kind: &ast::TyKind) -> bool { Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, None => false, } - }, + } _ => false, } } @@ -2109,7 +2115,7 @@ fn rewrite_static( // if after a semicolon is absolute path declaration (::) need to force // space after colon, because ::: syntax cannot compile - let force_space_after_colon = is_ty_kind_with_global_decl(&static_parts.ty.kind); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&static_parts.ty.kind); let colon = colon_spaces(context.config, force_space_after_colon); let mut prefix = format!( diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs new file mode 100644 index 00000000000..7b9972212f8 --- /dev/null +++ b/tests/source/issue-6470/case-2.rs @@ -0,0 +1,15 @@ +// rustfmt-space_after_colon: true + +struct SomeStruct { + some_field: ::some_crate::Thing, +} + +const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +fn main() { + let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let a1: int; + let a2:int; + let a3 :int; + let a4 : int; +} diff --git a/tests/target/issue-6470/case-2.rs b/tests/target/issue-6470/case-2.rs new file mode 100644 index 00000000000..7d6d98b0325 --- /dev/null +++ b/tests/target/issue-6470/case-2.rs @@ -0,0 +1,15 @@ +// rustfmt-space_after_colon: true + +struct SomeStruct { + some_field: ::some_crate::Thing, +} + +const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +fn main() { + let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let a1: int; + let a2: int; + let a3: int; + let a4: int; +} From cb0c596a324a00c02360e8b293f46a1d990ffba8 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Thu, 13 Feb 2025 13:01:57 +0700 Subject: [PATCH 05/16] add more testcase --- tests/source/issue-6470/case-1.rs | 43 ++++++++++++++++++++++++++----- tests/source/issue-6470/case-2.rs | 43 ++++++++++++++++++++++++++----- tests/target/issue-6470/case-1.rs | 43 ++++++++++++++++++++++++++----- tests/target/issue-6470/case-2.rs | 43 ++++++++++++++++++++++++++----- 4 files changed, 144 insertions(+), 28 deletions(-) diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs index e95dec44c16..faa97871671 100644 --- a/tests/source/issue-6470/case-1.rs +++ b/tests/source/issue-6470/case-1.rs @@ -1,15 +1,44 @@ // rustfmt-space_after_colon: false struct SomeStruct { - some_field: ::some_crate::Thing, + field1: ::some_crate::Thing, + field2 : ::some_crate::Thing, + + field3:some_crate::Thing, + field4 :some_crate::Thing, + field5: some_crate::Thing, + field6 : some_crate::Thing, + + field7:i32, + field8 :i32, + field9: i32, + field10 : i32, } -const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +const THING3: some_crate::SomeType = some_crate::SomeType::default(); +const THING4 :some_crate::SomeType = some_crate::SomeType::default(); +const THING5: some_crate::SomeType = some_crate::SomeType::default(); +const THING6 : some_crate::SomeType = some_crate::SomeType::default(); + +const THING7: i32 = 0; +const THING8 :i32 = 0; +const THING9: i32 = 0; +const THING10 : i32 = 0; fn main() { - let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let a1: int; - let a2:int; - let a3 :int; - let a4 : int; + let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + + let x3:some_crate::SomeType = ::some_crate::SomeType::default(); + let x4 : some_crate::SomeType = ::some_crate::SomeType::default(); + let x5: some_crate::SomeType = ::some_crate::SomeType::default(); + let x6 : some_crate::SomeType = ::some_crate::SomeType::default(); + + let x7: i32 = 0; + let x8 :i32 = 0; + let x9: i32 = 0; + let x10 : i32 = 0; } diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index 7b9972212f8..ba3d47bfed1 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -1,15 +1,44 @@ // rustfmt-space_after_colon: true struct SomeStruct { - some_field: ::some_crate::Thing, + field1: ::some_crate::Thing, + field2 : ::some_crate::Thing, + + field3:some_crate::Thing, + field4 :some_crate::Thing, + field5: some_crate::Thing, + field6 : some_crate::Thing, + + field7:i32, + field8 :i32, + field9: i32, + field10 : i32, } -const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +const THING3: some_crate::SomeType = some_crate::SomeType::default(); +const THING4 :some_crate::SomeType = some_crate::SomeType::default(); +const THING5: some_crate::SomeType = some_crate::SomeType::default(); +const THING6 : some_crate::SomeType = some_crate::SomeType::default(); + +const THING7: i32 = 0; +const THING8 :i32 = 0; +const THING9: i32 = 0; +const THING10 : i32 = 0; fn main() { - let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let a1: int; - let a2:int; - let a3 :int; - let a4 : int; + let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + + let x3:some_crate::SomeType = ::some_crate::SomeType::default(); + let x4 : some_crate::SomeType = ::some_crate::SomeType::default(); + let x5: some_crate::SomeType = ::some_crate::SomeType::default(); + let x6 : some_crate::SomeType = ::some_crate::SomeType::default(); + + let x7: i32 = 0; + let x8 :i32 = 0; + let x9: i32 = 0; + let x10 : i32 = 0; } diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs index 58fc23cb7fb..5c13720adc4 100644 --- a/tests/target/issue-6470/case-1.rs +++ b/tests/target/issue-6470/case-1.rs @@ -1,15 +1,44 @@ // rustfmt-space_after_colon: false struct SomeStruct { - some_field: ::some_crate::Thing, + field1: ::some_crate::Thing, + field2: ::some_crate::Thing, + + field3:some_crate::Thing, + field4:some_crate::Thing, + field5:some_crate::Thing, + field6:some_crate::Thing, + + field7:i32, + field8:i32, + field9:i32, + field10:i32, } -const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +const THING3:some_crate::SomeType = some_crate::SomeType::default(); +const THING4:some_crate::SomeType = some_crate::SomeType::default(); +const THING5:some_crate::SomeType = some_crate::SomeType::default(); +const THING6:some_crate::SomeType = some_crate::SomeType::default(); + +const THING7:i32 = 0; +const THING8:i32 = 0; +const THING9:i32 = 0; +const THING10:i32 = 0; fn main() { - let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let a1:int; - let a2:int; - let a3:int; - let a4:int; + let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + + let x3:some_crate::SomeType = ::some_crate::SomeType::default(); + let x4:some_crate::SomeType = ::some_crate::SomeType::default(); + let x5:some_crate::SomeType = ::some_crate::SomeType::default(); + let x6:some_crate::SomeType = ::some_crate::SomeType::default(); + + let x7:i32 = 0; + let x8:i32 = 0; + let x9:i32 = 0; + let x10:i32 = 0; } diff --git a/tests/target/issue-6470/case-2.rs b/tests/target/issue-6470/case-2.rs index 7d6d98b0325..033e707ed5f 100644 --- a/tests/target/issue-6470/case-2.rs +++ b/tests/target/issue-6470/case-2.rs @@ -1,15 +1,44 @@ // rustfmt-space_after_colon: true struct SomeStruct { - some_field: ::some_crate::Thing, + field1: ::some_crate::Thing, + field2: ::some_crate::Thing, + + field3: some_crate::Thing, + field4: some_crate::Thing, + field5: some_crate::Thing, + field6: some_crate::Thing, + + field7: i32, + field8: i32, + field9: i32, + field10: i32, } -const THING: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +const THING3: some_crate::SomeType = some_crate::SomeType::default(); +const THING4: some_crate::SomeType = some_crate::SomeType::default(); +const THING5: some_crate::SomeType = some_crate::SomeType::default(); +const THING6: some_crate::SomeType = some_crate::SomeType::default(); + +const THING7: i32 = 0; +const THING8: i32 = 0; +const THING9: i32 = 0; +const THING10: i32 = 0; fn main() { - let x: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let a1: int; - let a2: int; - let a3: int; - let a4: int; + let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + + let x3: some_crate::SomeType = ::some_crate::SomeType::default(); + let x4: some_crate::SomeType = ::some_crate::SomeType::default(); + let x5: some_crate::SomeType = ::some_crate::SomeType::default(); + let x6: some_crate::SomeType = ::some_crate::SomeType::default(); + + let x7: i32 = 0; + let x8: i32 = 0; + let x9: i32 = 0; + let x10: i32 = 0; } From 32284e05a490a32b7d0abe9f6c660f5756b6ad4f Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Thu, 13 Feb 2025 13:04:15 +0700 Subject: [PATCH 06/16] add more testcase --- tests/source/issue-6470/case-1.rs | 4 ++-- tests/source/issue-6470/case-2.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs index faa97871671..900e6f052b1 100644 --- a/tests/source/issue-6470/case-1.rs +++ b/tests/source/issue-6470/case-1.rs @@ -16,7 +16,7 @@ struct SomeStruct { } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); -const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); const THING3: some_crate::SomeType = some_crate::SomeType::default(); const THING4 :some_crate::SomeType = some_crate::SomeType::default(); @@ -30,7 +30,7 @@ const THING10 : i32 = 0; fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); let x3:some_crate::SomeType = ::some_crate::SomeType::default(); let x4 : some_crate::SomeType = ::some_crate::SomeType::default(); diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index ba3d47bfed1..4e0bbfbaa6b 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -16,7 +16,7 @@ struct SomeStruct { } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); -const THING2: ::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); const THING3: some_crate::SomeType = some_crate::SomeType::default(); const THING4 :some_crate::SomeType = some_crate::SomeType::default(); @@ -30,7 +30,7 @@ const THING10 : i32 = 0; fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); - let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); let x3:some_crate::SomeType = ::some_crate::SomeType::default(); let x4 : some_crate::SomeType = ::some_crate::SomeType::default(); From 62230c1c60072c563e2c24db3cd84812b11e70f0 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Mon, 17 Feb 2025 21:15:14 +0700 Subject: [PATCH 07/16] add more testcase --- src/expr.rs | 37 ++++++-- src/items.rs | 15 +-- src/utils.rs | 20 +++- tests/source/issue-6470/case-1.rs | 147 ++++++++++++++++++++++++++++++ tests/source/issue-6470/case-2.rs | 75 +++++++++++++++ tests/target/issue-6470/case-1.rs | 72 +++++++++++++++ tests/target/issue-6470/case-2.rs | 68 ++++++++++++++ 7 files changed, 409 insertions(+), 25 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 4b849515985..cc9d5928ade 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -6,7 +6,7 @@ use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token}; use rustc_span::{BytePos, Span}; use tracing::debug; - +use tracing::field::debug; use crate::chains::rewrite_chain; use crate::closures; use crate::comment::{ @@ -30,11 +30,7 @@ use crate::spanned::Spanned; use crate::stmt; use crate::string::{StringFormat, rewrite_string}; use crate::types::{PathContext, rewrite_path}; -use crate::utils::{ - colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, - inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes, - semicolon_for_expr, unicode_str_width, wrap_str, -}; +use crate::utils::{colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr, unicode_str_width, wrap_str}; use crate::vertical::rewrite_with_alignment; use crate::visitor::FmtVisitor; @@ -1880,8 +1876,27 @@ pub(crate) fn wrap_struct_field( } } -pub(crate) fn struct_lit_field_separator(config: &Config) -> &str { - colon_spaces(config, false) +pub(crate) fn struct_lit_field_separator(config: &Config, force_space_after_colon: bool) -> &str { + colon_spaces(config, force_space_after_colon) +} + +fn extract_ast_path_from_expr(expr: &ast::Expr) -> Option<&ast::Path> { + match &expr.kind { + ast::ExprKind::Call(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), + ast::ExprKind::MethodCall(box_method_call, ..) => extract_ast_path_from_expr(&*box_method_call.receiver), + ast::ExprKind::Binary(_, left_expr, ..) => extract_ast_path_from_expr(&*left_expr), + ast::ExprKind::Cast(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), + ast::ExprKind::Type(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), + ast::ExprKind::Field(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), + ast::ExprKind::Index(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), + ast::ExprKind::Range(Some(start_expr), ..) => extract_ast_path_from_expr(&*start_expr), + ast::ExprKind::Path(_, path, ..) => Some(&path), + ast::ExprKind::MacCall(mac, ..) => Some(&(*mac).path), + ast::ExprKind::Struct(ptr_struct_expr, ..) => { + Some(&(*ptr_struct_expr).path) + } + _ => None, + } } pub(crate) fn rewrite_field( @@ -1901,7 +1916,11 @@ pub(crate) fn rewrite_field( if field.is_shorthand { Ok(attrs_str + name) } else { - let mut separator = String::from(struct_lit_field_separator(context.config)); + let force_space_after_colon = match extract_ast_path_from_expr(&field.expr) { + Some(path) => is_absolute_decl_path(path), + _ => false, + }; + let mut separator = String::from(struct_lit_field_separator(context.config, force_space_after_colon)); for _ in 0..prefix_max_width.saturating_sub(name.len()) { separator.push(' '); } diff --git a/src/items.rs b/src/items.rs index 44441d2e527..16d45d183fb 100644 --- a/src/items.rs +++ b/src/items.rs @@ -97,7 +97,7 @@ impl Rewrite for ast::Local { if let Some(ref ty) = self.ty { let force_space_after_colon = - is_ty_kind_with_absolute_decl(&ty.clone().into_inner().kind); + is_ty_kind_with_absolute_decl(&(*ty).kind); let separator = type_annotation_separator(context.config, force_space_after_colon); let ty_shape = if pat_str.contains('\n') { @@ -2087,19 +2087,6 @@ impl<'a> StaticParts<'a> { } } -fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { - match ty_kind { - ast::TyKind::Path(None, ast_path) => { - let segments = &ast_path.segments; - match segments.first() { - Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, - None => false, - } - } - _ => false, - } -} - fn rewrite_static( context: &RewriteContext<'_>, static_parts: &StaticParts<'_>, diff --git a/src/utils.rs b/src/utils.rs index f013a829d28..65b7a78cd78 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,10 +6,10 @@ use rustc_ast::ast::{ }; use rustc_ast::ptr; use rustc_ast_pretty::pprust; -use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol}; +use rustc_span::{sym, symbol, BytePos, LocalExpnId, Span, Symbol, SyntaxContext}; use unicode_width::UnicodeWidthStr; -use crate::comment::{CharClasses, FullCodeCharKind, LineClasses, filter_normal_code}; +use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses}; use crate::config::{Config, StyleEdition}; use crate::rewrite::RewriteContext; use crate::shape::{Indent, Shape}; @@ -715,3 +715,19 @@ mod test { ); } } + + +pub fn is_absolute_decl_path(path :&ast::Path) -> bool { + let segments = &path.segments; + match segments.first() { + Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, + None => false, + } +} + +pub fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { + match ty_kind { + ast::TyKind::Path(None, ast_path) => is_absolute_decl_path(ast_path), + _ => false, + } +} \ No newline at end of file diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs index 900e6f052b1..7a911ee7e17 100644 --- a/tests/source/issue-6470/case-1.rs +++ b/tests/source/issue-6470/case-1.rs @@ -13,6 +13,11 @@ struct SomeStruct { field8 :i32, field9: i32, field10 : i32, + + field11:&::some_crate::Thing, + field12: &::some_crate::Thing, + field13 :&::some_crate::Thing, + field14 : &::some_crate::Thing, } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -28,6 +33,31 @@ const THING8 :i32 = 0; const THING9: i32 = 0; const THING10 : i32 = 0; +const THING11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING13 :&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + + + +static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC3: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC4 :some_crate::SomeType = some_crate::SomeType::default(); +static STATIC5: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC6 : some_crate::SomeType = some_crate::SomeType::default(); + +static STATIC7: i32 = 0; +static STATIC8 :i32 = 0; +static STATIC9: i32 = 0; +static STATIC10 : i32 = 0; + +static STATIC11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC13 :&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -41,4 +71,121 @@ fn main() { let x8 :i32 = 0; let x9: i32 = 0; let x10 : i32 = 0; + + let x11:&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x12 :&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x13: &::some_crate::SomeType = ::some_crate::SomeType::default(); + let x14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + + + let y_call = SomeStruct { + field1: ::some_crate::Thing::default(), + field2 : ::some_crate::Thing::default(), + + field3:some_crate::Thing::default(), + field4 :some_crate::Thing::default(), + field5: some_crate::Thing::default(), + field6 : some_crate::Thing::default(), + + field7:12, + field8 :12, + field9: 12, + field10 : 12, + + field11:&::some_crate::Thing::default(), + field12: &::some_crate::Thing::default(), + field13 :&::some_crate::Thing::default(), + field14 : &::some_crate::Thing::default(), + }; + + let y_method_call = SomeStruct { + field1: ::some_crate::Thing::Default.call(), + field2 : ::some_crate::Thing::Default.call(), + + ..y_call + }; + + let y_binary = SomeStruct { + field1: ::some_crate::Thing::Default+ 12, + field2 : ::some_crate::Thing::Default + 12, + + ..y_call + }; + + let y_cast = SomeStruct { + field1: ::some_crate::Thing::Default as i32, + field2 : ::some_crate::Thing::Default as i32, + + ..y_call + }; + + let y_type = SomeStruct { + field7: ::some_crate::Thing::Default, + field8 : ::some_crate::Thing::Default, + + ..y_call + }; + + let y_field = SomeStruct { + field1: ::some_crate::Thing::Default.some_field, + field2 : ::some_crate::Thing::Default.some_field, + + ..y_call + }; + + let y_index = SomeStruct { + field1: ::some_crate::Thing::Default[0], + field2 : ::some_crate::Thing::Default[0], + + ..y_call + }; + + let y_range = SomeStruct { + field1: ::some_crate::Thing::DefaultStart..12, + field2 : ::some_crate::Thing::DefaultStart..12, + + ..y_call + }; + + let y_path = SomeStruct { + field1: ::some_crate::Thing::Default, + field2 : ::some_crate::Thing::Default, + + ..y_call + }; + + let y_mac_call = SomeStruct { + field1: ::some_crate::macr!(), + field2 : ::some_crate::macr!(), + + ..y_call + }; + + let y_struct = SomeStruct { + field1: ::some_crate::Thing::SomeStruct{ + fieldA1: 123, + fieldA2: 123, + }, + field2 : ::some_crate::Thing::SomeStruct{ + fieldA1: 123, + fieldA2: 123, + }, + + ..y_call + }; } + +fn func1(x: ::some_crate::SomeType) {} +fn func2(x : ::some_crate::SomeType) {} +fn func3(x:some_crate::SomeType) {} +fn func4(x :some_crate::SomeType) {} +fn func5(x: some_crate::SomeType) {} +fn func6(x : some_crate::SomeType) {} +fn func7(x:i32) {} +fn func8(x: i32) {} +fn func9(x :i32) {} +fn func10(x : i32) {} +fn func11(x:&::some_crate::SomeType) {} +fn func12(x :&::some_crate::SomeType) {} +fn func13(x: &::some_crate::SomeType) {} +fn func14(x : &::some_crate::SomeType) {} diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index 4e0bbfbaa6b..5ae5238c483 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -3,6 +3,8 @@ struct SomeStruct { field1: ::some_crate::Thing, field2 : ::some_crate::Thing, + field1_enum: ::some_crate::Thing, + field2_enum : ::some_crate::Thing, field3:some_crate::Thing, field4 :some_crate::Thing, @@ -13,6 +15,11 @@ struct SomeStruct { field8 :i32, field9: i32, field10 : i32, + + field11:&::some_crate::Thing, + field12: &::some_crate::Thing, + field13 :&::some_crate::Thing, + field14 : &::some_crate::Thing, } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -28,6 +35,31 @@ const THING8 :i32 = 0; const THING9: i32 = 0; const THING10 : i32 = 0; +const THING11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING13 :&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + + + +static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC3: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC4 :some_crate::SomeType = some_crate::SomeType::default(); +static STATIC5: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC6 : some_crate::SomeType = some_crate::SomeType::default(); + +static STATIC7: i32 = 0; +static STATIC8 :i32 = 0; +static STATIC9: i32 = 0; +static STATIC10 : i32 = 0; + +static STATIC11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC13 :&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -41,4 +73,47 @@ fn main() { let x8 :i32 = 0; let x9: i32 = 0; let x10 : i32 = 0; + + let x11:&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x12 :&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x13: &::some_crate::SomeType = ::some_crate::SomeType::default(); + let x14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); + + let y = SomeStruct { + field1: ::some_crate::Thing::default(), + field2 : ::some_crate::Thing::default(), + field1_enum: ::some_crate::Thing::Enum1, + field2_enum : ::some_crate::Thing::Enum1, + + + field3:some_crate::Thing::default(), + field4 :some_crate::Thing::default(), + field5: some_crate::Thing::default(), + field6 : some_crate::Thing::default(), + + field7:12, + field8 :12, + field9: 12, + field10 : 12, + + field11:&::some_crate::Thing::default(), + field12: &::some_crate::Thing::default(), + field13 :&::some_crate::Thing::default(), + field14 : &::some_crate::Thing::default(), + }; } + +fn func1(x: ::some_crate::SomeType) {} +fn func2(x : ::some_crate::SomeType) {} +fn func3(x:some_crate::SomeType) {} +fn func4(x :some_crate::SomeType) {} +fn func5(x: some_crate::SomeType) {} +fn func6(x : some_crate::SomeType) {} +fn func7(x:i32) {} +fn func8(x: i32) {} +fn func9(x :i32) {} +fn func10(x : i32) {} +fn func11(x:&::some_crate::SomeType) {} +fn func12(x :&::some_crate::SomeType) {} +fn func13(x: &::some_crate::SomeType) {} +fn func14(x : &::some_crate::SomeType) {} diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs index 5c13720adc4..40460a3811f 100644 --- a/tests/target/issue-6470/case-1.rs +++ b/tests/target/issue-6470/case-1.rs @@ -3,6 +3,8 @@ struct SomeStruct { field1: ::some_crate::Thing, field2: ::some_crate::Thing, + field1_enum: ::some_crate::Thing, + field2_enum: ::some_crate::Thing, field3:some_crate::Thing, field4:some_crate::Thing, @@ -13,6 +15,11 @@ struct SomeStruct { field8:i32, field9:i32, field10:i32, + + field11:&::some_crate::Thing, + field12:&::some_crate::Thing, + field13:&::some_crate::Thing, + field14:&::some_crate::Thing, } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -28,6 +35,29 @@ const THING8:i32 = 0; const THING9:i32 = 0; const THING10:i32 = 0; +const THING11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING12:&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING13:&::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING14:&::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC3:some_crate::SomeType = some_crate::SomeType::default(); +static STATIC4:some_crate::SomeType = some_crate::SomeType::default(); +static STATIC5:some_crate::SomeType = some_crate::SomeType::default(); +static STATIC6:some_crate::SomeType = some_crate::SomeType::default(); + +static STATIC7:i32 = 0; +static STATIC8:i32 = 0; +static STATIC9:i32 = 0; +static STATIC10:i32 = 0; + +static STATIC11:&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC12:&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC13:&::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC14:&::some_crate::SomeType = ::some_crate::SomeType::default(); + fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -41,4 +71,46 @@ fn main() { let x8:i32 = 0; let x9:i32 = 0; let x10:i32 = 0; + + let x11:&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x12:&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x13:&::some_crate::SomeType = ::some_crate::SomeType::default(); + let x14:&::some_crate::SomeType = ::some_crate::SomeType::default(); + + let y = SomeStruct { + field1: ::some_crate::Thing::default(), + field2: ::some_crate::Thing::default(), + field1_enum: ::some_crate::Thing::Enum1, + field2_enum: ::some_crate::Thing::Enum1, + + field3:some_crate::Thing::default(), + field4:some_crate::Thing::default(), + field5:some_crate::Thing::default(), + field6:some_crate::Thing::default(), + + field7:12, + field8:12, + field9:12, + field10:12, + + field11:&::some_crate::Thing::default(), + field12:&::some_crate::Thing::default(), + field13:&::some_crate::Thing::default(), + field14:&::some_crate::Thing::default(), + }; } + +fn func1(x:::some_crate::SomeType) {} +fn func2(x:::some_crate::SomeType) {} +fn func3(x:some_crate::SomeType) {} +fn func4(x:some_crate::SomeType) {} +fn func5(x:some_crate::SomeType) {} +fn func6(x:some_crate::SomeType) {} +fn func7(x:i32) {} +fn func8(x:i32) {} +fn func9(x:i32) {} +fn func10(x:i32) {} +fn func11(x:&::some_crate::SomeType) {} +fn func12(x:&::some_crate::SomeType) {} +fn func13(x:&::some_crate::SomeType) {} +fn func14(x:&::some_crate::SomeType) {} diff --git a/tests/target/issue-6470/case-2.rs b/tests/target/issue-6470/case-2.rs index 033e707ed5f..b7d7e526188 100644 --- a/tests/target/issue-6470/case-2.rs +++ b/tests/target/issue-6470/case-2.rs @@ -13,6 +13,11 @@ struct SomeStruct { field8: i32, field9: i32, field10: i32, + + field11: &::some_crate::Thing, + field12: &::some_crate::Thing, + field13: &::some_crate::Thing, + field14: &::some_crate::Thing, } const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -28,6 +33,29 @@ const THING8: i32 = 0; const THING9: i32 = 0; const THING10: i32 = 0; +const THING11: &::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING13: &::some_crate::SomeType = ::some_crate::SomeType::default(); +const THING14: &::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC2: ::some_crate::SomeType = ::some_crate::SomeType::default(); + +static STATIC3: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC4: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC5: some_crate::SomeType = some_crate::SomeType::default(); +static STATIC6: some_crate::SomeType = some_crate::SomeType::default(); + +static STATIC7: i32 = 0; +static STATIC8: i32 = 0; +static STATIC9: i32 = 0; +static STATIC10: i32 = 0; + +static STATIC11: &::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC12: &::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC13: &::some_crate::SomeType = ::some_crate::SomeType::default(); +static STATIC14: &::some_crate::SomeType = ::some_crate::SomeType::default(); + fn main() { let x1: ::some_crate::SomeType = ::some_crate::SomeType::default(); let x2: ::some_crate::SomeType = ::some_crate::SomeType::default(); @@ -41,4 +69,44 @@ fn main() { let x8: i32 = 0; let x9: i32 = 0; let x10: i32 = 0; + + let x11: &::some_crate::SomeType = ::some_crate::SomeType::default(); + let x12: &::some_crate::SomeType = ::some_crate::SomeType::default(); + let x13: &::some_crate::SomeType = ::some_crate::SomeType::default(); + let x14: &::some_crate::SomeType = ::some_crate::SomeType::default(); + + let y = SomeStruct { + field1: ::some_crate::Thing::default(), + field2: ::some_crate::Thing::default(), + + field3: some_crate::Thing::default(), + field4: some_crate::Thing::default(), + field5: some_crate::Thing::default(), + field6: some_crate::Thing::default(), + + field7: 12, + field8: 12, + field9: 12, + field10: 12, + + field11: &::some_crate::Thing::default(), + field12: &::some_crate::Thing::default(), + field13: &::some_crate::Thing::default(), + field14: &::some_crate::Thing::default(), + }; } + +fn func1(x: ::some_crate::SomeType) {} +fn func2(x: ::some_crate::SomeType) {} +fn func3(x: some_crate::SomeType) {} +fn func4(x: some_crate::SomeType) {} +fn func5(x: some_crate::SomeType) {} +fn func6(x: some_crate::SomeType) {} +fn func7(x: i32) {} +fn func8(x: i32) {} +fn func9(x: i32) {} +fn func10(x: i32) {} +fn func11(x: &::some_crate::SomeType) {} +fn func12(x: &::some_crate::SomeType) {} +fn func13(x: &::some_crate::SomeType) {} +fn func14(x: &::some_crate::SomeType) {} From 3b535789c3d4a0971d5b0a23e321568222c82e50 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Mon, 17 Feb 2025 21:15:38 +0700 Subject: [PATCH 08/16] add more testcase --- tests/source/issue-6470/case-2.rs | 84 ++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 6 deletions(-) diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index 5ae5238c483..213118e7ed2 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -3,8 +3,6 @@ struct SomeStruct { field1: ::some_crate::Thing, field2 : ::some_crate::Thing, - field1_enum: ::some_crate::Thing, - field2_enum : ::some_crate::Thing, field3:some_crate::Thing, field4 :some_crate::Thing, @@ -79,12 +77,10 @@ fn main() { let x13: &::some_crate::SomeType = ::some_crate::SomeType::default(); let x14 : &::some_crate::SomeType = ::some_crate::SomeType::default(); - let y = SomeStruct { + + let y_call = SomeStruct { field1: ::some_crate::Thing::default(), field2 : ::some_crate::Thing::default(), - field1_enum: ::some_crate::Thing::Enum1, - field2_enum : ::some_crate::Thing::Enum1, - field3:some_crate::Thing::default(), field4 :some_crate::Thing::default(), @@ -101,6 +97,82 @@ fn main() { field13 :&::some_crate::Thing::default(), field14 : &::some_crate::Thing::default(), }; + + let y_method_call = SomeStruct { + field1: ::some_crate::Thing::Default.call(), + field2 : ::some_crate::Thing::Default.call(), + + ..y_call + }; + + let y_binary = SomeStruct { + field1: ::some_crate::Thing::Default+ 12, + field2 : ::some_crate::Thing::Default + 12, + + ..y_call + }; + + let y_cast = SomeStruct { + field1: ::some_crate::Thing::Default as i32, + field2 : ::some_crate::Thing::Default as i32, + + ..y_call + }; + + let y_type = SomeStruct { + field7: ::some_crate::Thing::Default, + field8 : ::some_crate::Thing::Default, + + ..y_call + }; + + let y_field = SomeStruct { + field1: ::some_crate::Thing::Default.some_field, + field2 : ::some_crate::Thing::Default.some_field, + + ..y_call + }; + + let y_index = SomeStruct { + field1: ::some_crate::Thing::Default[0], + field2 : ::some_crate::Thing::Default[0], + + ..y_call + }; + + let y_range = SomeStruct { + field1: ::some_crate::Thing::DefaultStart..12, + field2 : ::some_crate::Thing::DefaultStart..12, + + ..y_call + }; + + let y_path = SomeStruct { + field1: ::some_crate::Thing::Default, + field2 : ::some_crate::Thing::Default, + + ..y_call + }; + + let y_mac_call = SomeStruct { + field1: ::some_crate::macr!(), + field2 : ::some_crate::macr!(), + + ..y_call + }; + + let y_struct = SomeStruct { + field1: ::some_crate::Thing::SomeStruct{ + fieldA1: 123, + fieldA2: 123, + }, + field2 : ::some_crate::Thing::SomeStruct{ + fieldA1: 123, + fieldA2: 123, + }, + + ..y_call + }; } fn func1(x: ::some_crate::SomeType) {} From 471dcc5664de2cf14a1003a2226917e82c578a27 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Mon, 17 Feb 2025 21:18:13 +0700 Subject: [PATCH 09/16] undo reformat --- src/expr.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index cc9d5928ade..60f0987b828 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -6,7 +6,6 @@ use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token}; use rustc_span::{BytePos, Span}; use tracing::debug; -use tracing::field::debug; use crate::chains::rewrite_chain; use crate::closures; use crate::comment::{ @@ -30,7 +29,11 @@ use crate::spanned::Spanned; use crate::stmt; use crate::string::{StringFormat, rewrite_string}; use crate::types::{PathContext, rewrite_path}; -use crate::utils::{colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr, unicode_str_width, wrap_str}; +use crate::utils::{ + colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, + inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, outer_attributes, + semicolon_for_expr, unicode_str_width, wrap_str, +}; use crate::vertical::rewrite_with_alignment; use crate::visitor::FmtVisitor; From d5fb3ae7fbc03e3e471bc21a87e9397cd5b8c064 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Mon, 17 Feb 2025 21:19:05 +0700 Subject: [PATCH 10/16] undo reformat --- src/expr.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/expr.rs b/src/expr.rs index 60f0987b828..1a80194bd0f 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -6,6 +6,7 @@ use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token}; use rustc_span::{BytePos, Span}; use tracing::debug; + use crate::chains::rewrite_chain; use crate::closures; use crate::comment::{ From a05422a325f3b0e560d5d5ca21feebe40ee8813e Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Mon, 17 Feb 2025 21:21:50 +0700 Subject: [PATCH 11/16] undo reformat --- src/items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 16d45d183fb..7b0d8f9b733 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1947,7 +1947,7 @@ pub(crate) fn rewrite_struct_field( } let force_space_after_colon = - is_ty_kind_with_absolute_decl(&field.ty.clone().into_inner().kind); + is_ty_kind_with_absolute_decl(&(*field.ty).kind); let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); let prefix = rewrite_struct_field_prefix(context, field)?; From 318a51ff5c047991df59fc3f01d964895da94d96 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 19 Feb 2025 00:09:08 +0700 Subject: [PATCH 12/16] add testcase --- src/items.rs | 10 ++- src/types.rs | 39 ++++++++--- tests/source/issue-6470/case-1.rs | 41 +++++++++++ tests/source/issue-6470/case-2.rs | 41 +++++++++++ tests/target/issue-6470/case-1.rs | 109 ++++++++++++++++++++++++++++-- 5 files changed, 221 insertions(+), 19 deletions(-) diff --git a/src/items.rs b/src/items.rs index 7b0d8f9b733..c0ff9f126da 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1910,7 +1910,9 @@ pub(crate) fn rewrite_struct_field_prefix( ) -> RewriteResult { let vis = format_visibility(context, &field.vis); let safety = format_safety(field.safety); - let type_annotation_spacing = type_annotation_spacing(context.config, false); + let force_space_after_colon = + is_ty_kind_with_absolute_decl(&(*field.ty).kind); + let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); Ok(match field.ident { Some(name) => format!( "{vis}{safety}{}{}:", @@ -2307,7 +2309,8 @@ impl Rewrite for ast::Param { let (before_comment, after_comment) = get_missing_param_comments(context, self.pat.span, self.ty.span, shape); result.push_str(&before_comment); - result.push_str(colon_spaces(context.config, false)); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*self.ty).kind); + result.push_str(colon_spaces(context.config, force_space_after_colon)); result.push_str(&after_comment); let overhead = last_line_width(&result); let max_width = shape @@ -2335,7 +2338,8 @@ impl Rewrite for ast::Param { !has_multiple_attr_lines, )?; result.push_str(&before_comment); - result.push_str(colon_spaces(context.config, false)); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*self.ty).kind); + result.push_str(colon_spaces(context.config, force_space_after_colon)); result.push_str(&after_comment); let overhead = last_line_width(&result); let max_width = shape diff --git a/src/types.rs b/src/types.rs index 729a78137cd..e12388d4d86 100644 --- a/src/types.rs +++ b/src/types.rs @@ -22,10 +22,7 @@ use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, Rew use crate::shape::Shape; use crate::source_map::SpanUtils; use crate::spanned::Spanned; -use crate::utils::{ - colon_spaces, extra_offset, first_line_width, format_extern, format_mutability, - last_line_extendable, last_line_width, mk_sp, rewrite_ident, -}; +use crate::utils::{colon_spaces, extra_offset, first_line_width, format_extern, format_mutability, is_absolute_decl_path, is_ty_kind_with_absolute_decl, last_line_extendable, last_line_width, mk_sp, rewrite_ident}; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub(crate) enum PathContext { @@ -434,8 +431,8 @@ where } } -fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str { - colon_spaces(context.config, false) +fn type_bound_colon(context: &RewriteContext<'_>, force_space_after_colon: bool) -> &'static str { + colon_spaces(context.config, force_space_after_colon) } // If the return type is multi-lined, then force to use multiple lines for @@ -454,6 +451,25 @@ fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveL } } +fn is_bound_starts_with_absolut_decl(bounds: &ast::GenericBounds) -> bool { + if bounds.len() == 0 { + false + } else { + let first_bound = &bounds[0]; + match first_bound { + ast::GenericBound::Trait(poly_trait_ref) => + poly_trait_ref.bound_generic_params.len() == 0 && + poly_trait_ref.modifiers == ast::TraitBoundModifiers{ + constness: ast::BoundConstness::Never, + asyncness: ast::BoundAsyncness::Normal, + polarity: ast::BoundPolarity::Positive, + } && + is_absolute_decl_path(&poly_trait_ref.trait_ref.path), + _ => false, + } + } +} + impl Rewrite for ast::WherePredicate { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { self.rewrite_result(context, shape).ok() @@ -469,12 +485,15 @@ impl Rewrite for ast::WherePredicate { .. }) => { let type_str = bounded_ty.rewrite_result(context, shape)?; - let colon = type_bound_colon(context).trim_end(); + let force_space_after_colon = is_bound_starts_with_absolut_decl(bounds); + is_ty_kind_with_absolute_decl(&(*bounded_ty).kind); + let colon = type_bound_colon(context, force_space_after_colon).trim_end(); let lhs = if let Some(binder_str) = rewrite_bound_params(context, shape, bound_generic_params) { format!("for<{binder_str}> {type_str}{colon}") } else { + debug!("KANCIL {:?} {}", bounds, force_space_after_colon); format!("{type_str}{colon}") }; @@ -565,7 +584,8 @@ fn rewrite_bounded_lifetime( if bounds.is_empty() { Ok(result) } else { - let colon = type_bound_colon(context); + // the code for this point is `x:&'a SomeType`, so, no need to force adding space after colon + let colon = type_bound_colon(context, false); let overhead = last_line_width(&result) + colon.len(); let shape = shape.sub_width(overhead, span)?; let result = format!( @@ -680,7 +700,8 @@ impl Rewrite for ast::GenericParam { }; if !self.bounds.is_empty() { - param.push_str(type_bound_colon(context)); + let force_space_after_colon = is_bound_starts_with_absolut_decl(&self.bounds); + param.push_str(type_bound_colon(context, force_space_after_colon)); param.push_str(&self.bounds.rewrite_result(context, shape)?) } if let ast::GenericParamKind::Type { diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs index 7a911ee7e17..a9859dc0955 100644 --- a/tests/source/issue-6470/case-1.rs +++ b/tests/source/issue-6470/case-1.rs @@ -189,3 +189,44 @@ fn func11(x:&::some_crate::SomeType) {} fn func12(x :&::some_crate::SomeType) {} fn func13(x: &::some_crate::SomeType) {} fn func14(x : &::some_crate::SomeType) {} + +fn print_gen_with_where1(item: T) +where + T: ::some_crate::SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where2(item: T) +where + T:SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where3(item: T) { + println!("{}", item.to_string()); +} + +fn print_gen_with_where4(item: T) { + println!("{}", item.to_string()); +} + + +/// Adds two integers and returns the result. +/// +/// # Parameters +/// - `a`: The first integer to add. +/// - `b`: The second integer to add. +/// +/// # Returns +/// The sum of `a` and `b` as an integer. The return type is `i32`. +/// +/// # Example +/// ``` +/// let result = add(2, 3); +/// assert_eq!(result, 5); +/// ``` +fn add(a: i32, b: i32) -> i32 { + a + b +} \ No newline at end of file diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index 213118e7ed2..523ba34d68b 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -189,3 +189,44 @@ fn func11(x:&::some_crate::SomeType) {} fn func12(x :&::some_crate::SomeType) {} fn func13(x: &::some_crate::SomeType) {} fn func14(x : &::some_crate::SomeType) {} + +fn print_gen_with_where1(item: T) +where + T: ::some_crate::SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where2(item: T) +where + T:SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where3(item: T) { + println!("{}", item.to_string()); +} + +fn print_gen_with_where4(item: T) { + println!("{}", item.to_string()); +} + + +/// Adds two integers and returns the result. +/// +/// # Parameters +/// - `a`: The first integer to add. +/// - `b`: The second integer to add. +/// +/// # Returns +/// The sum of `a` and `b` as an integer. The return type is `i32`. +/// +/// # Example +/// ``` +/// let result = add(2, 3); +/// assert_eq!(result, 5); +/// ``` +fn add(a: i32, b: i32) -> i32 { + a + b +} \ No newline at end of file diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs index 40460a3811f..9f4b5d4236e 100644 --- a/tests/target/issue-6470/case-1.rs +++ b/tests/target/issue-6470/case-1.rs @@ -3,8 +3,6 @@ struct SomeStruct { field1: ::some_crate::Thing, field2: ::some_crate::Thing, - field1_enum: ::some_crate::Thing, - field2_enum: ::some_crate::Thing, field3:some_crate::Thing, field4:some_crate::Thing, @@ -77,11 +75,9 @@ fn main() { let x13:&::some_crate::SomeType = ::some_crate::SomeType::default(); let x14:&::some_crate::SomeType = ::some_crate::SomeType::default(); - let y = SomeStruct { + let y_call = SomeStruct { field1: ::some_crate::Thing::default(), field2: ::some_crate::Thing::default(), - field1_enum: ::some_crate::Thing::Enum1, - field2_enum: ::some_crate::Thing::Enum1, field3:some_crate::Thing::default(), field4:some_crate::Thing::default(), @@ -98,10 +94,86 @@ fn main() { field13:&::some_crate::Thing::default(), field14:&::some_crate::Thing::default(), }; + + let y_method_call = SomeStruct { + field1: ::some_crate::Thing::Default.call(), + field2: ::some_crate::Thing::Default.call(), + + ..y_call + }; + + let y_binary = SomeStruct { + field1: ::some_crate::Thing::Default + 12, + field2: ::some_crate::Thing::Default + 12, + + ..y_call + }; + + let y_cast = SomeStruct { + field1: ::some_crate::Thing::Default as i32, + field2: ::some_crate::Thing::Default as i32, + + ..y_call + }; + + let y_type = SomeStruct { + field7: ::some_crate::Thing::Default, + field8: ::some_crate::Thing::Default, + + ..y_call + }; + + let y_field = SomeStruct { + field1: ::some_crate::Thing::Default.some_field, + field2: ::some_crate::Thing::Default.some_field, + + ..y_call + }; + + let y_index = SomeStruct { + field1: ::some_crate::Thing::Default[0], + field2: ::some_crate::Thing::Default[0], + + ..y_call + }; + + let y_range = SomeStruct { + field1: ::some_crate::Thing::DefaultStart..12, + field2: ::some_crate::Thing::DefaultStart..12, + + ..y_call + }; + + let y_path = SomeStruct { + field1: ::some_crate::Thing::Default, + field2: ::some_crate::Thing::Default, + + ..y_call + }; + + let y_mac_call = SomeStruct { + field1: ::some_crate::macr!(), + field2: ::some_crate::macr!(), + + ..y_call + }; + + let y_struct = SomeStruct { + field1: ::some_crate::Thing::SomeStruct { + fieldA1:123, + fieldA2:123, + }, + field2: ::some_crate::Thing::SomeStruct { + fieldA1:123, + fieldA2:123, + }, + + ..y_call + }; } -fn func1(x:::some_crate::SomeType) {} -fn func2(x:::some_crate::SomeType) {} +fn func1(x: ::some_crate::SomeType) {} +fn func2(x: ::some_crate::SomeType) {} fn func3(x:some_crate::SomeType) {} fn func4(x:some_crate::SomeType) {} fn func5(x:some_crate::SomeType) {} @@ -114,3 +186,26 @@ fn func11(x:&::some_crate::SomeType) {} fn func12(x:&::some_crate::SomeType) {} fn func13(x:&::some_crate::SomeType) {} fn func14(x:&::some_crate::SomeType) {} + +fn print_gen_with_where1(item:T) +where + T: ::some_crate::SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where2(item:T) +where + T: SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where3(item:T) { + println!("{}", item.to_string()); +} + +fn print_gen_with_where4(item:T) { + println!("{}", item.to_string()); +} + From 5b93020f399655e6bd8efd9fe6836ea3b5aea55f Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 19 Feb 2025 00:17:08 +0700 Subject: [PATCH 13/16] add testcase --- src/types.rs | 1 - tests/source/issue-6470/case-1.rs | 30 +--------- tests/source/issue-6470/case-2.rs | 30 +--------- tests/target/issue-6470/case-1.rs | 12 +--- tests/target/issue-6470/case-2.rs | 93 ++++++++++++++++++++++++++++++- 5 files changed, 98 insertions(+), 68 deletions(-) diff --git a/src/types.rs b/src/types.rs index e12388d4d86..7c4b7a606a4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -493,7 +493,6 @@ impl Rewrite for ast::WherePredicate { { format!("for<{binder_str}> {type_str}{colon}") } else { - debug!("KANCIL {:?} {}", bounds, force_space_after_colon); format!("{type_str}{colon}") }; diff --git a/tests/source/issue-6470/case-1.rs b/tests/source/issue-6470/case-1.rs index a9859dc0955..3d711b23a1a 100644 --- a/tests/source/issue-6470/case-1.rs +++ b/tests/source/issue-6470/case-1.rs @@ -197,36 +197,10 @@ where println!("{}", item.to_string()); } -fn print_gen_with_where2(item: T) -where - T:SomeTrait + Clone, -{ +fn print_gen_with_where2(item: T) { println!("{}", item.to_string()); } -fn print_gen_with_where3(item: T) { +fn print_gen_with_where3(item: T) { println!("{}", item.to_string()); } - -fn print_gen_with_where4(item: T) { - println!("{}", item.to_string()); -} - - -/// Adds two integers and returns the result. -/// -/// # Parameters -/// - `a`: The first integer to add. -/// - `b`: The second integer to add. -/// -/// # Returns -/// The sum of `a` and `b` as an integer. The return type is `i32`. -/// -/// # Example -/// ``` -/// let result = add(2, 3); -/// assert_eq!(result, 5); -/// ``` -fn add(a: i32, b: i32) -> i32 { - a + b -} \ No newline at end of file diff --git a/tests/source/issue-6470/case-2.rs b/tests/source/issue-6470/case-2.rs index 523ba34d68b..f7f3c2b069e 100644 --- a/tests/source/issue-6470/case-2.rs +++ b/tests/source/issue-6470/case-2.rs @@ -197,36 +197,10 @@ where println!("{}", item.to_string()); } -fn print_gen_with_where2(item: T) -where - T:SomeTrait + Clone, -{ +fn print_gen_with_where2(item: T) { println!("{}", item.to_string()); } -fn print_gen_with_where3(item: T) { +fn print_gen_with_where3(item: T) { println!("{}", item.to_string()); } - -fn print_gen_with_where4(item: T) { - println!("{}", item.to_string()); -} - - -/// Adds two integers and returns the result. -/// -/// # Parameters -/// - `a`: The first integer to add. -/// - `b`: The second integer to add. -/// -/// # Returns -/// The sum of `a` and `b` as an integer. The return type is `i32`. -/// -/// # Example -/// ``` -/// let result = add(2, 3); -/// assert_eq!(result, 5); -/// ``` -fn add(a: i32, b: i32) -> i32 { - a + b -} \ No newline at end of file diff --git a/tests/target/issue-6470/case-1.rs b/tests/target/issue-6470/case-1.rs index 9f4b5d4236e..b4c4186c56a 100644 --- a/tests/target/issue-6470/case-1.rs +++ b/tests/target/issue-6470/case-1.rs @@ -194,18 +194,10 @@ where println!("{}", item.to_string()); } -fn print_gen_with_where2(item:T) -where - T: SomeTrait + Clone, -{ - println!("{}", item.to_string()); -} - -fn print_gen_with_where3(item:T) { +fn print_gen_with_where2(item:T) { println!("{}", item.to_string()); } -fn print_gen_with_where4(item:T) { +fn print_gen_with_where3(item:T) { println!("{}", item.to_string()); } - diff --git a/tests/target/issue-6470/case-2.rs b/tests/target/issue-6470/case-2.rs index b7d7e526188..a8cb726cdcb 100644 --- a/tests/target/issue-6470/case-2.rs +++ b/tests/target/issue-6470/case-2.rs @@ -75,7 +75,7 @@ fn main() { let x13: &::some_crate::SomeType = ::some_crate::SomeType::default(); let x14: &::some_crate::SomeType = ::some_crate::SomeType::default(); - let y = SomeStruct { + let y_call = SomeStruct { field1: ::some_crate::Thing::default(), field2: ::some_crate::Thing::default(), @@ -94,6 +94,82 @@ fn main() { field13: &::some_crate::Thing::default(), field14: &::some_crate::Thing::default(), }; + + let y_method_call = SomeStruct { + field1: ::some_crate::Thing::Default.call(), + field2: ::some_crate::Thing::Default.call(), + + ..y_call + }; + + let y_binary = SomeStruct { + field1: ::some_crate::Thing::Default + 12, + field2: ::some_crate::Thing::Default + 12, + + ..y_call + }; + + let y_cast = SomeStruct { + field1: ::some_crate::Thing::Default as i32, + field2: ::some_crate::Thing::Default as i32, + + ..y_call + }; + + let y_type = SomeStruct { + field7: ::some_crate::Thing::Default, + field8: ::some_crate::Thing::Default, + + ..y_call + }; + + let y_field = SomeStruct { + field1: ::some_crate::Thing::Default.some_field, + field2: ::some_crate::Thing::Default.some_field, + + ..y_call + }; + + let y_index = SomeStruct { + field1: ::some_crate::Thing::Default[0], + field2: ::some_crate::Thing::Default[0], + + ..y_call + }; + + let y_range = SomeStruct { + field1: ::some_crate::Thing::DefaultStart..12, + field2: ::some_crate::Thing::DefaultStart..12, + + ..y_call + }; + + let y_path = SomeStruct { + field1: ::some_crate::Thing::Default, + field2: ::some_crate::Thing::Default, + + ..y_call + }; + + let y_mac_call = SomeStruct { + field1: ::some_crate::macr!(), + field2: ::some_crate::macr!(), + + ..y_call + }; + + let y_struct = SomeStruct { + field1: ::some_crate::Thing::SomeStruct { + fieldA1: 123, + fieldA2: 123, + }, + field2: ::some_crate::Thing::SomeStruct { + fieldA1: 123, + fieldA2: 123, + }, + + ..y_call + }; } fn func1(x: ::some_crate::SomeType) {} @@ -110,3 +186,18 @@ fn func11(x: &::some_crate::SomeType) {} fn func12(x: &::some_crate::SomeType) {} fn func13(x: &::some_crate::SomeType) {} fn func14(x: &::some_crate::SomeType) {} + +fn print_gen_with_where1(item: T) +where + T: ::some_crate::SomeTrait + Clone, +{ + println!("{}", item.to_string()); +} + +fn print_gen_with_where2(item: T) { + println!("{}", item.to_string()); +} + +fn print_gen_with_where3(item: T) { + println!("{}", item.to_string()); +} From d87b0b20f036abcb13ab98952fbdd997fcfc4251 Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 19 Feb 2025 00:24:21 +0700 Subject: [PATCH 14/16] fix test error --- src/types.rs | 6 +++++- src/utils.rs | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/types.rs b/src/types.rs index 7c4b7a606a4..e4317241b31 100644 --- a/src/types.rs +++ b/src/types.rs @@ -22,7 +22,11 @@ use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, Rew use crate::shape::Shape; use crate::source_map::SpanUtils; use crate::spanned::Spanned; -use crate::utils::{colon_spaces, extra_offset, first_line_width, format_extern, format_mutability, is_absolute_decl_path, is_ty_kind_with_absolute_decl, last_line_extendable, last_line_width, mk_sp, rewrite_ident}; +use crate::utils::{ + colon_spaces, extra_offset, first_line_width, format_extern, format_mutability, + is_absolute_decl_path, is_ty_kind_with_absolute_decl, last_line_extendable, last_line_width, + mk_sp, rewrite_ident, +}; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub(crate) enum PathContext { diff --git a/src/utils.rs b/src/utils.rs index 65b7a78cd78..be949d7f717 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,10 +6,10 @@ use rustc_ast::ast::{ }; use rustc_ast::ptr; use rustc_ast_pretty::pprust; -use rustc_span::{sym, symbol, BytePos, LocalExpnId, Span, Symbol, SyntaxContext}; +use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol}; use unicode_width::UnicodeWidthStr; -use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses}; +use crate::comment::{CharClasses, FullCodeCharKind, LineClasses, filter_normal_code}; use crate::config::{Config, StyleEdition}; use crate::rewrite::RewriteContext; use crate::shape::{Indent, Shape}; @@ -717,7 +717,7 @@ mod test { } -pub fn is_absolute_decl_path(path :&ast::Path) -> bool { +pub fn is_absolute_decl_path(path: &ast::Path) -> bool { let segments = &path.segments; match segments.first() { Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, From 20c6e60de03745e9ac3899944bab1d592b2350db Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 19 Feb 2025 00:44:49 +0700 Subject: [PATCH 15/16] fix test error --- src/expr.rs | 17 ++++++++++------- src/items.rs | 9 +++------ src/types.rs | 21 ++++++++++++--------- src/utils.rs | 3 +-- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 1a80194bd0f..0e58848a8d7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -32,8 +32,8 @@ use crate::string::{StringFormat, rewrite_string}; use crate::types::{PathContext, rewrite_path}; use crate::utils::{ colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, - inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, outer_attributes, - semicolon_for_expr, unicode_str_width, wrap_str, + inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, + outer_attributes, semicolon_for_expr, unicode_str_width, wrap_str, }; use crate::vertical::rewrite_with_alignment; use crate::visitor::FmtVisitor; @@ -1887,7 +1887,9 @@ pub(crate) fn struct_lit_field_separator(config: &Config, force_space_after_colo fn extract_ast_path_from_expr(expr: &ast::Expr) -> Option<&ast::Path> { match &expr.kind { ast::ExprKind::Call(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), - ast::ExprKind::MethodCall(box_method_call, ..) => extract_ast_path_from_expr(&*box_method_call.receiver), + ast::ExprKind::MethodCall(box_method_call, ..) => { + extract_ast_path_from_expr(&*box_method_call.receiver) + } ast::ExprKind::Binary(_, left_expr, ..) => extract_ast_path_from_expr(&*left_expr), ast::ExprKind::Cast(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), ast::ExprKind::Type(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr), @@ -1896,9 +1898,7 @@ fn extract_ast_path_from_expr(expr: &ast::Expr) -> Option<&ast::Path> { ast::ExprKind::Range(Some(start_expr), ..) => extract_ast_path_from_expr(&*start_expr), ast::ExprKind::Path(_, path, ..) => Some(&path), ast::ExprKind::MacCall(mac, ..) => Some(&(*mac).path), - ast::ExprKind::Struct(ptr_struct_expr, ..) => { - Some(&(*ptr_struct_expr).path) - } + ast::ExprKind::Struct(ptr_struct_expr, ..) => Some(&(*ptr_struct_expr).path), _ => None, } } @@ -1924,7 +1924,10 @@ pub(crate) fn rewrite_field( Some(path) => is_absolute_decl_path(path), _ => false, }; - let mut separator = String::from(struct_lit_field_separator(context.config, force_space_after_colon)); + let mut separator = String::from(struct_lit_field_separator( + context.config, + force_space_after_colon, + )); for _ in 0..prefix_max_width.saturating_sub(name.len()) { separator.push(' '); } diff --git a/src/items.rs b/src/items.rs index c0ff9f126da..5a00b5cd6dc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -96,8 +96,7 @@ impl Rewrite for ast::Local { let mut infix = String::with_capacity(32); if let Some(ref ty) = self.ty { - let force_space_after_colon = - is_ty_kind_with_absolute_decl(&(*ty).kind); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*ty).kind); let separator = type_annotation_separator(context.config, force_space_after_colon); let ty_shape = if pat_str.contains('\n') { @@ -1910,8 +1909,7 @@ pub(crate) fn rewrite_struct_field_prefix( ) -> RewriteResult { let vis = format_visibility(context, &field.vis); let safety = format_safety(field.safety); - let force_space_after_colon = - is_ty_kind_with_absolute_decl(&(*field.ty).kind); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*field.ty).kind); let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); Ok(match field.ident { Some(name) => format!( @@ -1948,8 +1946,7 @@ pub(crate) fn rewrite_struct_field( return Ok(context.snippet(field.span()).to_owned()); } - let force_space_after_colon = - is_ty_kind_with_absolute_decl(&(*field.ty).kind); + let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*field.ty).kind); let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon); let prefix = rewrite_struct_field_prefix(context, field)?; diff --git a/src/types.rs b/src/types.rs index e4317241b31..5147fb108f4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -461,14 +461,16 @@ fn is_bound_starts_with_absolut_decl(bounds: &ast::GenericBounds) -> bool { } else { let first_bound = &bounds[0]; match first_bound { - ast::GenericBound::Trait(poly_trait_ref) => - poly_trait_ref.bound_generic_params.len() == 0 && - poly_trait_ref.modifiers == ast::TraitBoundModifiers{ - constness: ast::BoundConstness::Never, - asyncness: ast::BoundAsyncness::Normal, - polarity: ast::BoundPolarity::Positive, - } && - is_absolute_decl_path(&poly_trait_ref.trait_ref.path), + ast::GenericBound::Trait(poly_trait_ref) => { + let plain_trait_modifiers = ast::TraitBoundModifiers { + constness: ast::BoundConstness::Never, + asyncness: ast::BoundAsyncness::Normal, + polarity: ast::BoundPolarity::Positive, + }; + poly_trait_ref.modifiers == plain_trait_modifiers + && poly_trait_ref.bound_generic_params.len() == 0 + && is_absolute_decl_path(&poly_trait_ref.trait_ref.path) + } _ => false, } } @@ -587,7 +589,8 @@ fn rewrite_bounded_lifetime( if bounds.is_empty() { Ok(result) } else { - // the code for this point is `x:&'a SomeType`, so, no need to force adding space after colon + // the code for this point is `x:&'a SomeType`, + // so, no need to force adding space after colon let colon = type_bound_colon(context, false); let overhead = last_line_width(&result) + colon.len(); let shape = shape.sub_width(overhead, span)?; diff --git a/src/utils.rs b/src/utils.rs index be949d7f717..e7e9d8ca867 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -716,7 +716,6 @@ mod test { } } - pub fn is_absolute_decl_path(path: &ast::Path) -> bool { let segments = &path.segments; match segments.first() { @@ -730,4 +729,4 @@ pub fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { ast::TyKind::Path(None, ast_path) => is_absolute_decl_path(ast_path), _ => false, } -} \ No newline at end of file +} From 6b3cdb3a98cb6416b7adc3651a8640da8f4b0b6a Mon Sep 17 00:00:00 2001 From: Febriananda Wida Pramudita Date: Wed, 19 Feb 2025 00:46:54 +0700 Subject: [PATCH 16/16] restrict publicity --- src/utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e7e9d8ca867..19d8346ca69 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -716,7 +716,7 @@ mod test { } } -pub fn is_absolute_decl_path(path: &ast::Path) -> bool { +pub(crate) fn is_absolute_decl_path(path: &ast::Path) -> bool { let segments = &path.segments; match segments.first() { Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot, @@ -724,7 +724,7 @@ pub fn is_absolute_decl_path(path: &ast::Path) -> bool { } } -pub fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { +pub(crate) fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool { match ty_kind { ast::TyKind::Path(None, ast_path) => is_absolute_decl_path(ast_path), _ => false,