-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flow] negation consistency check for type guards
Summary: Today’s [consistency check](https://flow.org/en/docs/types/type-guards/#predicate-type-is-consistent-with-refined-type) guarantees that the refinement happening in the then-branch is safe. It does not prove correct, however, the refinement in the else-branch. For example, consider ``` function isPositive(value: mixed): value is number { return typeof value === "number" && value > 0; // no error here } ``` If we use this type guard in an if-then-else, we'll notice that `x` is refined in both the then- and else-branches: ``` function bar(x: number | string) { if (isPositive(x)) { // x is number here -- okay } else { // x is string here -- BUG } } ``` Here we mistakenly infer that `x` is `string` in the else branch. This diff implements a fix for this soundness hole: a check that ensures that the type guard after being applied the negation of the function predicate is refined down to the empty type. For `isPositive` this check will fail because `number` refined with the negation of the predicate encoded in ``` typeof value === "number" && value > 0 ``` is still `number` (`value` could just be a non-positive number). For now this check is only enforced when the flowconfig option `one_sided_type_guards=true`, because using a one-sided type guard is typically the right way to fix this issue. Credit to danvk for [bringing up this issue](https://twitter.com/danvdk/status/1765745099554668830) and suggesting this version of the complete consistency check. Reviewed By: SamChou19815 Differential Revision: D56207325 fbshipit-source-id: 319f5b4d9fdc2bd24ff2dd0afd2fcb84cd9c6498
- Loading branch information
1 parent
2a24c0e
commit 622ef38
Showing
10 changed files
with
354 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.