Skip to content

Commit a8c66db

Browse files
evincarofautumnsgolemon
authored andcommitted
Look transitively through nullable chains.
This causes `??T` to be treated as `?T` for the purpose of null checking. Reviewed By: @gabelevi Differential Revision: D1250188
1 parent a02ac73 commit a8c66db

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

hphp/hack/src/typing/typing.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,11 @@ and non_null env ty =
22972297
match ty with
22982298
| _, Toption ty ->
22992299
let env, ty = Env.expand_type env ty in
2300-
env, ty
2300+
(* When "??T" appears in the typing environment due to implicit
2301+
* typing, the recursion here ensures that it's treated as
2302+
* isomorphic to "?T"; that is, all nulls are created equal.
2303+
*)
2304+
non_null env ty
23012305
| r, Tunresolved tyl ->
23022306
let env, tyl = lfold non_null env tyl in
23032307
(* We need to flatten the unresolved types, otherwise we could
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?hh
2+
3+
function bar(): array<int, ?int> {
4+
return array(123 => null, 456 => 789);
5+
}
6+
7+
function idx2<Tk, Tv>(?Indexish<Tk, Tv> $collection, ?Tk $index): ?Tv {
8+
return idx($collection, $index);
9+
}
10+
11+
function foo(): int {
12+
$a = idx2(bar(), 123);
13+
if ($a !== null) {
14+
return $a;
15+
}
16+
return 345;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No errors

0 commit comments

Comments
 (0)