Suggestion: Improve typechecker error for refinement on container keys
- Dominant language
- C++
- Stars
- 18.7k
- Forks
- 3.1k
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 2
Description
### HHVM Version
```
HipHop VM 4.13.2 (rel)
Compiler: 1573166016_N
Repo schema: 12f66242996fabd00f4fab1e33c4fbc011981c09
```
### Standalone code, or other way to reproduce the problem
```
function example(dict $foo): void {
if ($foo['bar'] is Traversable<_>) {
foreach ($foo['bar'] as $it) {
// do something
}
}
}
```
### Actual result
```
Typing[4110] Invalid foreach
--> test.hack
1 | function example(dict $foo): void {
| ^^^^^ But got mixed
3 | foreach ($foo['bar'] as $it) {
| ^^^^^^^^^^^
| ^^^^^^^^^^^ Expected Traversable<[unresolved]> because this is used in a foreach statement
1 error found.
```
### Expected result
This is something that consistently trips up new Hack developers as they learn about type refinement and `is/as`. The most ideal state would be that the typechecker could remember the refinement on a sub-key of a container. But I expect that's a significant change. A perhaps much smaller change would be to provide a clearer error message about how to fix this problem, such as recommending the user pull `$foo['bar']` into a local variable before refining its type with the type test.
Contributor guide
Assessment
This issue has not been assessed yet.