Skip to content

Commit 318a51f

Browse files
committed
add testcase
1 parent a05422a commit 318a51f

File tree

5 files changed

+221
-19
lines changed

5 files changed

+221
-19
lines changed

src/items.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,9 @@ pub(crate) fn rewrite_struct_field_prefix(
19101910
) -> RewriteResult {
19111911
let vis = format_visibility(context, &field.vis);
19121912
let safety = format_safety(field.safety);
1913-
let type_annotation_spacing = type_annotation_spacing(context.config, false);
1913+
let force_space_after_colon =
1914+
is_ty_kind_with_absolute_decl(&(*field.ty).kind);
1915+
let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon);
19141916
Ok(match field.ident {
19151917
Some(name) => format!(
19161918
"{vis}{safety}{}{}:",
@@ -2307,7 +2309,8 @@ impl Rewrite for ast::Param {
23072309
let (before_comment, after_comment) =
23082310
get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
23092311
result.push_str(&before_comment);
2310-
result.push_str(colon_spaces(context.config, false));
2312+
let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*self.ty).kind);
2313+
result.push_str(colon_spaces(context.config, force_space_after_colon));
23112314
result.push_str(&after_comment);
23122315
let overhead = last_line_width(&result);
23132316
let max_width = shape
@@ -2335,7 +2338,8 @@ impl Rewrite for ast::Param {
23352338
!has_multiple_attr_lines,
23362339
)?;
23372340
result.push_str(&before_comment);
2338-
result.push_str(colon_spaces(context.config, false));
2341+
let force_space_after_colon = is_ty_kind_with_absolute_decl(&(*self.ty).kind);
2342+
result.push_str(colon_spaces(context.config, force_space_after_colon));
23392343
result.push_str(&after_comment);
23402344
let overhead = last_line_width(&result);
23412345
let max_width = shape

src/types.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, Rew
2222
use crate::shape::Shape;
2323
use crate::source_map::SpanUtils;
2424
use crate::spanned::Spanned;
25-
use crate::utils::{
26-
colon_spaces, extra_offset, first_line_width, format_extern, format_mutability,
27-
last_line_extendable, last_line_width, mk_sp, rewrite_ident,
28-
};
25+
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};
2926

3027
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
3128
pub(crate) enum PathContext {
@@ -434,8 +431,8 @@ where
434431
}
435432
}
436433

437-
fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
438-
colon_spaces(context.config, false)
434+
fn type_bound_colon(context: &RewriteContext<'_>, force_space_after_colon: bool) -> &'static str {
435+
colon_spaces(context.config, force_space_after_colon)
439436
}
440437

441438
// 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
454451
}
455452
}
456453

454+
fn is_bound_starts_with_absolut_decl(bounds: &ast::GenericBounds) -> bool {
455+
if bounds.len() == 0 {
456+
false
457+
} else {
458+
let first_bound = &bounds[0];
459+
match first_bound {
460+
ast::GenericBound::Trait(poly_trait_ref) =>
461+
poly_trait_ref.bound_generic_params.len() == 0 &&
462+
poly_trait_ref.modifiers == ast::TraitBoundModifiers{
463+
constness: ast::BoundConstness::Never,
464+
asyncness: ast::BoundAsyncness::Normal,
465+
polarity: ast::BoundPolarity::Positive,
466+
} &&
467+
is_absolute_decl_path(&poly_trait_ref.trait_ref.path),
468+
_ => false,
469+
}
470+
}
471+
}
472+
457473
impl Rewrite for ast::WherePredicate {
458474
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
459475
self.rewrite_result(context, shape).ok()
@@ -469,12 +485,15 @@ impl Rewrite for ast::WherePredicate {
469485
..
470486
}) => {
471487
let type_str = bounded_ty.rewrite_result(context, shape)?;
472-
let colon = type_bound_colon(context).trim_end();
488+
let force_space_after_colon = is_bound_starts_with_absolut_decl(bounds);
489+
is_ty_kind_with_absolute_decl(&(*bounded_ty).kind);
490+
let colon = type_bound_colon(context, force_space_after_colon).trim_end();
473491
let lhs = if let Some(binder_str) =
474492
rewrite_bound_params(context, shape, bound_generic_params)
475493
{
476494
format!("for<{binder_str}> {type_str}{colon}")
477495
} else {
496+
debug!("KANCIL {:?} {}", bounds, force_space_after_colon);
478497
format!("{type_str}{colon}")
479498
};
480499

@@ -565,7 +584,8 @@ fn rewrite_bounded_lifetime(
565584
if bounds.is_empty() {
566585
Ok(result)
567586
} else {
568-
let colon = type_bound_colon(context);
587+
// the code for this point is `x:&'a SomeType`, so, no need to force adding space after colon
588+
let colon = type_bound_colon(context, false);
569589
let overhead = last_line_width(&result) + colon.len();
570590
let shape = shape.sub_width(overhead, span)?;
571591
let result = format!(
@@ -680,7 +700,8 @@ impl Rewrite for ast::GenericParam {
680700
};
681701

682702
if !self.bounds.is_empty() {
683-
param.push_str(type_bound_colon(context));
703+
let force_space_after_colon = is_bound_starts_with_absolut_decl(&self.bounds);
704+
param.push_str(type_bound_colon(context, force_space_after_colon));
684705
param.push_str(&self.bounds.rewrite_result(context, shape)?)
685706
}
686707
if let ast::GenericParamKind::Type {

tests/source/issue-6470/case-1.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,44 @@ fn func11(x:&::some_crate::SomeType) {}
189189
fn func12(x :&::some_crate::SomeType) {}
190190
fn func13(x: &::some_crate::SomeType) {}
191191
fn func14(x : &::some_crate::SomeType) {}
192+
193+
fn print_gen_with_where1<T>(item: T)
194+
where
195+
T: ::some_crate::SomeTrait + Clone,
196+
{
197+
println!("{}", item.to_string());
198+
}
199+
200+
fn print_gen_with_where2<T>(item: T)
201+
where
202+
T:SomeTrait + Clone,
203+
{
204+
println!("{}", item.to_string());
205+
}
206+
207+
fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item: T) {
208+
println!("{}", item.to_string());
209+
}
210+
211+
fn print_gen_with_where4<T: some_crate::SomeTrait + Clone>(item: T) {
212+
println!("{}", item.to_string());
213+
}
214+
215+
216+
/// Adds two integers and returns the result.
217+
///
218+
/// # Parameters
219+
/// - `a`: The first integer to add.
220+
/// - `b`: The second integer to add.
221+
///
222+
/// # Returns
223+
/// The sum of `a` and `b` as an integer. The return type is `i32`.
224+
///
225+
/// # Example
226+
/// ```
227+
/// let result = add(2, 3);
228+
/// assert_eq!(result, 5);
229+
/// ```
230+
fn add(a: i32, b: i32) -> i32 {
231+
a + b
232+
}

tests/source/issue-6470/case-2.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,44 @@ fn func11(x:&::some_crate::SomeType) {}
189189
fn func12(x :&::some_crate::SomeType) {}
190190
fn func13(x: &::some_crate::SomeType) {}
191191
fn func14(x : &::some_crate::SomeType) {}
192+
193+
fn print_gen_with_where1<T>(item: T)
194+
where
195+
T: ::some_crate::SomeTrait + Clone,
196+
{
197+
println!("{}", item.to_string());
198+
}
199+
200+
fn print_gen_with_where2<T>(item: T)
201+
where
202+
T:SomeTrait + Clone,
203+
{
204+
println!("{}", item.to_string());
205+
}
206+
207+
fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item: T) {
208+
println!("{}", item.to_string());
209+
}
210+
211+
fn print_gen_with_where4<T: some_crate::SomeTrait + Clone>(item: T) {
212+
println!("{}", item.to_string());
213+
}
214+
215+
216+
/// Adds two integers and returns the result.
217+
///
218+
/// # Parameters
219+
/// - `a`: The first integer to add.
220+
/// - `b`: The second integer to add.
221+
///
222+
/// # Returns
223+
/// The sum of `a` and `b` as an integer. The return type is `i32`.
224+
///
225+
/// # Example
226+
/// ```
227+
/// let result = add(2, 3);
228+
/// assert_eq!(result, 5);
229+
/// ```
230+
fn add(a: i32, b: i32) -> i32 {
231+
a + b
232+
}

tests/target/issue-6470/case-1.rs

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
struct SomeStruct {
44
field1: ::some_crate::Thing,
55
field2: ::some_crate::Thing,
6-
field1_enum: ::some_crate::Thing,
7-
field2_enum: ::some_crate::Thing,
86

97
field3:some_crate::Thing,
108
field4:some_crate::Thing,
@@ -77,11 +75,9 @@ fn main() {
7775
let x13:&::some_crate::SomeType = ::some_crate::SomeType::default();
7876
let x14:&::some_crate::SomeType = ::some_crate::SomeType::default();
7977

80-
let y = SomeStruct {
78+
let y_call = SomeStruct {
8179
field1: ::some_crate::Thing::default(),
8280
field2: ::some_crate::Thing::default(),
83-
field1_enum: ::some_crate::Thing::Enum1,
84-
field2_enum: ::some_crate::Thing::Enum1,
8581

8682
field3:some_crate::Thing::default(),
8783
field4:some_crate::Thing::default(),
@@ -98,10 +94,86 @@ fn main() {
9894
field13:&::some_crate::Thing::default(),
9995
field14:&::some_crate::Thing::default(),
10096
};
97+
98+
let y_method_call = SomeStruct {
99+
field1: ::some_crate::Thing::Default.call(),
100+
field2: ::some_crate::Thing::Default.call(),
101+
102+
..y_call
103+
};
104+
105+
let y_binary = SomeStruct {
106+
field1: ::some_crate::Thing::Default + 12,
107+
field2: ::some_crate::Thing::Default + 12,
108+
109+
..y_call
110+
};
111+
112+
let y_cast = SomeStruct {
113+
field1: ::some_crate::Thing::Default as i32,
114+
field2: ::some_crate::Thing::Default as i32,
115+
116+
..y_call
117+
};
118+
119+
let y_type = SomeStruct {
120+
field7: ::some_crate::Thing::Default,
121+
field8: ::some_crate::Thing::Default,
122+
123+
..y_call
124+
};
125+
126+
let y_field = SomeStruct {
127+
field1: ::some_crate::Thing::Default.some_field,
128+
field2: ::some_crate::Thing::Default.some_field,
129+
130+
..y_call
131+
};
132+
133+
let y_index = SomeStruct {
134+
field1: ::some_crate::Thing::Default[0],
135+
field2: ::some_crate::Thing::Default[0],
136+
137+
..y_call
138+
};
139+
140+
let y_range = SomeStruct {
141+
field1: ::some_crate::Thing::DefaultStart..12,
142+
field2: ::some_crate::Thing::DefaultStart..12,
143+
144+
..y_call
145+
};
146+
147+
let y_path = SomeStruct {
148+
field1: ::some_crate::Thing::Default,
149+
field2: ::some_crate::Thing::Default,
150+
151+
..y_call
152+
};
153+
154+
let y_mac_call = SomeStruct {
155+
field1: ::some_crate::macr!(),
156+
field2: ::some_crate::macr!(),
157+
158+
..y_call
159+
};
160+
161+
let y_struct = SomeStruct {
162+
field1: ::some_crate::Thing::SomeStruct {
163+
fieldA1:123,
164+
fieldA2:123,
165+
},
166+
field2: ::some_crate::Thing::SomeStruct {
167+
fieldA1:123,
168+
fieldA2:123,
169+
},
170+
171+
..y_call
172+
};
101173
}
102174

103-
fn func1(x:::some_crate::SomeType) {}
104-
fn func2(x:::some_crate::SomeType) {}
175+
fn func1(x: ::some_crate::SomeType) {}
176+
fn func2(x: ::some_crate::SomeType) {}
105177
fn func3(x:some_crate::SomeType) {}
106178
fn func4(x:some_crate::SomeType) {}
107179
fn func5(x:some_crate::SomeType) {}
@@ -114,3 +186,26 @@ fn func11(x:&::some_crate::SomeType) {}
114186
fn func12(x:&::some_crate::SomeType) {}
115187
fn func13(x:&::some_crate::SomeType) {}
116188
fn func14(x:&::some_crate::SomeType) {}
189+
190+
fn print_gen_with_where1<T>(item:T)
191+
where
192+
T: ::some_crate::SomeTrait + Clone,
193+
{
194+
println!("{}", item.to_string());
195+
}
196+
197+
fn print_gen_with_where2<T>(item:T)
198+
where
199+
T: SomeTrait + Clone,
200+
{
201+
println!("{}", item.to_string());
202+
}
203+
204+
fn print_gen_with_where3<T: ::some_crate::SomeTrait + Clone>(item:T) {
205+
println!("{}", item.to_string());
206+
}
207+
208+
fn print_gen_with_where4<T:some_crate::SomeTrait + Clone>(item:T) {
209+
println!("{}", item.to_string());
210+
}
211+

0 commit comments

Comments
 (0)