facebook / facebook/hhvm

Typechecker does not identify errors in unreachable code

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

Description

**Describe the bug**
Errors in code after a return statement are not identified.

**Standalone code, or other way to reproduce the problem**

```Hack
function unreachable_1(): int {
return 5;
return $bar;
}

function unreachable_2(): int {
$bar = 6;
return 5;
return $bar->getApple();
}
```

```
$ cat unreachable.hack
function unreachable_1(): int {
return 5;
return $bar;
}

function unreachable_2(): int {
$bar = 6;
return 5;
return $bar->getApple();
}

$ hh_client
No errors!
```

**Expected behavior**

Typechecker error about the undefined variable _or_ error about unreachable code.

**Actual behavior**

No errors.

**Environment**
- Operating system: MacOS Bug Sur
- Installation method: homebrew
- HHVM Version

```
hhvm --version
HipHop VM 4.150.0-dev (rel) (non-lowptr)
Compiler: 1645593988_N
Repo schema: 3a9d8190fa75970b0339c54801281ff070f42b81

hh_client --version
hackc-3d05e8829ec5c101725f1ce265af9f3038bc288f-4.150.0-dev
```
**Additional context**
This is related to #8660, where @Wilfred [writes](https://github.com/facebook/hhvm/issues/8660#issuecomment-589180582):

> Dead code is really tricky. Users like to have some type checking there, but it's legitimate for the type checker to ignore it entirely. Dead code checks are a best effort.

The typechecker is working here in the goal of preventing runtime errors—the dead code can't produce an error. But this is a failure in the related goal of avoiding bugs; the existence of unreachable code itself is often a bug. I found this issue because of a refactor that introduced an early return and missed some important behavior.

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.