From 12bcd8b60332e486223e4033cdf3e3a78195d5b2 Mon Sep 17 00:00:00 2001 From: blickly Date: Thu, 30 Jun 2016 11:37:07 -0700 Subject: [PATCH] Consider `while (true) {...}` loops as infinite loops. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=126325051 --- .../jscomp/ControlFlowAnalysis.java | 21 +++++++++++-------- .../NewTypeInferenceES5OrLowerTest.java | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/com/google/javascript/jscomp/ControlFlowAnalysis.java b/src/com/google/javascript/jscomp/ControlFlowAnalysis.java index ae261e67379..543315faf2d 100644 --- a/src/com/google/javascript/jscomp/ControlFlowAnalysis.java +++ b/src/com/google/javascript/jscomp/ControlFlowAnalysis.java @@ -373,24 +373,27 @@ private void handleIf(Node node) { } private void handleWhile(Node node) { + Node cond = node.getFirstChild(); // Control goes to the first statement if the condition evaluates to true. - createEdge(node, Branch.ON_TRUE, - computeFallThrough(node.getSecondChild())); + createEdge(node, Branch.ON_TRUE, computeFallThrough(cond.getNext())); - // Control goes to the follow() if the condition evaluates to false. - createEdge(node, Branch.ON_FALSE, - computeFollowNode(node, this)); + if (!cond.isTrue()) { + // Control goes to the follow() if the condition evaluates to false. + createEdge(node, Branch.ON_FALSE, computeFollowNode(node, this)); + } connectToPossibleExceptionHandler( node, NodeUtil.getConditionExpression(node)); } private void handleDo(Node node) { + Node cond = node.getFirstChild(); // The first edge can be the initial iteration as well as the iterations // after. - createEdge(node, Branch.ON_TRUE, computeFallThrough(node.getFirstChild())); - // The edge that leaves the do loop if the condition fails. - createEdge(node, Branch.ON_FALSE, - computeFollowNode(node, this)); + createEdge(node, Branch.ON_TRUE, computeFallThrough(cond)); + if (!cond.isTrue()) { + // The edge that leaves the do loop if the condition fails. + createEdge(node, Branch.ON_FALSE, computeFollowNode(node, this)); + } connectToPossibleExceptionHandler( node, NodeUtil.getConditionExpression(node)); } diff --git a/test/com/google/javascript/jscomp/NewTypeInferenceES5OrLowerTest.java b/test/com/google/javascript/jscomp/NewTypeInferenceES5OrLowerTest.java index 609f08bfc3d..f2654b5141d 100644 --- a/test/com/google/javascript/jscomp/NewTypeInferenceES5OrLowerTest.java +++ b/test/com/google/javascript/jscomp/NewTypeInferenceES5OrLowerTest.java @@ -356,7 +356,7 @@ public void testEmptyBlockPropagation() { public void testForLoopInference() { typeCheck(LINE_JOINER.join( "var x = 5;", - "for (;true;) {", + "for (;x < 10;) {", " x = 'str';", "}", "var /** (string|number) */ y = x;", @@ -365,7 +365,7 @@ public void testForLoopInference() { typeCheck(LINE_JOINER.join( "var x = 5;", - "while (true) {", + "while (x < 10) {", " x = 'str';", "}", "(function(/** string */ s){})(x);", @@ -373,7 +373,7 @@ public void testForLoopInference() { NewTypeInference.INVALID_ARGUMENT_TYPE); typeCheck(LINE_JOINER.join( - "while (true) {", + "while (true || false) {", " var x = 'str';", "}", "var /** (string|undefined) */ y = x;",