You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix type checker crash caused by large number of uses of Shape::keyExists
Summary:
After a conditional we create a "union" of types. These can grow very large e.g. with shape refinement, as in this example:
```
type example_shape = shape(
?'example_1' => int,
?'example_2' => int,
?'example_3' => int,
?'example_4' => int,
?'example_5' => int,
);
function takes_example_shape(example_shape $in): string {
if (Shapes::keyExists($in, 'example_1')) {
...
}
if (Shapes::keyExists($in, 'example_2')) {
...
}
if (Shapes::keyExists($in, 'example_3')) {
...
}
if (Shapes::keyExists($in, 'example_4')) {
...
}
if (Shapes::keyExists($in, 'example_5')) {
...
}
...
```
Eventually, Hack crashes.
The crux of the problem is the "union" operation on locals that takes one very large shape type of the form `shape(?'x' => t, <more stuff>)` and computes the union with another very large shape type `shape('x' => t, <more stuff>)`. Spot the difference: just the `?`. In fact, the latter type is a subtype of the former, so the union is equivalent just to the former type.
The fix is to avoid constructing an explicit union if one type is a subtype of the other. To do this, we adapt the `simplify_subtype` function to construct a helper function `is_sub_type_alt` that returns `Some b` if one type is definitely a subtype, or definitely *not* a subtype of the other, and returns `None` if it doesn't know. (There is already an `is_sub_type` function but it's quite hacky and we want to move to a more careful approach that doesn't "force" a subtype, or generate errors and then back off).
Part of the diff involves changing `simplify_subtype` and helpers so that it doesn't generate errors.
Fixes#8249
Reviewed By: manzyuk
Differential Revision: D8731917
fbshipit-source-id: 753ddd507ad7f918c6648825a7ab1f4901044f2a
0 commit comments