1
- use core:: ops:: ControlFlow ;
2
1
use std:: borrow:: Cow ;
3
2
4
- use rustc_ast:: visit:: Visitor ;
5
3
use rustc_ast:: * ;
6
4
use rustc_data_structures:: fx:: FxIndexMap ;
7
5
use rustc_hir as hir;
@@ -476,17 +474,6 @@ fn expand_format_args<'hir>(
476
474
return hir:: ExprKind :: Call ( new, new_args) ;
477
475
}
478
476
479
- // If the args array contains exactly all the original arguments once,
480
- // in order, we can use a simple array instead of a `match` construction.
481
- // However, if there's a yield point in any argument except the first one,
482
- // we don't do this, because an Argument cannot be kept across yield points.
483
- //
484
- // This is an optimization, speeding up compilation about 1-2% in some cases.
485
- // See https://github.com/rust-lang/rust/pull/106770#issuecomment-1380790609
486
- let use_simple_array = argmap. len ( ) == arguments. len ( )
487
- && argmap. iter ( ) . enumerate ( ) . all ( |( i, ( & ( j, _) , _) ) | i == j)
488
- && arguments. iter ( ) . skip ( 1 ) . all ( |arg| !may_contain_yield_point ( & arg. expr ) ) ;
489
-
490
477
let args = if arguments. is_empty ( ) {
491
478
// Generate:
492
479
// &<core::fmt::Argument>::none()
@@ -510,31 +497,6 @@ fn expand_format_args<'hir>(
510
497
) ) ;
511
498
let none = ctx. expr_call ( macsp, none_fn, & [ ] ) ;
512
499
ctx. expr ( macsp, hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Ref , hir:: Mutability :: Not , none) )
513
- } else if use_simple_array {
514
- // Generate:
515
- // &[
516
- // <core::fmt::Argument>::new_display(&arg0),
517
- // <core::fmt::Argument>::new_lower_hex(&arg1),
518
- // <core::fmt::Argument>::new_debug(&arg2),
519
- // …
520
- // ]
521
- let elements = ctx. arena . alloc_from_iter ( arguments. iter ( ) . zip ( argmap) . map (
522
- |( arg, ( ( _, ty) , placeholder_span) ) | {
523
- let placeholder_span =
524
- placeholder_span. unwrap_or ( arg. expr . span ) . with_ctxt ( macsp. ctxt ( ) ) ;
525
- let arg_span = match arg. kind {
526
- FormatArgumentKind :: Captured ( _) => placeholder_span,
527
- _ => arg. expr . span . with_ctxt ( macsp. ctxt ( ) ) ,
528
- } ;
529
- let arg = ctx. lower_expr ( & arg. expr ) ;
530
- let ref_arg = ctx. arena . alloc ( ctx. expr (
531
- arg_span,
532
- hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Ref , hir:: Mutability :: Not , arg) ,
533
- ) ) ;
534
- make_argument ( ctx, placeholder_span, ref_arg, ty)
535
- } ,
536
- ) ) ;
537
- ctx. expr_array_ref ( macsp, elements)
538
500
} else {
539
501
// Generate:
540
502
// &match (&arg0, &arg1, &…) {
@@ -635,34 +597,6 @@ fn expand_format_args<'hir>(
635
597
}
636
598
}
637
599
638
- fn may_contain_yield_point ( e : & ast:: Expr ) -> bool {
639
- struct MayContainYieldPoint ;
640
-
641
- impl Visitor < ' _ > for MayContainYieldPoint {
642
- type Result = ControlFlow < ( ) > ;
643
-
644
- fn visit_expr ( & mut self , e : & ast:: Expr ) -> ControlFlow < ( ) > {
645
- if let ast:: ExprKind :: Await ( _, _) | ast:: ExprKind :: Yield ( _) = e. kind {
646
- ControlFlow :: Break ( ( ) )
647
- } else {
648
- visit:: walk_expr ( self , e)
649
- }
650
- }
651
-
652
- fn visit_mac_call ( & mut self , _: & ast:: MacCall ) -> ControlFlow < ( ) > {
653
- // Macros should be expanded at this point.
654
- unreachable ! ( "unexpanded macro in ast lowering" ) ;
655
- }
656
-
657
- fn visit_item ( & mut self , _: & ast:: Item ) -> ControlFlow < ( ) > {
658
- // Do not recurse into nested items.
659
- ControlFlow :: Continue ( ( ) )
660
- }
661
- }
662
-
663
- MayContainYieldPoint . visit_expr ( e) . is_break ( )
664
- }
665
-
666
600
fn for_all_argument_indexes ( template : & mut [ FormatArgsPiece ] , mut f : impl FnMut ( & mut usize ) ) {
667
601
for piece in template {
668
602
let FormatArgsPiece :: Placeholder ( placeholder) = piece else { continue } ;
0 commit comments