facebook / facebook/hhvm

[ Typechecker | Inference bug ] Nested loops cause Hack to accept badly typed code?

Open
#8,876 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
18.7k
Forks
3.1k
Avg merge
1h 47m
Merged PRs (30d)
2

Description

**Describe the bug**
When two loops are nested, the typechecker ignores the possibility that the inner loop runs more than once. There may be more shapes of code that could surface the same bug.

**Standalone code, or other way to reproduce the problem**
```HACK
function detected(): void {
$either = new B();
for (; ; ) {
$either->b();
$either = new A();
}
}

function undetected(): void {
$either = new B();
for (; ; ) {
for (; ; ) {
$either->b();
$either = new A();
}
}
}

final class A {
public function a(): void {}
}

final class B {
public function b(): void {}
}
```

Steps to reproduce the behavior:
1. Invoke typechecker, observe a single error in `detected()`
2. Invoke `undetected()` from a script, observe bad undefined method error.

**Expected behavior**

The typechecker should emit an error for both functions.

**Actual behavior**

```
Typing[4053] No instance method b in A [1]
-> Did you mean a instead? [2]
-> This is why I think it is an object of type A [3]
-> Declaration of A is here [4]

bug.hack:4:14
2 | $either = new B();
3 | for (; ; ) {
[1] 4 | $either->b();
[3] 5 | $either = new A();
6 | }
7 | }
:
18 | }
19 |
[4] 20 | final class A {
[2] 21 | public function a(): void {}
22 | }
23 |

1 error found
```

```
Fatal error: Call to undefined method A::b()
```

**Environment**
- Operating system
> Ubuntu 18.04
- Installation method
> apt-get with dl.hhvm.com repository
- HHVM Version
```
HipHop VM 4.123.0 (rel) (non-lowptr)
Compiler: 1629137703_222182196
Repo schema: 10a34637ad661d98ba3344717656fcc76209c2f8
hackc-5ac1dbb836dec427bfb33a977a7e432f2c1d948c-4.123.0
```

**Additional context**
N/A

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.