Skip to content

Commit

Permalink
add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
FwP-IDN committed Feb 18, 2025
1 parent a05422a commit 318a51f
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}{}{}:",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 30 additions & 9 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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<String> {
self.rewrite_result(context, shape).ok()
Expand All @@ -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}")
};

Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions tests/source/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(item: T)
where
T: ::some_crate::SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where2<T>(item: T)
where
T:SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item: T) {
println!("{}", item.to_string());
}

fn print_gen_with_where4<T: some_crate::SomeTrait + Clone>(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
}
41 changes: 41 additions & 0 deletions tests/source/issue-6470/case-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(item: T)
where
T: ::some_crate::SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where2<T>(item: T)
where
T:SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item: T) {
println!("{}", item.to_string());
}

fn print_gen_with_where4<T: some_crate::SomeTrait + Clone>(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
}
109 changes: 102 additions & 7 deletions tests/target/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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) {}
Expand All @@ -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<T>(item:T)
where
T: ::some_crate::SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where2<T>(item:T)
where
T: SomeTrait + Clone,
{
println!("{}", item.to_string());
}

fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item:T) {
println!("{}", item.to_string());
}

fn print_gen_with_where4<T:some_crate::SomeTrait + Clone>(item:T) {
println!("{}", item.to_string());
}

0 comments on commit 318a51f

Please sign in to comment.