Skip to content

Commit 4fbcc71

Browse files
rishipalcopybara-github
authored andcommitted
Change PeepholeRemoveDeadCode to stop asserting that the AST is normalized.
The pass runs both before and after denormalize. So it’s a bug that the pass unconditionally asserts that the AST is normalized. This CL deletes the check. Alternatively, even just guarding the check with `isASTNormalized` would works - http://sponge2/c65113df-4f96-4148-9400-6f551562349c ``` if (!isASTNormalized()) { // the pass is running after denormalize return subtree; } throw checkNormalization(false, "WHILE"); ``` PiperOrigin-RevId: 561471939
1 parent 334e0b6 commit 4fbcc71

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/com/google/javascript/jscomp/PeepholeRemoveDeadCode.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ Node optimizeSubtree(Node subtree) {
5858
case IF:
5959
return tryFoldIf(subtree);
6060
case WHILE:
61-
throw checkNormalization(false, "WHILE");
61+
// This pass gets run both before and after denormalize. Hence, the AST could potentially
62+
// contain WHILE (denormalized).
63+
// TODO: Ideally, we should optimize this case instead of returning
64+
return subtree;
6265
case FOR:
6366
{
6467
Node condition = NodeUtil.getConditionExpression(subtree);
@@ -787,7 +790,6 @@ Node tryOptimizeBlock(Node n) {
787790
for (Node c = n.getFirstChild(); c != null; ) {
788791
Node next = c.getNext(); // save c.next, since 'c' may be removed
789792
if (!isUnremovableNode(c) && !mayHaveSideEffects(c)) {
790-
checkNormalization(!NodeUtil.isFunctionDeclaration(n), "function declaration");
791793
// TODO(johnlenz): determine what this is actually removing. Candidates
792794
// include: EMPTY nodes, control structures without children
793795
// (removing infinite loops), empty try blocks. What else?
@@ -1377,10 +1379,4 @@ private Node tryRemoveOptionalCall(Node optionalCall) {
13771379
this.reportChangeToEnclosingScope(result);
13781380
return result;
13791381
}
1380-
1381-
private static @Nullable IllegalStateException checkNormalization(
1382-
boolean condition, String feature) {
1383-
checkState(condition, "Unexpected %s. AST should be normalized.", feature);
1384-
return null;
1385-
}
13861382
}

test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public void process(Node externs, Node root) {
5555
@Before
5656
public void setUp() throws Exception {
5757
super.setUp();
58+
// This pass doesn't need the AST to be normalized as it runs both before and after
59+
// `denormalize`. Since it runs PureFunctionsIdentifier pass which expects the AST to be
60+
// normalized, we're doing `enableNormalize` for these tests.
5861
enableNormalize();
5962
// TODO(bradfordcsmith): Stop normalizing the expected output or document why it is necessary.
6063
enableNormalizeExpectedOutput();

0 commit comments

Comments
 (0)