@@ -59,7 +59,13 @@ impl Optimizer<'_> {
59
59
}
60
60
}
61
61
62
- if let Some ( usage) = self . data . vars . get ( & ident. node_id ) {
62
+ let node_id = match self . r . find_binding_by_ident ( ident) {
63
+ RefTo :: Itself => ident. node_id ,
64
+ RefTo :: Binding ( node_id) => node_id,
65
+ RefTo :: Unresolved => return ,
66
+ } ;
67
+ debug_assert ! ( node_id != NodeId :: DUMMY ) ;
68
+ if let Some ( usage) = self . data . vars . get ( & node_id) {
63
69
let ref_count = usage. ref_count - u32:: from ( can_drop && usage. ref_count > 1 ) ;
64
70
if !usage. flags . contains ( VarUsageInfoFlags :: VAR_INITIALIZED ) {
65
71
return ;
@@ -97,7 +103,7 @@ impl Optimizer<'_> {
97
103
98
104
// No use => dropped
99
105
if ref_count == 0 {
100
- self . mode . store ( ident . node_id , & * init) ;
106
+ self . mode . store ( node_id, & * init) ;
101
107
102
108
if init. may_have_side_effects ( self . ctx . expr_ctx ) {
103
109
// TODO: Inline partially
@@ -113,8 +119,6 @@ impl Optimizer<'_> {
113
119
114
120
let mut inlined_into_init = false ;
115
121
116
- let id = ident. node_id ;
117
-
118
122
// We inline arrays partially if it's pure (all elements are literal), and not
119
123
// modified.
120
124
// We don't drop definition, but we just inline array accesses with numeric
@@ -150,7 +154,7 @@ impl Optimizer<'_> {
150
154
) ;
151
155
self . vars
152
156
. lits_for_array_access
153
- . insert ( ident . node_id , Box :: new ( init. clone ( ) ) ) ;
157
+ . insert ( node_id, Box :: new ( init. clone ( ) ) ) ;
154
158
}
155
159
}
156
160
}
@@ -194,7 +198,7 @@ impl Optimizer<'_> {
194
198
}
195
199
196
200
if !usage. mutated ( ) {
197
- self . mode . store ( ident . node_id , & * init) ;
201
+ self . mode . store ( node_id, & * init) ;
198
202
}
199
203
200
204
if usage. flags . contains ( VarUsageInfoFlags :: USED_RECURSIVELY ) {
@@ -285,9 +289,7 @@ impl Optimizer<'_> {
285
289
{
286
290
true
287
291
} else {
288
- self . vars
289
- . lits_for_cmp
290
- . insert ( ident. node_id , init. clone ( ) . into ( ) ) ;
292
+ self . vars . lits_for_cmp . insert ( node_id, init. clone ( ) . into ( ) ) ;
291
293
false
292
294
}
293
295
}
@@ -317,7 +319,7 @@ impl Optimizer<'_> {
317
319
self . vars . inline_with_multi_replacer ( init, self . r ) ;
318
320
}
319
321
320
- self . mode . store ( id , & * init) ;
322
+ self . mode . store ( node_id , & * init) ;
321
323
322
324
let VarUsageInfo {
323
325
usage_count,
@@ -327,11 +329,7 @@ impl Optimizer<'_> {
327
329
} = * * usage;
328
330
let mut inc_usage = || {
329
331
if let Expr :: Ident ( i) = & * init {
330
- let node_id = match self . r . find_binding_by_ident ( i) {
331
- RefTo :: Binding ( node_id) => node_id,
332
- RefTo :: Unresolved => return ,
333
- RefTo :: Itself => unreachable ! ( ) ,
334
- } ;
332
+ debug_assert ! ( matches!( self . r. find_binding_by_ident( i) , RefTo :: Binding ( _) ) ) ;
335
333
debug_assert ! ( i. node_id != node_id) ;
336
334
if let Some ( u) = self . data . vars . get_mut ( & node_id) {
337
335
u. flags |= flags & VarUsageInfoFlags :: USED_AS_ARG ;
@@ -373,7 +371,7 @@ impl Optimizer<'_> {
373
371
374
372
inc_usage ( ) ;
375
373
376
- self . vars . lits . insert ( id , init. take ( ) . into ( ) ) ;
374
+ self . vars . lits . insert ( node_id , init. take ( ) . into ( ) ) ;
377
375
378
376
ident. take ( ) ;
379
377
} else if self . options . inline != 0 || self . options . reduce_vars {
@@ -383,15 +381,15 @@ impl Optimizer<'_> {
383
381
ident. ctxt
384
382
) ;
385
383
386
- self . mode . store ( id , & * init) ;
384
+ self . mode . store ( node_id , & * init) ;
387
385
388
386
inc_usage ( ) ;
389
387
390
- self . vars . lits . insert ( id , init. clone ( ) . into ( ) ) ;
388
+ self . vars . lits . insert ( node_id , init. clone ( ) . into ( ) ) ;
391
389
}
392
390
}
393
391
394
- let usage = self . data . vars . get ( & id ) . unwrap ( ) ;
392
+ let usage = self . data . vars . get ( & node_id ) . unwrap ( ) ;
395
393
396
394
// Single use => inlined
397
395
if !self . ctx . bit_ctx . contains ( BitCtx :: IsExported )
@@ -500,7 +498,12 @@ impl Optimizer<'_> {
500
498
return ;
501
499
}
502
500
503
- if let Some ( init_usage) = self . data . vars . get ( & id. node_id ) {
501
+ let id_node_id = match self . r . find_binding_by_ident ( id) {
502
+ RefTo :: Binding ( node_id) => node_id,
503
+ RefTo :: Itself => unreachable ! ( ) ,
504
+ RefTo :: Unresolved => return ,
505
+ } ;
506
+ if let Some ( init_usage) = self . data . vars . get ( & id_node_id) {
504
507
if init_usage. flags . contains ( VarUsageInfoFlags :: REASSIGNED )
505
508
|| !init_usage. flags . contains ( VarUsageInfoFlags :: DECLARED )
506
509
{
@@ -534,9 +537,12 @@ impl Optimizer<'_> {
534
537
_ => {
535
538
for id in idents_used_by ( init) {
536
539
let node_id = match self . r . find_binding_by_node_id ( id) {
537
- RefTo :: Binding ( node_id) => node_id,
540
+ RefTo :: Binding ( node_id) => {
541
+ debug_assert ! ( id != node_id) ;
542
+ node_id
543
+ }
544
+ RefTo :: Itself => id,
538
545
RefTo :: Unresolved => continue ,
539
- RefTo :: Itself => unreachable ! ( ) ,
540
546
} ;
541
547
if let Some ( v_usage) = self . data . vars . get ( & node_id) {
542
548
if v_usage. property_mutation_count > usage. property_mutation_count
@@ -664,6 +670,7 @@ impl Optimizer<'_> {
664
670
return ;
665
671
}
666
672
673
+ debug_assert_eq ! ( self . r. find_binding_by_ident( & i) , RefTo :: Itself ) ;
667
674
if let Some ( usage) = self . data . vars . get ( & i. node_id ) {
668
675
if !usage. flags . contains ( VarUsageInfoFlags :: REASSIGNED ) {
669
676
trace_op ! ( "typeofs: Storing typeof `{}{:?}`" , i. sym, i. ctxt) ;
@@ -724,6 +731,7 @@ impl Optimizer<'_> {
724
731
return ;
725
732
}
726
733
734
+ debug_assert_eq ! ( self . r. find_binding_by_ident( & i) , RefTo :: Itself ) ;
727
735
let id = i. node_id ;
728
736
729
737
if let Some ( usage) = self . data . vars . get ( & id) {
@@ -792,7 +800,7 @@ impl Optimizer<'_> {
792
800
}
793
801
794
802
self . vars . simple_functions . insert (
795
- i . node_id ,
803
+ id ,
796
804
FnExpr {
797
805
ident : None ,
798
806
function : f. function . clone ( ) ,
@@ -888,7 +896,7 @@ impl Optimizer<'_> {
888
896
}
889
897
} ;
890
898
891
- self . vars . vars_for_inlining . insert ( i . node_id , e) ;
899
+ self . vars . vars_for_inlining . insert ( id , e) ;
892
900
} else {
893
901
log_abort ! ( "inline: [x] Usage: {:?}" , usage) ;
894
902
}
@@ -906,7 +914,16 @@ impl Optimizer<'_> {
906
914
if let MemberProp :: Computed ( prop) = & mut me. prop {
907
915
if let Expr :: Lit ( Lit :: Num ( ..) ) = & * prop. expr {
908
916
if let Expr :: Ident ( obj) = & * me. obj {
909
- let new = self . vars . lits_for_array_access . get ( & obj. node_id ) ;
917
+ let node_id = match self . r . find_binding_by_ident ( obj) {
918
+ RefTo :: Unresolved => None ,
919
+ RefTo :: Binding ( node_id) => Some ( node_id) ,
920
+ RefTo :: Itself => unreachable ! ( ) ,
921
+ } ;
922
+
923
+ let new = node_id. and_then ( |node_id| {
924
+ debug_assert ! ( node_id != obj. node_id) ;
925
+ self . vars . lits_for_array_access . get ( & node_id)
926
+ } ) ;
910
927
911
928
if let Some ( new) = new {
912
929
report_change ! ( "inline: Inlined array access" ) ;
@@ -924,6 +941,9 @@ impl Optimizer<'_> {
924
941
RefTo :: Unresolved => return ,
925
942
RefTo :: Itself => unreachable ! ( ) ,
926
943
} ;
944
+ debug_assert ! ( i. node_id != node_id) ;
945
+ debug_assert ! ( i. node_id != NodeId :: DUMMY ) ;
946
+ debug_assert ! ( node_id != NodeId :: DUMMY ) ;
927
947
928
948
if let Some ( mut value) = self
929
949
. vars
@@ -946,7 +966,7 @@ impl Optimizer<'_> {
946
966
947
967
// currently renamer relies on the fact no distinct var has same ctxt, we need
948
968
// to remap all new bindings.
949
- // let bindings: FxHashSet<NodeId> = collect_decls(&*value);
969
+ let bindings: FxHashSet < NodeId > = collect_decls ( & * value) ;
950
970
// let new_mark = Mark::new();
951
971
// let mut cache = FxHashMap::default();
952
972
// let mut remap = FxHashMap::default();
@@ -992,7 +1012,7 @@ impl Optimizer<'_> {
992
1012
}
993
1013
}
994
1014
995
- if let Some ( value) = self . vars . vars_for_inlining . remove ( & i . node_id ) {
1015
+ if let Some ( value) = self . vars . vars_for_inlining . remove ( & node_id) {
996
1016
self . changed = true ;
997
1017
report_change ! ( "inline: Replacing '{}' with an expression" , i) ;
998
1018
0 commit comments