Skip to content

Commit

Permalink
Consider while (true) {...} loops as infinite loops.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=126325051
  • Loading branch information
blickly committed Jun 30, 2016
1 parent c874409 commit 12bcd8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/com/google/javascript/jscomp/ControlFlowAnalysis.java
Expand Up @@ -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));
}
Expand Down
Expand Up @@ -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;",
Expand All @@ -365,15 +365,15 @@ public void testForLoopInference() {

typeCheck(LINE_JOINER.join(
"var x = 5;",
"while (true) {",
"while (x < 10) {",
" x = 'str';",
"}",
"(function(/** string */ s){})(x);",
"var /** (string|number) */ y = x;"),
NewTypeInference.INVALID_ARGUMENT_TYPE);

typeCheck(LINE_JOINER.join(
"while (true) {",
"while (true || false) {",
" var x = 'str';",
"}",
"var /** (string|undefined) */ y = x;",
Expand Down

0 comments on commit 12bcd8b

Please sign in to comment.