https://github.com/facebook/hhvm
Revision 95e6f39f27c414e0fef7670983144a409781d36d authored by Catherine Gasnier on 07 July 2019, 16:48:18 UTC, committed by Hhvm Bot on 07 July 2019, 16:51:59 UTC
Summary:
Putting this diff up again, because since we now have proper refinements of disjunctions in D9923073, it would be nice if the following worked:
```
function f(mixed $x): void {
  if ($x is string || $x === null) {
    expect_nullable_string($x);
  }
}
```

When we encounter a condition that is a null check, we refine the type of the variable to nonnull on one side. This diff adds refinement of the variable to type of null on the other side.

Example code:
```
function get(): ?SomeClass;

$x = get(); // ?SomeClass
if ($x !== null) {
  // $x now has type SomeClass (refined)
} else {
  // pre: $x has type ?SomeClass (not refined)
  // now: $x has type null
}
```

This can be useful when the type of the variable changes in one branch and we don't get into incorrect type after the conditional check. e.g.
```
function get(): ?SomeClass;

$x = get(); // ?SomeClass
if ($x !== null) {
  // $x now has type SomeClass (refined)
  $x = new OtherClass();
  // $x now has type OtherClass
} else {
  // do nothing
}
// pre: $x has type (?SomeClass | OtherClass)
// post: $x has type ?OtherClass
```

Reviewed By: Wilfred

Differential Revision: D8399732

fbshipit-source-id: 05d7be2c4f831d4b38efe89b05a2a3148de26f5a
1 parent a5a9d14
History
Tip revision: 95e6f39f27c414e0fef7670983144a409781d36d authored by Catherine Gasnier on 07 July 2019, 16:48:18 UTC
Refine expression to type of null at conditions
Tip revision: 95e6f39
File Mode Size
CMake
hphp
patches
third-party @ 02afc16
.gitignore -rw-r--r-- 1.5 KB
.gitmodules -rw-r--r-- 114 bytes
CMakeLists.txt -rw-r--r-- 3.8 KB
CODE_OF_CONDUCT.md -rw-r--r-- 249 bytes
CONTRIBUTING.md -rw-r--r-- 3.3 KB
ISSUE_TEMPLATE.md -rw-r--r-- 637 bytes
LICENSE.PHP -rw-r--r-- 3.1 KB
LICENSE.ZEND -rw-r--r-- 2.7 KB
README.md -rw-r--r-- 2.9 KB
SECURITY.md -rw-r--r-- 449 bytes
configure -rwxr-xr-x 642 bytes

README.md

back to top