@@ -384,8 +384,12 @@ impl<'a> PeepholeOptimizations {
384384 if let Some ( first_decl) = var_decl. declarations . first_mut ( )
385385 && let Some ( first_decl_init) = first_decl. init . as_mut ( )
386386 {
387- let changed =
388- Self :: substitute_single_use_symbol_in_statement ( first_decl_init, result, ctx) ;
387+ let changed = Self :: substitute_single_use_symbol_in_statement (
388+ first_decl_init,
389+ result,
390+ ctx,
391+ false ,
392+ ) ;
389393 if changed {
390394 ctx. state . changed = true ;
391395 }
@@ -433,8 +437,12 @@ impl<'a> PeepholeOptimizations {
433437
434438 ctx : & mut Ctx < ' a , ' _ > ,
435439 ) {
436- let changed =
437- Self :: substitute_single_use_symbol_in_statement ( & mut expr_stmt. expression , result, ctx) ;
440+ let changed = Self :: substitute_single_use_symbol_in_statement (
441+ & mut expr_stmt. expression ,
442+ result,
443+ ctx,
444+ false ,
445+ ) ;
438446 if changed {
439447 ctx. state . changed = true ;
440448 }
@@ -562,6 +570,7 @@ impl<'a> PeepholeOptimizations {
562570 & mut switch_stmt. discriminant ,
563571 result,
564572 ctx,
573+ false ,
565574 ) ;
566575 if changed {
567576 ctx. state . changed = true ;
@@ -588,7 +597,7 @@ impl<'a> PeepholeOptimizations {
588597 ctx : & mut Ctx < ' a , ' _ > ,
589598 ) -> ControlFlow < ( ) > {
590599 let changed =
591- Self :: substitute_single_use_symbol_in_statement ( & mut if_stmt. test , result, ctx) ;
600+ Self :: substitute_single_use_symbol_in_statement ( & mut if_stmt. test , result, ctx, false ) ;
592601 if changed {
593602 ctx. state . changed = true ;
594603 }
@@ -747,8 +756,12 @@ impl<'a> PeepholeOptimizations {
747756 ctx : & mut Ctx < ' a , ' _ > ,
748757 ) {
749758 if let Some ( ret_argument_expr) = & mut ret_stmt. argument {
750- let changed =
751- Self :: substitute_single_use_symbol_in_statement ( ret_argument_expr, result, ctx) ;
759+ let changed = Self :: substitute_single_use_symbol_in_statement (
760+ ret_argument_expr,
761+ result,
762+ ctx,
763+ false ,
764+ ) ;
752765 if changed {
753766 ctx. state . changed = true ;
754767 }
@@ -799,8 +812,12 @@ impl<'a> PeepholeOptimizations {
799812
800813 ctx : & mut Ctx < ' a , ' _ > ,
801814 ) {
802- let changed =
803- Self :: substitute_single_use_symbol_in_statement ( & mut throw_stmt. argument , result, ctx) ;
815+ let changed = Self :: substitute_single_use_symbol_in_statement (
816+ & mut throw_stmt. argument ,
817+ result,
818+ ctx,
819+ false ,
820+ ) ;
804821 if changed {
805822 ctx. state . changed = true ;
806823 }
@@ -830,10 +847,12 @@ impl<'a> PeepholeOptimizations {
830847 if let Some ( first_decl) = var_decl. declarations . first_mut ( )
831848 && let Some ( first_decl_init) = first_decl. init . as_mut ( )
832849 {
850+ let is_block_scoped_decl = !first_decl. kind . is_var ( ) ;
833851 let changed = Self :: substitute_single_use_symbol_in_statement (
834852 first_decl_init,
835853 result,
836854 ctx,
855+ is_block_scoped_decl,
837856 ) ;
838857 if changed {
839858 ctx. state . changed = true ;
@@ -843,7 +862,7 @@ impl<'a> PeepholeOptimizations {
843862 match_expression ! ( ForStatementInit ) => {
844863 let init = init. to_expression_mut ( ) ;
845864 let changed =
846- Self :: substitute_single_use_symbol_in_statement ( init, result, ctx) ;
865+ Self :: substitute_single_use_symbol_in_statement ( init, result, ctx, false ) ;
847866 if changed {
848867 ctx. state . changed = true ;
849868 }
@@ -922,10 +941,12 @@ impl<'a> PeepholeOptimizations {
922941 // That is evaluated before the right hand side is evaluated. So, in that case, skip the single use substitution.
923942 if !matches ! ( & for_in_stmt. left, ForStatementLeft :: VariableDeclaration ( var_decl) if var_decl. has_init( ) )
924943 {
944+ let is_block_scoped_decl = matches ! ( & for_in_stmt. left, ForStatementLeft :: VariableDeclaration ( var_decl) if !var_decl. kind. is_var( ) ) ;
925945 let changed = Self :: substitute_single_use_symbol_in_statement (
926946 & mut for_in_stmt. right ,
927947 result,
928948 ctx,
949+ is_block_scoped_decl,
929950 ) ;
930951 if changed {
931952 ctx. state . changed = true ;
@@ -1005,8 +1026,13 @@ impl<'a> PeepholeOptimizations {
10051026 result : & mut Vec < ' a , Statement < ' a > > ,
10061027 ctx : & mut Ctx < ' a , ' _ > ,
10071028 ) {
1008- let changed =
1009- Self :: substitute_single_use_symbol_in_statement ( & mut for_of_stmt. right , result, ctx) ;
1029+ let is_block_scoped_decl = matches ! ( & for_of_stmt. left, ForStatementLeft :: VariableDeclaration ( var_decl) if !var_decl. kind. is_var( ) ) ;
1030+ let changed = Self :: substitute_single_use_symbol_in_statement (
1031+ & mut for_of_stmt. right ,
1032+ result,
1033+ ctx,
1034+ is_block_scoped_decl,
1035+ ) ;
10101036 if changed {
10111037 ctx. state . changed = true ;
10121038 }
@@ -1100,6 +1126,7 @@ impl<'a> PeepholeOptimizations {
11001126 expr_in_stmt : & mut Expression < ' a > ,
11011127 stmts : & mut Vec < ' a , Statement < ' a > > ,
11021128 ctx : & Ctx < ' a , ' _ > ,
1129+ non_scoped_literal_only : bool ,
11031130 ) -> bool {
11041131 // TODO: we should skip this compression when direct eval exists
11051132 // because the code inside eval may reference the variable
@@ -1139,6 +1166,9 @@ impl<'a> PeepholeOptimizations {
11391166 {
11401167 return true ;
11411168 }
1169+ if non_scoped_literal_only && !prev_decl_init. is_literal_value ( false , ctx) {
1170+ return true ;
1171+ }
11421172 let replaced = Self :: substitute_single_use_symbol_in_expression (
11431173 expr_in_stmt,
11441174 & prev_decl_id. name ,
0 commit comments