Mitigate the effect of T25936918 on typing of idx
commita8318c04a2eae516b236fa39c1f4eedbf1053c8f
authorSasha Manzyuk <manzyuk@fb.com>
Thu, 1 Mar 2018 12:29:10 +0000 (1 04:29 -0800)
committerHhvm Bot <hhvm-bot@users.noreply.github.com>
Thu, 1 Mar 2018 12:37:58 +0000 (1 04:37 -0800)
tree355e60f71343e12e89f6f80cbf16efd0dc1a0d56
parent66f75f26ab4a79766005b0e6d654431021232832
Mitigate the effect of T25936918 on typing of idx

Summary:
A majority of, if not all, instances of T25936918 in www arise from the function `idx`.  We don't have a proper fix for T25936918, but we can mitigate the problem by changing the way the type signature of `idx` is rewritten internally by the type checker: instead of
```
idx<Tk, Tv>(?KeyedContainer<Tk, Tv> $collection, ?Tk $index): ?Tv
```
we can use
```
idx<Tk, Tv>(?KeyedContainer<Tk, ?Tv> $collection, ?Tk $index): ?Tv
```
(Note that `KeyedContainer<Tk, Tv>` is covariant in `Tv`.)

As an alternative, I've considered changing the type of `idx`  specified in `idx.hhi` to
```
function idx<Tk, Tv>(
  ?KeyedContainer<Tk, Tv> $collection,
  ?Tk $index,
  Tv $default,
): Tv {}
```
and rewriting the argument list by passing extra `null` if `idx` is called with 2 arguments.  This works but because I have to manufacture `null` out of thin air, there is no position information associated with it and error messages can sometimes be confusing.  The implementation of typing simplifies though.

I've tried running the new version of the type checker on www (with `nonnull` enabled).  Surprisingly, this revealed some new errors that don't look directly related but that I think are valid: D7111140.  On the other hand, I was able to remove some of the previously introduced `HH_FIXME`s: D7111145.  (I believe those are all `HH_FIXME` that I had to add because of this bug.)

Reviewed By: dlreeves

Differential Revision: D7111132

fbshipit-source-id: f4f241df262fc03d670886e2fd071b0060113d71
hphp/hack/hhi/idx.hhi
hphp/hack/src/typing/typing.ml
hphp/hack/test/typecheck/idx2_5.php [new file with mode: 0644]
hphp/hack/test/typecheck/idx2_5.php.exp [new file with mode: 0644]