Fix naming for `for` loops
commit3068f1b2ab3b4fe20f8079f53fc812e95a367bc8
authorFangyi Zhou <fangyizhou@fb.com>
Mon, 23 Apr 2018 13:09:20 +0000 (23 06:09 -0700)
committerHhvm Bot <hhvm-bot@users.noreply.github.com>
Mon, 23 Apr 2018 13:12:38 +0000 (23 06:12 -0700)
tree3d5a0ce62a1fc94a379be000a1c9bdaef17e71b1
parent9f302a6511e9f324c85c7f047b0382c180171098
Fix naming for `for` loops

Summary:
If a `for` loop is skipped, the iteration step is not executed. Had there been a definition of a new variable in the iteration step, it is not ensured to be assigned. An equivalent `while` loop will report errors of similar kind, which is intended behaviour. See examples below.

```
function foo(): void {
  for (; false; $x = 10) {}
  echo $x; // should error here
}
```
```
function bar(): void {
  while (false) {
    $x = 10;
  }
  echo $x; // error
}
```

In addition, the block should be named before the iteration.

```
function foo(): void {
  for (; true; $x = 10) {
    $y = $x; // should error here
  }
}
```
```
function bar(): void {
  while (true) {
    $y = $x; // error
    $x = 10;
  }
}
```

Reviewed By: CatherineGasnier

Differential Revision: D7686350

fbshipit-source-id: 27b91367361624dbbec0ecbdc98e5b0e3e61d517
hphp/hack/src/naming/naming.ml
hphp/hack/test/typecheck/naming_fail_for.php [new file with mode: 0644]
hphp/hack/test/typecheck/naming_fail_for.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/naming_fail_for_2.php [new file with mode: 0644]
hphp/hack/test/typecheck/naming_fail_for_2.php.exp [new file with mode: 0644]