hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

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

Open muglug opened this issue 4 years ago • 0 comments

Given this code

class ATest {}

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

function foo(dict<string, ATest> $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<string, ATest>)

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<string, ATest> $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

muglug avatar Sep 21 '21 22:09 muglug