Skip to content

Commit 9255320

Browse files
committed
add change, and testcase
1 parent 054efdd commit 9255320

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ pub(crate) fn wrap_struct_field(
17861786
}
17871787

17881788
pub(crate) fn struct_lit_field_separator(config: &Config) -> &str {
1789-
colon_spaces(config)
1789+
colon_spaces(config, false)
17901790
}
17911791

17921792
pub(crate) fn rewrite_field(

src/items.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::borrow::Cow;
44
use std::cmp::{Ordering, max, min};
5+
use std::backtrace::Backtrace;
56

67
use regex::Regex;
78
use rustc_ast::visit;
@@ -42,8 +43,8 @@ const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
4243
tokens: None,
4344
};
4445

45-
fn type_annotation_separator(config: &Config) -> &str {
46-
colon_spaces(config)
46+
fn type_annotation_separator(config: &Config, force_space_after_colon: bool) -> &str {
47+
colon_spaces(config, force_space_after_colon)
4748
}
4849

4950
// Statements of the form
@@ -96,7 +97,13 @@ impl Rewrite for ast::Local {
9697
let mut infix = String::with_capacity(32);
9798

9899
if let Some(ref ty) = self.ty {
99-
let separator = type_annotation_separator(context.config);
100+
101+
let force_space_after_colon = match ty.clone().into_inner().kind {
102+
ast::TyKind::Path(None, _) => true,
103+
_ => false,
104+
};
105+
106+
let separator = type_annotation_separator(context.config, force_space_after_colon);
100107
let ty_shape = if pat_str.contains('\n') {
101108
shape.with_max_width(context.config)
102109
} else {
@@ -1890,10 +1897,10 @@ fn rewrite_ty<R: Rewrite>(
18901897
Ok(result)
18911898
}
18921899

1893-
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1900+
fn type_annotation_spacing(config: &Config, force_space_after_colon: bool) -> (&str, &str) {
18941901
(
18951902
if config.space_before_colon() { " " } else { "" },
1896-
if config.space_after_colon() { " " } else { "" },
1903+
if force_space_after_colon || config.space_after_colon() { " " } else { "" },
18971904
)
18981905
}
18991906

@@ -1903,7 +1910,7 @@ pub(crate) fn rewrite_struct_field_prefix(
19031910
) -> RewriteResult {
19041911
let vis = format_visibility(context, &field.vis);
19051912
let safety = format_safety(field.safety);
1906-
let type_annotation_spacing = type_annotation_spacing(context.config);
1913+
let type_annotation_spacing = type_annotation_spacing(context.config, false);
19071914
Ok(match field.ident {
19081915
Some(name) => format!(
19091916
"{vis}{safety}{}{}:",
@@ -1924,6 +1931,10 @@ impl Rewrite for ast::FieldDef {
19241931
}
19251932
}
19261933

1934+
use std::sync::atomic::{AtomicUsize};
1935+
1936+
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
1937+
19271938
pub(crate) fn rewrite_struct_field(
19281939
context: &RewriteContext<'_>,
19291940
field: &ast::FieldDef,
@@ -1939,7 +1950,11 @@ pub(crate) fn rewrite_struct_field(
19391950
return Ok(context.snippet(field.span()).to_owned());
19401951
}
19411952

1942-
let type_annotation_spacing = type_annotation_spacing(context.config);
1953+
let force_space_after_colon = match field.ty.clone().into_inner().kind {
1954+
ast::TyKind::Path(None, _) => true,
1955+
_ => false,
1956+
};
1957+
let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon);
19431958
let prefix = rewrite_struct_field_prefix(context, field)?;
19441959

19451960
let attrs_str = field.attrs.rewrite_result(context, shape)?;
@@ -2091,7 +2106,14 @@ fn rewrite_static(
20912106
return None;
20922107
}
20932108

2094-
let colon = colon_spaces(context.config);
2109+
// if after a semicolon is absolute path declaration (::) need to force
2110+
// space after colon, because ::: syntax cannot compile
2111+
let force_space_after_colon = match static_parts.ty.kind {
2112+
ast::TyKind::Path(None, _) => true,
2113+
_ => false,
2114+
};
2115+
let colon = colon_spaces(context.config, force_space_after_colon);
2116+
20952117
let mut prefix = format!(
20962118
"{}{}{}{} {}{}{}",
20972119
format_visibility(context, static_parts.vis),
@@ -2294,7 +2316,7 @@ impl Rewrite for ast::Param {
22942316
let (before_comment, after_comment) =
22952317
get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
22962318
result.push_str(&before_comment);
2297-
result.push_str(colon_spaces(context.config));
2319+
result.push_str(colon_spaces(context.config, false));
22982320
result.push_str(&after_comment);
22992321
let overhead = last_line_width(&result);
23002322
let max_width = shape
@@ -2322,7 +2344,7 @@ impl Rewrite for ast::Param {
23222344
!has_multiple_attr_lines,
23232345
)?;
23242346
result.push_str(&before_comment);
2325-
result.push_str(colon_spaces(context.config));
2347+
result.push_str(colon_spaces(context.config, false));
23262348
result.push_str(&after_comment);
23272349
let overhead = last_line_width(&result);
23282350
let max_width = shape

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ where
435435
}
436436

437437
fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
438-
colon_spaces(context.config)
438+
colon_spaces(context.config, false)
439439
}
440440

441441
// If the return type is multi-lined, then force to use multiple lines for

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,9 @@ pub(crate) fn filtered_str_fits(snippet: &str, max_width: usize, shape: Shape) -
423423
}
424424

425425
#[inline]
426-
pub(crate) fn colon_spaces(config: &Config) -> &'static str {
426+
pub(crate) fn colon_spaces(config: &Config, force_space_after_colon: bool) -> &'static str {
427427
let before = config.space_before_colon();
428-
let after = config.space_after_colon();
428+
let after = force_space_after_colon || config.space_after_colon();
429429
match (before, after) {
430430
(true, true) => " : ",
431431
(true, false) => " :",

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-space_after_colon: false
2+
3+
struct SomeStruct {
4+
some_field: ::some_crate::Thing,
5+
}
6+
7+
const THING: ::some_crate::SomeType = ::some_crate::SomeType::default();
8+
9+
fn main() {
10+
let x: ::some_crate::SomeType = ::some_crate::SomeType::default();
11+
let a1: int;
12+
let a2:int;
13+
let a3 :int;
14+
let a4 : int;
15+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-space_after_colon: false
2+
3+
struct SomeStruct {
4+
some_field: ::some_crate::Thing,
5+
}
6+
7+
const THING: ::some_crate::SomeType = ::some_crate::SomeType::default();
8+
9+
fn main() {
10+
let x: ::some_crate::SomeType = ::some_crate::SomeType::default();
11+
let a1: int;
12+
let a2: int;
13+
let a3: int;
14+
let a4: int;
15+
}

0 commit comments

Comments
 (0)