@@ -19,10 +19,7 @@ use crate::expr::{
19
19
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
20
20
ExprType , RhsTactics ,
21
21
} ;
22
- use crate :: lists:: {
23
- definitive_tactic, extract_post_comment, extract_pre_comment, get_comment_end,
24
- has_extra_newline, itemize_list, write_list, ListFormatting , ListItem , Separator ,
25
- } ;
22
+ use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
26
23
use crate :: macros:: { rewrite_macro, MacroPosition } ;
27
24
use crate :: overflow;
28
25
use crate :: rewrite:: { Rewrite , RewriteContext } ;
@@ -194,7 +191,7 @@ impl<'a> FnSig<'a> {
194
191
) -> FnSig < ' a > {
195
192
FnSig {
196
193
unsafety : method_sig. header . unsafety ,
197
- is_async : method_sig. header . asyncness ,
194
+ is_async : method_sig. header . asyncness . node ,
198
195
constness : method_sig. header . constness . node ,
199
196
defaultness : ast:: Defaultness :: Final ,
200
197
abi : method_sig. header . abi ,
@@ -216,7 +213,7 @@ impl<'a> FnSig<'a> {
216
213
generics,
217
214
abi : fn_header. abi ,
218
215
constness : fn_header. constness . node ,
219
- is_async : fn_header. asyncness ,
216
+ is_async : fn_header. asyncness . node ,
220
217
defaultness,
221
218
unsafety : fn_header. unsafety ,
222
219
visibility : visibility. clone ( ) ,
@@ -1833,7 +1830,9 @@ fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
1833
1830
1834
1831
impl Rewrite for ast:: Arg {
1835
1832
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1836
- if is_named_arg ( self ) {
1833
+ if let Some ( ref explicit_self) = self . to_self ( ) {
1834
+ rewrite_explicit_self ( context, explicit_self)
1835
+ } else if is_named_arg ( self ) {
1837
1836
let mut result = self
1838
1837
. pat
1839
1838
. rewrite ( context, Shape :: legacy ( shape. width , shape. indent ) ) ?;
@@ -1862,9 +1861,8 @@ impl Rewrite for ast::Arg {
1862
1861
}
1863
1862
1864
1863
fn rewrite_explicit_self (
1865
- explicit_self : & ast:: ExplicitSelf ,
1866
- args : & [ ast:: Arg ] ,
1867
1864
context : & RewriteContext < ' _ > ,
1865
+ explicit_self : & ast:: ExplicitSelf ,
1868
1866
) -> Option < String > {
1869
1867
match explicit_self. node {
1870
1868
ast:: SelfKind :: Region ( lt, m) => {
@@ -1880,10 +1878,7 @@ fn rewrite_explicit_self(
1880
1878
None => Some ( format ! ( "&{}self" , mut_str) ) ,
1881
1879
}
1882
1880
}
1883
- ast:: SelfKind :: Explicit ( ref ty, _) => {
1884
- assert ! ( !args. is_empty( ) , "&[ast::Arg] shouldn't be empty." ) ;
1885
-
1886
- let mutability = explicit_self_mutability ( & args[ 0 ] ) ;
1881
+ ast:: SelfKind :: Explicit ( ref ty, mutability) => {
1887
1882
let type_str = ty. rewrite (
1888
1883
context,
1889
1884
Shape :: legacy ( context. config . max_width ( ) , Indent :: empty ( ) ) ,
@@ -1895,23 +1890,7 @@ fn rewrite_explicit_self(
1895
1890
type_str
1896
1891
) )
1897
1892
}
1898
- ast:: SelfKind :: Value ( _) => {
1899
- assert ! ( !args. is_empty( ) , "&[ast::Arg] shouldn't be empty." ) ;
1900
-
1901
- let mutability = explicit_self_mutability ( & args[ 0 ] ) ;
1902
-
1903
- Some ( format ! ( "{}self" , format_mutability( mutability) ) )
1904
- }
1905
- }
1906
- }
1907
-
1908
- // Hacky solution caused by absence of `Mutability` in `SelfValue` and
1909
- // `SelfExplicit` variants of `ast::ExplicitSelf_`.
1910
- fn explicit_self_mutability ( arg : & ast:: Arg ) -> ast:: Mutability {
1911
- if let ast:: PatKind :: Ident ( ast:: BindingMode :: ByValue ( mutability) , _, _) = arg. pat . node {
1912
- mutability
1913
- } else {
1914
- unreachable ! ( )
1893
+ ast:: SelfKind :: Value ( mutability) => Some ( format ! ( "{}self" , format_mutability( mutability) ) ) ,
1915
1894
}
1916
1895
}
1917
1896
@@ -2048,14 +2027,12 @@ fn rewrite_fn_base(
2048
2027
let arg_str = rewrite_args (
2049
2028
context,
2050
2029
& fd. inputs ,
2051
- fd. get_self ( ) . as_ref ( ) ,
2052
2030
one_line_budget,
2053
2031
multi_line_budget,
2054
2032
indent,
2055
2033
arg_indent,
2056
2034
args_span,
2057
- fd. variadic ,
2058
- generics_str. contains ( '\n' ) ,
2035
+ fd. c_variadic ,
2059
2036
) ?;
2060
2037
2061
2038
let put_args_in_block = match context. config . indent_style ( ) {
@@ -2272,19 +2249,13 @@ impl WhereClauseOption {
2272
2249
fn rewrite_args (
2273
2250
context : & RewriteContext < ' _ > ,
2274
2251
args : & [ ast:: Arg ] ,
2275
- explicit_self : Option < & ast:: ExplicitSelf > ,
2276
2252
one_line_budget : usize ,
2277
2253
multi_line_budget : usize ,
2278
2254
indent : Indent ,
2279
2255
arg_indent : Indent ,
2280
2256
span : Span ,
2281
2257
variadic : bool ,
2282
- generics_str_contains_newline : bool ,
2283
2258
) -> Option < String > {
2284
- let terminator = ")" ;
2285
- let separator = "," ;
2286
- let next_span_start = span. hi ( ) ;
2287
-
2288
2259
if args. len ( ) == 0 {
2289
2260
let comment = context
2290
2261
. snippet ( mk_sp (
@@ -2295,158 +2266,47 @@ fn rewrite_args(
2295
2266
. trim ( ) ;
2296
2267
return Some ( comment. to_owned ( ) ) ;
2297
2268
}
2298
-
2299
- let mut arg_item_strs = args
2300
- . iter ( )
2301
- . map ( |arg| {
2269
+ let arg_items: Vec < _ > = itemize_list (
2270
+ context. snippet_provider ,
2271
+ args. iter ( ) ,
2272
+ ")" ,
2273
+ "," ,
2274
+ |arg| span_lo_for_arg ( arg) ,
2275
+ |arg| arg. ty . span . hi ( ) ,
2276
+ |arg| {
2302
2277
arg. rewrite ( context, Shape :: legacy ( multi_line_budget, arg_indent) )
2303
- . unwrap_or_else ( || context. snippet ( arg. span ( ) ) . to_owned ( ) )
2304
- } )
2305
- . collect :: < Vec < _ > > ( ) ;
2306
-
2307
- // Account for sugary self.
2308
- let mut pre_comment_str = "" ;
2309
- let mut post_comment_str = "" ;
2310
- let min_args = explicit_self
2311
- . and_then ( |explicit_self| rewrite_explicit_self ( explicit_self, args, context) )
2312
- . map_or ( 1 , |self_str| {
2313
- pre_comment_str = context. snippet ( mk_sp ( span. lo ( ) , args[ 0 ] . pat . span . lo ( ) ) ) ;
2314
-
2315
- let next_start = if args. len ( ) > 1 {
2316
- args[ 1 ] . pat . span ( ) . lo ( )
2317
- } else {
2318
- span. hi ( )
2319
- } ;
2320
- post_comment_str = context. snippet ( mk_sp ( args[ 0 ] . ty . span . hi ( ) , next_start) ) ;
2321
-
2322
- arg_item_strs[ 0 ] = self_str;
2323
- 2
2324
- } ) ;
2325
-
2326
- // Comments between args.
2327
- let mut arg_items = Vec :: new ( ) ;
2328
- if min_args == 2 {
2329
- arg_items. push ( ListItem :: from_str ( "" ) ) ;
2330
- }
2331
-
2332
- if args. len ( ) >= min_args || variadic {
2333
- let comment_span_start = if min_args == 2 {
2334
- let remove_comma_byte_pos = context
2335
- . snippet_provider
2336
- . span_after ( mk_sp ( args[ 0 ] . ty . span . hi ( ) , args[ 1 ] . pat . span . lo ( ) ) , "," ) ;
2337
- let first_post_and_second_pre_span =
2338
- mk_sp ( remove_comma_byte_pos, args[ 1 ] . pat . span . lo ( ) ) ;
2339
- if count_newlines ( context. snippet ( first_post_and_second_pre_span) ) > 0 {
2340
- context
2341
- . snippet_provider
2342
- . span_after ( first_post_and_second_pre_span, "\n " )
2343
- } else {
2344
- remove_comma_byte_pos
2345
- }
2346
- } else {
2347
- span. lo ( )
2348
- } ;
2349
-
2350
- enum ArgumentKind < ' a > {
2351
- Regular ( & ' a ast:: Arg ) ,
2352
- Variadic ( BytePos ) ,
2353
- }
2354
-
2355
- let variadic_arg = if variadic {
2356
- let variadic_span = mk_sp ( args. last ( ) . unwrap ( ) . ty . span . hi ( ) , span. hi ( ) ) ;
2357
- let variadic_start =
2358
- context. snippet_provider . span_after ( variadic_span, "..." ) - BytePos ( 3 ) ;
2359
- Some ( ArgumentKind :: Variadic ( variadic_start) )
2360
- } else {
2361
- None
2362
- } ;
2363
-
2364
- let more_items = itemize_list (
2365
- context. snippet_provider ,
2366
- args[ min_args - 1 ..]
2367
- . iter ( )
2368
- . map ( ArgumentKind :: Regular )
2369
- . chain ( variadic_arg) ,
2370
- terminator,
2371
- separator,
2372
- |arg| match * arg {
2373
- ArgumentKind :: Regular ( arg) => span_lo_for_arg ( arg) ,
2374
- ArgumentKind :: Variadic ( start) => start,
2375
- } ,
2376
- |arg| match * arg {
2377
- ArgumentKind :: Regular ( arg) => arg. ty . span . hi ( ) ,
2378
- ArgumentKind :: Variadic ( start) => start + BytePos ( 3 ) ,
2379
- } ,
2380
- |arg| match * arg {
2381
- ArgumentKind :: Regular ( ..) => None ,
2382
- ArgumentKind :: Variadic ( ..) => Some ( "..." . to_owned ( ) ) ,
2383
- } ,
2384
- comment_span_start,
2385
- next_span_start,
2386
- false ,
2387
- ) ;
2388
-
2389
- arg_items. extend ( more_items) ;
2390
- }
2391
-
2392
- let arg_items_len = arg_items. len ( ) ;
2393
- let fits_in_one_line = !generics_str_contains_newline
2394
- && ( arg_items. is_empty ( )
2395
- || arg_items_len == 1 && arg_item_strs[ 0 ] . len ( ) <= one_line_budget) ;
2396
-
2397
- for ( index, ( item, arg) ) in arg_items. iter_mut ( ) . zip ( arg_item_strs) . enumerate ( ) {
2398
- // add pre comment and post comment for first arg(self)
2399
- if index == 0 && explicit_self. is_some ( ) {
2400
- let ( pre_comment, pre_comment_style) = extract_pre_comment ( pre_comment_str) ;
2401
- item. pre_comment = pre_comment;
2402
- item. pre_comment_style = pre_comment_style;
2403
-
2404
- let comment_end =
2405
- get_comment_end ( post_comment_str, separator, terminator, arg_items_len == 1 ) ;
2406
-
2407
- item. new_lines = has_extra_newline ( post_comment_str, comment_end) ;
2408
- item. post_comment = extract_post_comment ( post_comment_str, comment_end, separator) ;
2409
- }
2410
- item. item = Some ( arg) ;
2411
- }
2412
-
2413
- let last_line_ends_with_comment = arg_items
2414
- . iter ( )
2415
- . last ( )
2416
- . and_then ( |item| item. post_comment . as_ref ( ) )
2417
- . map_or ( false , |s| s. trim ( ) . starts_with ( "//" ) ) ;
2418
-
2419
- let ( indent, trailing_comma) = match context. config . indent_style ( ) {
2420
- IndentStyle :: Block if fits_in_one_line => {
2421
- ( indent. block_indent ( context. config ) , SeparatorTactic :: Never )
2422
- }
2423
- IndentStyle :: Block => (
2424
- indent. block_indent ( context. config ) ,
2425
- context. config . trailing_comma ( ) ,
2426
- ) ,
2427
- IndentStyle :: Visual if last_line_ends_with_comment => {
2428
- ( arg_indent, context. config . trailing_comma ( ) )
2429
- }
2430
- IndentStyle :: Visual => ( arg_indent, SeparatorTactic :: Never ) ,
2431
- } ;
2278
+ . or_else ( || Some ( context. snippet ( arg. span ( ) ) . to_owned ( ) ) )
2279
+ } ,
2280
+ span. lo ( ) ,
2281
+ span. hi ( ) ,
2282
+ false ,
2283
+ )
2284
+ . collect ( ) ;
2432
2285
2433
2286
let tactic = definitive_tactic (
2434
2287
& arg_items,
2435
- context. config . fn_args_density ( ) . to_list_tactic ( ) ,
2288
+ context
2289
+ . config
2290
+ . fn_args_density ( )
2291
+ . to_list_tactic ( arg_items. len ( ) ) ,
2436
2292
Separator :: Comma ,
2437
2293
one_line_budget,
2438
2294
) ;
2439
2295
let budget = match tactic {
2440
2296
DefinitiveListTactic :: Horizontal => one_line_budget,
2441
2297
_ => multi_line_budget,
2442
2298
} ;
2443
-
2444
- debug ! ( "rewrite_args: budget: {}, tactic: {:?}" , budget, tactic) ;
2445
-
2299
+ let indent = match context. config . indent_style ( ) {
2300
+ IndentStyle :: Block => indent. block_indent ( context. config ) ,
2301
+ IndentStyle :: Visual => arg_indent,
2302
+ } ;
2446
2303
let trailing_separator = if variadic {
2447
2304
SeparatorTactic :: Never
2448
2305
} else {
2449
- trailing_comma
2306
+ match context. config . indent_style ( ) {
2307
+ IndentStyle :: Block => context. config . trailing_comma ( ) ,
2308
+ IndentStyle :: Visual => SeparatorTactic :: Never ,
2309
+ }
2450
2310
} ;
2451
2311
let fmt = ListFormatting :: new ( Shape :: legacy ( budget, indent) , context. config )
2452
2312
. tactic ( tactic)
0 commit comments