add support for --pessimise_builtins to hh_expect
commit4c5272ccfd515044666aca81a67427439c7647dc
authorFrancesco Zappa Nardelli <fzn@fb.com>
Mon, 27 Dec 2021 13:35:24 +0000 (27 05:35 -0800)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Mon, 27 Dec 2021 13:36:40 +0000 (27 05:36 -0800)
treea652a3d69756834fb77bc591aad77f40db073a69
parentaeaca150bd412fb93efd4e7f1ec66cf736bb6f82
add support for --pessimise_builtins to hh_expect

Summary:
`hh_expect_equivalent` has a slighlty odd behaviour wrt pessimisation.  Consider

```
function test(shape('k' => vec<int>) $s): void {
  $vec = Shapes::idx($s, 'k', vec[]);
  hh_expect_equivalent<vec<int>>($vec);
}
```

After pessimisation `$vec : ~vec<int>` and, for pessimisation testing purposes, `hh_expect_equivalent` should accept `~vec<int>` as "equivalent" to `vec<int>`.

On the other hand, consider

```
function foo(): void {
  $v = Vector {};
  hh_expect_equivalent<Vector<int>>($v);
}
```

After pessimisation, `$v` still has type `Vector<int>`, and `hh_expect_equivalent` should accept `Vector<int>` as "equivalent" to `Vector<int>`.

I suggest that if `--pessimise-builtins` is selected,

```
hh_expect_equivalent<t2>($a)
```

where `$a : t1`, should implement the check

```
(t1 == t2 || t1 == ~t2)
```

Unless I am mistaken this can be simplified into

```
(t1 == t2 || t1 == ~t2)
<=> (t1 <: t2 && t2 <: t1) || (t1 <: ~t2 && ~t2 <: t1)
<=> (t1 <: t2 | ~t2) && (t2 & ~t2 <: t1)
<=> t1 <: ~t2  && t2 <: t1
```
(observe the asymmetry).

This diff implements the above and relaxes the check performed by hh_expect_equivalent into `t1 <: ~t2  && t2 <: t1` if `--pessimise-builtins` is enabled.

Reviewed By: andrewjkennedy

Differential Revision: D33237479

fbshipit-source-id: 8faa1ccfec88e297d9a2c94486f51a779cac7c8c
hphp/hack/src/typing/typing.ml