Permalink
Browse files

Handle the case when the subtype is a union in simplify_subtype under…

… the new inference flag

Summary: What it says on the tin.  Effectively, this implements the rule that `(t1 | ... | tn) <: u` is equivalent to `t1 <: u & ... & tn <: u`.  This is true for unions, which is what we intend unresolved types to become in the new inference algorithm, but is not true for unresolved types as they are currently implemented.  (E.g., we treat the case of empty union on the left bound to a type variable specially.)  Hence, this change is guarded by the new inference flag.

Reviewed By: andrewjkennedy

Differential Revision: D13153680

fbshipit-source-id: 1607175f6074d2e4113b41337fd89989aa9c84de
  • Loading branch information...
manzyuk authored and hhvm-bot committed Nov 22, 2018
1 parent 8589a9b commit 568c49ea070edfa568c6f7538f6ddbc93265fae0
@@ -898,8 +898,14 @@ and simplify_subtype
| _, Tunresolved [ty_super'] when deep ->
simplify_subtype ~seen_generic_params ~deep ~this_ty ty_sub ty_super' env
| Tunresolved tyl, _ ->
if TypecheckerOptions.new_inference (Env.get_tcopt env)
then
List.fold_left tyl ~init:(env, TL.valid) ~f:(fun res ty_sub ->
res &&& simplify_subtype ~seen_generic_params ~deep ty_sub ty_super)
else default ()
(* Don't yet attempt to deal with unresolved types *)
| Tunresolved _, _
| _, Tunresolved _ ->
default ()
@@ -1604,6 +1610,10 @@ and sub_type_inner_helper env ~this_ty
match ety_sub, ety_super with
| (_, Tunresolved _), _
when TypecheckerOptions.new_inference (Env.get_tcopt env) ->
assert false
| (_, Tunresolved _), (_, Tunresolved _) ->
fst (Unify.unify env ty_super ty_sub)
@@ -0,0 +1,6 @@
<?hh // strict
// Copyright 2004-present Facebook. All Rights Reserved.
function test(int $x, string $y): vec<arraykey> {
return vec[$x, $y];
}
@@ -0,0 +1,6 @@
<?hh // strict
// Copyright 2004-present Facebook. All Rights Reserved.
function test(int $x, string $y): vec<num> {
return vec[$x, $y];
}
@@ -0,0 +1,6 @@
File "union_subtype_bad.php", line 5, characters 10-20:
Some elements in this Vector are incompatible (Typing[4110])
File "union_subtype_bad.php", line 4, characters 39-41:
This is a num (int/float)
File "union_subtype_bad.php", line 4, characters 23-28:
It is incompatible with a string

0 comments on commit 568c49e

Please sign in to comment.