Improve typing of array_map
commit44c6ddc949e3e8710f7c7919ea24d2098de51b34
authorSasha Manzyuk <manzyuk@fb.com>
Wed, 18 Apr 2018 09:45:48 +0000 (18 02:45 -0700)
committerHhvm Bot <hhvm-bot@users.noreply.github.com>
Wed, 18 Apr 2018 09:47:04 +0000 (18 02:47 -0700)
tree10153cb5fb9ce817622531a4dbadc0eafab6025d
parentb658d936c0b4caa95c56549438503e49c9d443da
Improve typing of array_map

Summary:
Typing of `array_map` used to be completely broken.  It was basically possible to pass a function whose parameter types were incompatible with the types of values in the containers being mapped over.  E.g., the following function would type check:
```
<?hh // strict

function test<TA, TB, TC>((function(TA): TB) $f, vec<TC> $x): void {
  array_map($f, $x);
}
```
Furthermore, `array_map` over literals would sometimes not work, e.g.:
```
<?hh // strict

function test(): void {
  array_map($x ==> $x, Vector {'foo', 42});
}
```
would fail to type check:
```
File "hphp/hack/test/typecheck/array_map8.php", line 4, characters 24-41:
Some elements in this Vector are incompatible (Typing[4110])
File "hphp/hack/test/typecheck/array_map8.php", line 4, characters 32-36:
This is a string
File "hphp/hack/test/typecheck/array_map8.php", line 4, characters 39-40:
It is incompatible with an int
```
(The same error would be reported if one used `vec` instead of `Vector` but bizarrely wouldn't if one used `varray`.)

This diff fixes those errors.

Reviewed By: andrewjkennedy

Differential Revision: D7655423

fbshipit-source-id: a2966779b1c68ec9045ddc39a13b5baebc980680
hphp/hack/src/typing/typing.ml
hphp/hack/test/tast/array_map.php.exp
hphp/hack/test/typecheck/array_map10.php [new file with mode: 0644]
hphp/hack/test/typecheck/array_map10.php.exp [new file with mode: 0644]
hphp/hack/test/typecheck/array_map9.php [new file with mode: 0644]
hphp/hack/test/typecheck/array_map9.php.exp [new file with mode: 0644]