Skip to content

Commit

Permalink
Support dynamic for splats in tuples
Browse files Browse the repository at this point in the history
Summary: Tuple types of the form `(t, ...T)` support dynamic if `t` does and `T` does.

Differential Revision: D66009281

fbshipit-source-id: e935d6252c1bc48f2fde2c83b2861444db7db18f
  • Loading branch information
Andrew Kennedy authored and facebook-github-bot committed Nov 19, 2024
1 parent a490802 commit b0df8ac
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
12 changes: 7 additions & 5 deletions hphp/hack/src/typing/typing_subtype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4186,8 +4186,12 @@ end = struct
~rhs:{ super_like = false; super_supportdyn = false; ty_super }
in
param_props env &&& ret_prop
| (_, Ttuple { t_required; t_extra = Textra { t_optional; t_variadic } })
->
| (_, Ttuple { t_required; t_extra }) ->
let extras =
match t_extra with
| Textra { t_variadic; t_optional } -> t_variadic :: t_optional
| Tsplat t_splat -> [t_splat]
in
List.fold_left
~init:(env, TL.valid)
~f:(fun res ty_sub ->
Expand All @@ -4198,9 +4202,7 @@ end = struct
~lhs:{ sub_supportdyn; ty_sub }
~rhs:
{ super_like = false; super_supportdyn = false; ty_super })
((t_variadic :: t_optional) @ t_required)
(* TODO splats in tuples *)
| (_, Ttuple { t_extra = Tsplat _; _ }) -> invalid env ~fail
(extras @ t_required)
| ( _,
Tshape
{
Expand Down
12 changes: 12 additions & 0 deletions hphp/hack/test/typecheck/splat/tuple_splat_dynamic.bad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh

<<file: __EnableUnstableFeatures('open_tuples', 'type_splat')>>

function expect_dyn(dynamic $_):void { }

<<__NoAutoDynamic>>
class C { }

function test<<<__NoAutoBound>> T as (C...)>((int, ...T) $tup):void {
expect_dyn($tup);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ERROR: File "tuple_splat_dynamic.bad.php", line 11, characters 14-17:
Invalid argument (Typing[4110])
File "tuple_splat_dynamic.bad.php", line 5, characters 21-27:
Expected `dynamic`
File "tuple_splat_dynamic.bad.php", line 10, characters 46-56:
Type `(int, ...T)` is not a subtype of `dynamic` under dynamic-aware subtyping
12 changes: 12 additions & 0 deletions hphp/hack/test/typecheck/splat/tuple_splat_dynamic.good.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?hh

<<file: __EnableUnstableFeatures('open_tuples', 'type_splat')>>

function expect_dyn(dynamic $_):void { }

<<__SupportDynamicType>>
class C { }

function test<T as (C...)>((int, ...T) $tup):void {
expect_dyn($tup);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No errors

0 comments on commit b0df8ac

Please sign in to comment.