facebook / facebook/hhvm

[Typechecker] dict types cannot be refined further for individual keys

Open
#8,900 0 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

Given this code

```hack
class ATest {}

class ATestChild extends ATest {
public function doThing(): void {}
}

function foo(dict $arr) {
if (HH\Lib\C\contains_key($arr, 'b') && $arr['b'] is ATestChild) {
$arr['b']->doThing();
}
}
```

The typechecker should understand here that `$arr['b']` is always an `ATestChild`, since we haven't mutated the value of `$arr` anywhere between the assertion `$arr['b'] is ATestChild` and the call `$arr['b']->doThing()`.

In Psalm this is essentially modelled as a shape backed by a dict — something akin to

`shape('b' => ATestChild, ...dict)`

**Actual behavior**

```
Typing[4053] No instance method doThing in ATest [1]
-> This is why I think it is an object of type ATest [2]
-> Declaration of ATest is here [3]

test.hack:9:14
[3] 1 | class ATest {}
2 |
3 | class ATestChild extends ATest {
4 | public function doThing(): void {}
5 | }
6 |
[2] 7 | function foo(dict $arr) {
8 | if (C\contains_key($arr, 'b') && $arr['b'] is ATestChild) {
[1] 9 | $arr['b']->doThing();
10 | }
11 | }
```

**Environment**
- Operating system: 'MacOS Catalina'.
- Installation method: Homebrew
- HHVM Version: 4.128

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.