-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FlowSensitiveInlineVariablesTest.java
882 lines (749 loc) · 27 KB
/
FlowSensitiveInlineVariablesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
* Copyright 2009 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.javascript.rhino.Node;
/**
* Unit tests for {@link FlowSensitiveInlineVariables}.
*
*/
public final class FlowSensitiveInlineVariablesTest extends CompilerTestCase {
public static final String EXTERN_FUNCTIONS = lines(
"var print;",
"/** @nosideeffects */ function noSFX() {}",
" function hasSFX() {}");
@Override
protected void setUp() throws Exception {
super.setUp();
enableNormalize();
}
@Override
protected int getNumRepetitions() {
// Test repeatedly inline.
return 3;
}
@Override
protected CompilerPass getProcessor(final Compiler compiler) {
//return new FlowSensitiveInlineVariables(compiler);
return new CompilerPass() {
@Override
public void process(Node externs, Node root) {
(new MarkNoSideEffectCalls(compiler)).process(externs, root);
(new FlowSensitiveInlineVariables(compiler)).process(externs, root);
}
};
}
public void testSimpleAssign() {
inline("var x; x = 1; print(x)", "var x; print(1)");
inline("var x; x = 1; x", "var x; 1");
inline("var x; x = 1; var a = x", "var x; var a = 1");
inline("var x; x = 1; x = x + 1", "var x; x = 1 + 1");
}
public void testSimpleVar() {
inline("var x = 1; print(x)", "var x; print(1)");
inline("var x = 1; x", "var x; 1");
inline("var x = 1; var a = x", "var x; var a = 1");
inline("var x = 1; x = x + 1", "var x; x = 1 + 1");
}
public void testSimpleLet() {
inline("let x = 1; print(x)", "let x; print(1)");
inline("let x = 1; x", "let x; 1");
inline("let x = 1; let a = x", "let x; let a = 1");
inline("let x = 1; x = x + 1", "let x; x = 1 + 1");
}
public void testSimpleConst() {
inline("const x = 1; print(x)", "const x = undefined; print(1)");
inline("const x = 1; x", "const x = undefined; 1");
inline("const x = 1; const a = x", "const x = undefined; const a = 1");
}
public void testSimpleForIn() {
inline("var a,b,x = a in b; x",
"var a,b,x; a in b");
noInline("var a, b; var x = a in b; print(1); x");
noInline("var a,b,x = a in b; delete a[b]; x");
}
public void testExported() {
noInline("var _x = 1; print(_x)");
}
public void testDoNotInlineIncrement() {
noInline("var x = 1; x++;");
noInline("var x = 1; x--;");
}
public void testMultiUse() {
noInline("var x; x = 1; print(x); print (x);");
}
public void testMultiUseInSameCfgNode() {
noInline("var x; x = 1; print(x) || print (x);");
}
public void testMultiUseInTwoDifferentPath() {
noInline("var x = 1; if (print) { print(x) } else { alert(x) }");
}
public void testAssignmentBeforeDefinition() {
inline("x = 1; var x = 0; print(x)","x = 1; var x; print(0)" );
}
public void testVarInConditionPath() {
noInline("if (foo) { var x = 0 } print(x)");
}
public void testMultiDefinitionsBeforeUse() {
inline("var x = 0; x = 1; print(x)", "var x = 0; print(1)");
}
public void testMultiDefinitionsInSameCfgNode() {
noInline("var x; (x = 1) || (x = 2); print(x)");
noInline("var x; x = (1 || (x = 2)); print(x)");
noInline("var x;(x = 1) && (x = 2); print(x)");
noInline("var x;x = (1 && (x = 2)); print(x)");
noInline("var x; x = 1 , x = 2; print(x)");
}
public void testNotReachingDefinitions() {
noInline("var x; if (foo) { x = 0 } print (x)");
}
public void testNoInlineLoopCarriedDefinition() {
// First print is undefined instead.
noInline("var x; while(true) { print(x); x = 1; }");
// Prints 0 1 1 1 1....
noInline("var x = 0; while(true) { print(x); x = 1; }");
}
public void testDoNotExitLoop() {
noInline("while (z) { var x = 3; } var y = x;");
}
public void testDoNotInlineWithinLoop() {
noInline("var y = noSFX(); do { var z = y.foo(); } while (true);");
}
public void testDoNotInlineCatchExpression1() {
noInline(
"var a;\n" +
"try {\n" +
" throw Error(\"\");\n" +
"}catch(err) {" +
" a = err;\n" +
"}\n" +
"return a.stack\n");
}
public void testDoNotInlineCatchExpression1a() {
noInline(
"var a;\n" +
"try {\n" +
" throw Error(\"\");\n" +
"}catch(err) {" +
" a = err + 1;\n" +
"}\n" +
"return a.stack\n");
}
public void testDoNotInlineCatchExpression2() {
noInline(
"var a;\n" +
"try {\n" +
" if (x) {throw Error(\"\");}\n" +
"}catch(err) {" +
" a = err;\n" +
"}\n" +
"return a.stack\n");
}
public void testDoNotInlineCatchExpression3() {
noInline(
"var a;\n" +
"try {\n" +
" throw Error(\"\");\n" +
"} catch(err) {" +
" err = x;\n" +
" a = err;\n" +
"}\n" +
"return a.stack\n");
}
public void testDoNotInlineCatchExpression4() {
// Note: it is valid to inline "x" here but we currently don't.
noInline(
"try {\n" +
" stuff();\n" +
"} catch (e) {\n" +
" x = e;\n" +
" print(x);\n" +
"}");
}
public void testDefinitionAfterUse() {
inline("var x = 0; print(x); x = 1", "var x; print(0); x = 1");
}
public void testInlineSameVariableInStraightLine() {
inline("var x; x = 1; print(x); x = 2; print(x)",
"var x; print(1); print(2)");
}
public void testInlineInDifferentPaths() {
inline("var x; if (print) {x = 1; print(x)} else {x = 2; print(x)}",
"var x; if (print) {print(1)} else {print(2)}");
}
public void testNoInlineInMergedPath() {
noInline(
"var x,y;x = 1;while(y) { if(y){ print(x) } else { x = 1 } } print(x)");
}
public void testInlineIntoExpressions() {
inline("var x = 1; print(x + 1);", "var x; print(1 + 1)");
}
public void testInlineExpressions1() {
inline("var a, b; var x = a+b; print(x)", "var a, b; var x; print(a+b)");
}
public void testInlineExpressions2() {
// We can't inline because of the redefinition of "a".
noInline("var a, b; var x = a + b; a = 1; print(x)");
}
public void testInlineExpressions3() {
inline("var a,b,x; x=a+b; x=a-b ; print(x)",
"var a,b,x; x=a+b; print(a-b)");
}
public void testInlineExpressions4() {
// Precision is lost due to comma's.
noInline("var a,b,x; x=a+b, x=a-b; print(x)");
}
public void testInlineExpressions5() {
noInline("var a; var x = a = 1; print(x)");
}
public void testInlineExpressions6() {
noInline("var a, x; a = 1 + (x = 1); print(x)");
}
public void testInlineExpression7() {
// Possible side effects in foo() that might conflict with bar();
noInline("var x = foo() + 1; bar(); print(x)");
// This is a possible case but we don't have analysis to prove this yet.
// TODO(user): It is possible to cover this case with the same algorithm
// as the missing return check.
noInline("var x = foo() + 1; print(x)");
}
public void testInlineExpression8() {
// The same variable inlined twice.
inline(
"var a,b;" +
"var x = a + b; print(x); x = a - b; print(x)",
"var a,b;" +
"var x; print(a + b); print(a - b)");
}
public void testInlineExpression9() {
// Check for actual control flow sensitivity.
inline(
"var a,b;" +
"var x; if (g) { x= a + b; print(x) } x = a - b; print(x)",
"var a,b;" +
"var x; if (g) { print(a + b)} print(a - b)");
}
public void testInlineExpression10() {
// The DFA is not fine grain enough for this.
noInline("var x, y; x = ((y = 1), print(y))");
}
public void testInlineExpressions11() {
inline("var x; x = x + 1; print(x)", "var x; print(x + 1)");
noInline("var x; x = x + 1; print(x); print(x)");
}
public void testInlineExpressions12() {
// ++ is an assignment and considered to modify state so it will not be
// inlined.
noInline("var x = 10; x = c++; print(x)");
}
public void testInlineExpressions13() {
inline("var a = 1, b = 2;" +
"var x = a;" +
"var y = b;" +
"var z = x + y;" +
"var i = z;" +
"var j = z + y;" +
"var k = i;",
"var a, b;" +
"var x;" +
"var y = 2;" +
"var z = 1 + y;" +
"var i;" +
"var j = z + y;" +
"var k = z;");
}
public void testNoInlineIfDefinitionMayNotReach() {
noInline("var x; if (x=1) {} x;");
}
public void testNoInlineEscapedToInnerFunction() {
noInline("var x = 1; function foo() { x = 2 }; print(x)");
}
public void testNoInlineLValue() {
noInline("var x; if (x = 1) { print(x) }");
}
public void testSwitchCase() {
inline("var x = 1; switch(x) { }", "var x; switch(1) { }");
}
public void testShadowedVariableInnerFunction() {
inline("var x = 1; print(x) || (function() { var x; x = 1; print(x)})()",
"var x; print(1) || (function() { var x; print(1)})()");
}
public void testCatch() {
noInline("var x = 0; try { } catch (x) { }");
noInline("try { } catch (x) { print(x) }");
}
public void testNoInlineGetProp() {
// We don't know if j alias a.b
noInline("var x = a.b.c; j.c = 1; print(x);");
}
public void testNoInlineGetProp2() {
noInline("var x = 1 * a.b.c; j.c = 1; print(x);");
}
public void testNoInlineGetProp3() {
// Anything inside a function is fine.
inline("var x = function(){1 * a.b.c}; print(x);",
"var x; print(function(){1 * a.b.c});");
}
public void testNoInlineGetEle() {
// Again we don't know if i = j
noInline("var x = a[i]; a[j] = 2; print(x); ");
}
// TODO(user): These should be inlinable.
public void testNoInlineConstructors() {
noInline("var x = new Iterator(); x.next();");
}
// TODO(user): These should be inlinable.
public void testNoInlineArrayLits() {
noInline("var x = []; print(x)");
}
// TODO(user): These should be inlinable.
public void testNoInlineObjectLits() {
noInline("var x = {}; print(x)");
}
// TODO(user): These should be inlinable after the REGEX checks.
public void testNoInlineRegExpLits() {
noInline("var x = /y/; print(x)");
}
public void testInlineConstructorCallsIntoLoop() {
// Don't inline construction into loops.
noInline("var x = new Iterator();" +
"for(i = 0; i < 10; i++) {j = x.next()}");
}
public void testRemoveWithLabels() {
inline("var x = 1; L: x = 2; print(x)", "var x = 1; L:{} print(2)");
inline("var x = 1; L: M: x = 2; print(x)", "var x = 1; L:M:{} print(2)");
inline("var x = 1; L: M: N: x = 2; print(x)",
"var x = 1; L:M:N:{} print(2)");
}
public void testInlineAcrossSideEffect1() {
// This can't be inlined because print() has side-effects and might change
// the definition of noSFX.
//
// noSFX must be both const and pure in order to inline it.
noInline("var y; var x = noSFX(y); print(x)");
//inline("var y; var x = noSFX(y); print(x)", "var y;var x;print(noSFX(y))");
}
public void testInlineAcrossSideEffect2() {
// Think noSFX() as a function that reads y.foo and return it
// and SFX() write some new value of y.foo. If that's the case,
// inlining across hasSFX() is not valid.
// This is a case where hasSFX is right of the source of the inlining.
noInline("var y; var x = noSFX(y), z = hasSFX(y); print(x)");
noInline("var y; var x = noSFX(y), z = new hasSFX(y); print(x)");
noInline("var y; var x = new noSFX(y), z = new hasSFX(y); print(x)");
}
public void testInlineAcrossSideEffect3() {
// This is a case where hasSFX is left of the destination of the inlining.
noInline("var y; var x = noSFX(y); hasSFX(y), print(x)");
noInline("var y; var x = noSFX(y); new hasSFX(y), print(x)");
noInline("var y; var x = new noSFX(y); new hasSFX(y), print(x)");
}
public void testInlineAcrossSideEffect4() {
// This is a case where hasSFX is some control flow path between the
// source and its destination.
noInline("var y; var x = noSFX(y); hasSFX(y); print(x)");
noInline("var y; var x = noSFX(y); new hasSFX(y); print(x)");
noInline("var y; var x = new noSFX(y); new hasSFX(y); print(x)");
}
public void testCanInlineAcrossNoSideEffect() {
// This can't be inlined because print() has side-effects and might change
// the definition of noSFX. We should be able to mark noSFX as const
// in some way.
noInline(
"var y; var x = noSFX(y), z = noSFX(); noSFX(); noSFX(), print(x)");
//inline(
// "var y; var x = noSFX(y), z = noSFX(); noSFX(); noSFX(), print(x)",
// "var y; var x, z = noSFX(); noSFX(); noSFX(), print(noSFX(y))");
}
public void testDependOnOuterScopeVariables() {
noInline("var x; function foo() { var y = x; x = 0; print(y) }");
noInline("var x; function foo() { var y = x; x++; print(y) }");
// Sadly, we don't understand the data flow of outer scoped variables as
// it can be modified by code outside of this scope. We can't inline
// at all if the definition has dependence on such variable.
noInline("var x; function foo() { var y = x; print(y) }");
}
public void testInlineIfNameIsLeftSideOfAssign() {
inline("var x = 1; x = print(x) + 1", "var x; x = print(1) + 1");
inline("var x = 1; L: x = x + 2", "var x; L: x = 1 + 2");
inline("var x = 1; x = (x = x + 1)", "var x; x = (x = 1 + 1)");
noInline("var x = 1; x = (x = (x = 10) + x)");
noInline("var x = 1; x = (f(x) + (x = 10) + x);");
noInline("var x = 1; x=-1,foo(x)");
noInline("var x = 1; x-=1,foo(x)");
}
public void testInlineArguments() {
testSame("function _func(x) { print(x) }");
testSame("function _func(x,y) { if(y) { x = 1 }; print(x) }");
test("function f(x, y) { x = 1; print(x) }",
"function f(x, y) { print(1) }");
test("function f(x, y) { if (y) { x = 1; print(x) }}",
"function f(x, y) { if (y) { print(1) }}");
}
public void testInvalidInlineArguments1() {
testSame("function f(x, y) { x = 1; arguments[0] = 2; print(x) }");
testSame("function f(x, y) { x = 1; var z = arguments;" +
"z[0] = 2; z[1] = 3; print(x)}");
testSame("function g(a){a[0]=2} function f(x){x=1;g(arguments);print(x)}");
}
public void testInvalidInlineArguments2() {
testSame("function f(c) {var f = c; arguments[0] = this;" +
"f.apply(this, arguments); return this;}");
}
public void testForIn() {
noInline("var x; var y = {}; for(x in y){}");
noInline("var x; var y = {}; var z; for(x in z = y){print(z)}");
noInline("var x; var y = {}; var z; for(x in y){print(z)}");
}
public void testForInDestructuring() {
noInline("var x = 1, y = [], z; for ({z = x} in y) {}");
noInline("var x = 1, y = [], z; for ([z = x] in y) {}");
noInline("var x = 1, y = [], z; print(x); for ({z = x} in y) {}");
noInline("var x = 1, y = [], z; print(x); for ([z = x] in y) {}");
noInline("var x = 1, y = [], z; print(x); for (let {z = x} in y) {}");
noInline("var x = 1, y = [], z; print(x); for (const {z = x} in y) {}");
}
public void testNotOkToSkipCheckPathBetweenNodes() {
noInline("var x; for(x = 1; foo(x);) {}");
noInline("var x; for(; x = 1;foo(x)) {}");
}
public void testIssue698() {
// Most of the flow algorithms operate on Vars. We want to make
// sure the algorithm bails out appropriately if it sees
// a var that it doesn't know about.
inline(
"var x = ''; "
+ "unknown.length < 2 && (unknown='0' + unknown);"
+ "x = x + unknown; "
+ "unknown.length < 3 && (unknown='0' + unknown);"
+ "x = x + unknown; "
+ "return x;",
"var x; "
+ "unknown.length < 2 && (unknown='0' + unknown);"
+ "x = '' + unknown; "
+ "unknown.length < 3 && (unknown='0' + unknown);"
+ "x = x + unknown; "
+ "return x;");
}
public void testIssue777() {
test(
"function f(cmd, ta) {" +
" var temp = cmd;" +
" var temp2 = temp >> 2;" +
" cmd = STACKTOP;" +
" for (var src = temp2, dest = cmd >> 2, stop = src + 37;" +
" src < stop;" +
" src++, dest++) {" +
" HEAP32[dest] = HEAP32[src];" +
" }" +
" temp = ta;" +
" temp2 = temp >> 2;" +
" ta = STACKTOP;" +
" STACKTOP += 8;" +
" HEAP32[ta >> 2] = HEAP32[temp2];" +
" HEAP32[ta + 4 >> 2] = HEAP32[temp2 + 1];" +
"}",
"function f(cmd, ta){" +
" var temp;" +
" var temp2 = cmd >> 2;" +
" cmd = STACKTOP;" +
" var src = temp2;" +
" var dest = cmd >> 2;" +
" var stop = src + 37;" +
" for(;src<stop;src++,dest++)HEAP32[dest]=HEAP32[src];" +
" temp2 = ta >> 2;" +
" ta = STACKTOP;" +
" STACKTOP += 8;" +
" HEAP32[ta>>2] = HEAP32[temp2];" +
" HEAP32[ta+4>>2] = HEAP32[temp2+1];" +
"}");
}
public void testTransitiveDependencies1() {
test(
"function f(x) { var a = x; var b = a; x = 3; return b; }",
"function f(x) { var a; var b = x; x = 3; return b; }");
}
public void testTransitiveDependencies2() {
test(
"function f(x) { var a = x; var b = a; var c = b; x = 3; return c; }",
"function f(x) { var a ; var b = x; var c ; x = 3; return b; }");
}
public void testIssue794a() {
noInline(
"var x = 1; " +
"try { x += someFunction(); } catch (e) {}" +
"x += 1;" +
"try { x += someFunction(); } catch (e) {}" +
"return x;");
}
public void testIssue794b() {
noInline(
"var x = 1; " +
"try { x = x + someFunction(); } catch (e) {}" +
"x = x + 1;" +
"try { x = x + someFunction(); } catch (e) {}" +
"return x;");
}
public void testVarAssignInsideHookIssue965() {
noInline("var i = 0; return 1 ? (i = 5) : 0, i;");
noInline("var i = 0; return (1 ? (i = 5) : 0) ? i : 0;");
noInline("var i = 0; return (1 ? (i = 5) : 0) || i;");
noInline("var i = 0; return (1 ? (i = 5) : 0) * i;");
}
// GitHub issue #250: https://github.com/google/closure-compiler/issues/250
public void testInlineStringConcat() {
test(lines(
"function f() {",
" var x = '';",
" x = x + '1';",
" x = x + '2';",
" x = x + '3';",
" x = x + '4';",
" x = x + '5';",
" x = x + '6';",
" x = x + '7';",
" return x;",
"}"),
"function f() { var x; return '' + '1' + '2' + '3' + '4' + '5' + '6' + '7'; }");
}
public void testInlineInArrowFunctions() {
test("() => {var v; v = 1; return v;} ",
"() => {var v; return 1;}");
test("(v) => {v = 1; return v;}",
"(v) => {return 1;}");
}
public void testInlineInClassMemberFunctions() {
test(
lines(
"class C {",
" func() {",
" var x;",
" x = 1;",
" return x;",
" }",
"}"
),
lines(
"class C {",
" func() {",
" var x;",
" return 1;",
" }",
"}"
)
);
}
public void testInlineLet() {
inline("let a = 1; print(a + 1)",
"let a; print(1 + 1)");
inline("let a; a = 1; print(a + 1)",
"let a; print(1 + 1)");
noInline("let a = noSFX(); print(a)");
}
public void testInlineConst() {
inline("const a = 1; print(a + 1)",
"const a = undefined; print(1 + 1)");
inline("const a = 1; const b = a; print(b + 1)",
"const a = undefined; const b = undefined; print(1 + 1)");
noInline("const a = noSFX(); print(a)");
}
public void testSpecific() {
inline("let a = 1; print(a + 1)",
"let a; print(1 + 1)");
}
public void testBlockScoping() {
inline(
lines(
"let a = 1",
"print(a + 1);",
"{",
" let b = 2;",
" print(b + 1);",
"}"
),
lines(
"let a;",
"print(1 + 1);",
"{",
" let b;",
" print(2 + 1);",
"}"));
inline(
lines(
"let a = 1",
"{",
" let a = 2;",
" print(a + 1);",
"}",
"print(a + 1);"
),
lines(
"let a = 1",
"{",
" let a;",
" print(2 + 1);",
"}",
"print(a + 1);"));
inline(
lines(
"let a = 1;",
" {let b;}",
"print(a)"
),
lines(
"let a;",
" {let b;}",
"print(1)"));
// This test fails to inline due to CheckPathsBetweenNodes analysis in the canInline function
// in FlowSensitiveInlineVariables.
noInline(
lines(
"let a = 1;",
"{",
" let b;",
" f(b);",
"}",
"return(a)"));
}
public void testBlockScoping_shouldntInline() {
// TODO(b/73123594): fix this test. It adds a reference to a block-scoped let outside its block.
inline(
lines(
"var JSCompiler_inline_result;",
"{",
" let a = 1;",
" if (3 < 4) {",
" a = 2;",
" }",
" JSCompiler_inline_result = a;",
"}",
"alert(JSCompiler_inline_result);"),
lines(
"var JSCompiler_inline_result;",
"{",
" let a = 1;",
" if (3 < 4) {",
" a = 2;",
" }",
"}",
"alert(a);")); // a is not defined here! This is wrong.
}
public void testInlineInGenerators() {
test(
lines(
"function* f() {",
" var x = 1;",
" return x + 1;",
"}"
),
lines(
"function* f() {",
" var x;",
" return 1 + 1;",
"}"
)
);
}
public void testNoInlineForOf() {
noInline("for (var x of n){} ");
noInline("var x = 1; var n = {}; for(x of n) {}");
}
public void testForOfDestructuring() {
noInline("var x = 1, y = [], z; for ({z = x} of y) {}");
noInline("var x = 1, y = [], z; for ([z = x] of y) {}");
noInline("var x = 1, y = [], z; print(x); for ({z = x} of y) {}");
noInline("var x = 1, y = [], z; print(x); for ([z = x] of y) {}");
noInline("var x = 1, y = [], z; print(x); for (let [z = x] of y) {}");
noInline("var x = 1, y = [], z; print(x); for (const [z = x] of y) {}");
}
public void testTemplateStrings() {
inline("var name = 'Foo'; `Hello ${name}`",
"var name; `Hello ${'Foo'}`");
inline("var name = 'Foo'; var foo = name; `Hello ${foo}`",
"var name; var foo; `Hello ${'Foo'}`");
inline(" var age = 3; `Age: ${age}`",
"var age; `Age: ${3}`");
}
public void testArrayDestructuring() {
noInline("var [a, b, c] = [1, 2, 3]; print(a + b + c);");
noInline("var arr = [1, 2, 3, 4]; var [a, b, ,d] = arr;");
noInline("var x = 3; [x] = 4; print(x);");
inline("var [x] = []; x = 3; print(x);", "var [x] = []; print(3);");
}
public void testObjectDestructuring() {
noInline("var {a, b} = {a: 3, b: 4}; print(a + b);");
noInline("var obj = {a: 3, b: 4}; var {a, b} = obj;");
}
public void testDestructuringDefaultValue() {
inline("var x = 1; var [y = x] = [];", "var x; var [y = 1] = [];");
inline("var x = 1; var {y = x} = {};", "var x; var {y = 1} = {};");
inline("var x = 1; var {[3]: y = x} = {};", "var x; var {[3]: y = 1} = {};");
noInline("var x = 1; var {[x]: y = x} = {};");
noInline("var x = 1; var [y = x] = []; print(x);");
// don't inline because x is only conditionally reassigned to 2.
noInline("var x = 1; var [y = (x = 2, 4)] = []; print(x);");
}
public void testDestructuringComputedProperty() {
inline("var x = 1; var {[x]: y} = {};", "var x; var {[1]: y} = {};");
noInline("var x = 1; var {[x]: y} = {}; print(x);");
}
public void testDeadAssignments() {
inline(
"let a = 3; if (3 < 4) { a = 8; } else { print(a); }",
"let a; if (3 < 4) { a = 8 } else { print(3); }");
inline(
"let a = 3; if (3 < 4) { [a] = 8; } else { print(a); }",
"let a; if (3 < 4) { [a] = 8 } else { print(3); }");
}
public void testDestructuringEvaluationOrder() {
// Should not inline "x = 2" in these cases because x is changed beforehand
noInline("var x = 2; var {x, y = x} = {x: 3};");
noInline("var x = 2; var {y = (x = 3), z = x} = {};");
// These examples are safe to inline, but FlowSensitiveInlineVariables never inlines variables
// used twice in the same CFG node even when safe to do so.
noInline("var x = 2; var {a: y = (x = 3)} = {a: x};");
noInline("var x = 1; var {a = x} = {a: (x = 2, 3)};");
noInline("var x = 2; var {a: x = 3} = {a: x};");
}
public void testDestructuringWithSideEffects() {
noInline("function f() { x++; } var x = 2; var {y = x} = {key: f()}");
noInline("function f() { x++; } var x = 2; var y = x; var {z = y} = {a: f()}");
noInline("function f() { x++; } var x = 2; var y = x; var {a = f(), b = y} = {}");
noInline("function f() { x++; } var x = 2; var y = x; var {[f()]: z = y} = {}");
noInline("function f() { x++; } var x = 2; var y = x; var {a: {b: z = y} = f()} = {};");
noInline("function f() { x++; } var x = 2; var y; var {z = (y = x, 3)} = {a: f()}; print(y);");
inline(
"function f() { x++; } var x = 2; var y = x; var {z = f()} = {a: y}",
"function f() { x++; } var x = 2; var y ; var {z = f()} = {a: x}");
inline(
"function f() { x++; } var x = 2; var y = x; var {a = y, b = f()} = {}",
"function f() { x++; } var x = 2; var y ; var {a = x, b = f()} = {}");
inline(
"function f() { x++; } var x = 2; var y = x; var {[y]: z = f()} = {}",
"function f() { x++; } var x = 2; var y ; var {[x]: z = f()} = {}");
inline(
"function f() { x++; } var x = 2; var y = x; var {a: {b: z = f()} = y} = {};",
"function f() { x++; } var x = 2; var y ; var {a: {b: z = f()} = x} = {};");
}
private void noInline(String input) {
inline(input, input);
}
private void inline(String input, String expected) {
test(
externs(EXTERN_FUNCTIONS),
srcs("function _func() {" + input + "}"),
expected("function _func() {" + expected + "}"));
}
}