@@ -380,7 +380,6 @@ fn expand_format_args<'hir>(
380
380
}
381
381
}
382
382
} ) ) ;
383
- let lit_pieces = ctx. expr_array_ref ( fmt. span , lit_pieces) ;
384
383
385
384
// Whether we'll use the `Arguments::new_v1_formatted` form (true),
386
385
// or the `Arguments::new_v1` form (false).
@@ -407,6 +406,24 @@ fn expand_format_args<'hir>(
407
406
}
408
407
}
409
408
409
+ let arguments = fmt. arguments . all_args ( ) ;
410
+
411
+ if allow_const && lit_pieces. len ( ) <= 1 && arguments. is_empty ( ) && argmap. is_empty ( ) {
412
+ // Generate:
413
+ // <core::fmt::Arguments>::new_str(literal)
414
+ let new = ctx. arena . alloc ( ctx. expr_lang_item_type_relative (
415
+ macsp,
416
+ hir:: LangItem :: FormatArguments ,
417
+ sym:: new_str,
418
+ ) ) ;
419
+ let args = if lit_pieces. is_empty ( ) {
420
+ ctx. arena . alloc_from_iter ( [ ctx. expr_str ( fmt. span , kw:: Empty ) ] ) // Empty string.
421
+ } else {
422
+ lit_pieces // Just one single literal string piece.
423
+ } ;
424
+ return hir:: ExprKind :: Call ( new, args) ;
425
+ }
426
+
410
427
let format_options = use_format_options. then ( || {
411
428
// Generate:
412
429
// &[format_spec_0, format_spec_1, format_spec_2]
@@ -421,20 +438,6 @@ fn expand_format_args<'hir>(
421
438
ctx. expr_array_ref ( macsp, ctx. arena . alloc_from_iter ( elements) )
422
439
} ) ;
423
440
424
- let arguments = fmt. arguments . all_args ( ) ;
425
-
426
- if allow_const && arguments. is_empty ( ) && argmap. is_empty ( ) {
427
- // Generate:
428
- // <core::fmt::Arguments>::new_const(lit_pieces)
429
- let new = ctx. arena . alloc ( ctx. expr_lang_item_type_relative (
430
- macsp,
431
- hir:: LangItem :: FormatArguments ,
432
- sym:: new_const,
433
- ) ) ;
434
- let new_args = ctx. arena . alloc_from_iter ( [ lit_pieces] ) ;
435
- return hir:: ExprKind :: Call ( new, new_args) ;
436
- }
437
-
438
441
// If the args array contains exactly all the original arguments once,
439
442
// in order, we can use a simple array instead of a `match` construction.
440
443
// However, if there's a yield point in any argument except the first one,
@@ -555,6 +558,8 @@ fn expand_format_args<'hir>(
555
558
)
556
559
} ;
557
560
561
+ let lit_pieces = ctx. expr_array_ref ( fmt. span , lit_pieces) ;
562
+
558
563
if let Some ( format_options) = format_options {
559
564
// Generate:
560
565
// <core::fmt::Arguments>::new_v1_formatted(
0 commit comments