22
33use std:: borrow:: Cow ;
44use std:: cmp:: { Ordering , max, min} ;
5+ use std:: backtrace:: Backtrace ;
56
67use regex:: Regex ;
78use 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+
19271938pub ( 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
0 commit comments