Skip to content

Commit

Permalink
[flow][tuples] Refactor: update ArrayAT's tuple_view to use record, c…
Browse files Browse the repository at this point in the history
…reate empty_tuple_view

Summary:
Refactor: update ArrayAT's tuple_view to use record, and create empty_tuple_view

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D56713882

fbshipit-source-id: 7c6199b7580ac619b78d5a3cd383b2128246b708
  • Loading branch information
gkz authored and facebook-github-bot committed Apr 29, 2024
1 parent d07fbab commit 98ff96c
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 33 deletions.
8 changes: 7 additions & 1 deletion src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ let rec dump_t_ (depth, tvars) cx t =
p t ~extra:(spf "%s, %s" (Properties.string_of_id props_tmap) obj_kind)
| DefT (_, ArrT (ArrayAT { elem_t; tuple_view = None; react_dro = _ })) ->
p ~extra:(spf "Array %s" (kid elem_t)) t
| DefT (_, ArrT (ArrayAT { elem_t; tuple_view = Some (elements, _arity); react_dro = _ })) ->
| DefT
( _,
ArrT
(ArrayAT
{ elem_t; tuple_view = Some (TupleView { elements; arity = _ }); react_dro = _ }
)
) ->
p
~extra:
(spf
Expand Down
5 changes: 3 additions & 2 deletions src/typing/env_resolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ let rec resolve_binding cx reason loc b =
| Ast.Expression.Object obj -> mk_obj loc obj
| Ast.Expression.Array { Ast.Expression.Array.elements = []; _ } ->
let (_, elem_t) = Statement.empty_array cx loc in
DefT (reason, ArrT (ArrayAT { elem_t; tuple_view = Some ([], (0, 0)); react_dro = None }))
DefT
(reason, ArrT (ArrayAT { elem_t; tuple_view = Some empty_tuple_view; react_dro = None }))
| Ast.Expression.Array { Ast.Expression.Array.elements; _ } ->
(* TODO merge code with statement.ml implementation *)
let array_elements cx undef_loc =
Expand Down Expand Up @@ -723,7 +724,7 @@ let rec resolve_binding cx reason loc b =
else
let elem_t = EmptyT.make (mk_reason REmptyArrayElement loc) in
Flow_js_utils.add_output cx Error_message.(EEmptyArrayNoProvider { loc });
(elem_t, Some ([], (0, 0)), replace_desc_reason REmptyArrayLit reason)
(elem_t, Some empty_tuple_view, replace_desc_reason REmptyArrayLit reason)
in
let t = DefT (reason, ArrT (ArrayAT { elem_t; tuple_view; react_dro = None })) in
let cache = Context.node_cache cx in
Expand Down
36 changes: 25 additions & 11 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4256,7 +4256,7 @@ struct
);
rec_flow cx trace (from, ObjAssignFromT (use_op, r, o, t, default_obj_assign_kind)))
elements
| ArrayAT { tuple_view = Some (elements, _arity); _ } ->
| ArrayAT { tuple_view = Some (TupleView { elements; arity = _ }); _ } ->
(* Object.assign(o, ...[x,y,z]) -> Object.assign(o, x, y, z) *)
let ts = tuple_ts_of_elements elements in
List.iter
Expand Down Expand Up @@ -4655,10 +4655,15 @@ struct
| ArrayAT { tuple_view = None; _ }
| ROArrayAT _ ->
arrtype
| ArrayAT { elem_t; tuple_view = Some (elements, (num_req, num_total)); react_dro } ->
| ArrayAT
{
elem_t;
tuple_view = Some (TupleView { elements; arity = (num_req, num_total) });
react_dro;
} ->
let elements = Base.List.drop elements i in
let arity = (max (num_req - i) 0, max (num_total - i) 0) in
ArrayAT { elem_t; tuple_view = Some (elements, arity); react_dro }
ArrayAT { elem_t; tuple_view = Some (TupleView { elements; arity }); react_dro }
| TupleAT { elem_t; elements; arity = (num_req, num_total); react_dro } ->
TupleAT
{
Expand Down Expand Up @@ -4706,14 +4711,14 @@ struct
react_dro;
tuple_view =
Base.Option.map
~f:(fun (elements, arity) ->
~f:(fun (TupleView { elements; arity }) ->
let elements =
Base.List.map
~f:(fun (TupleElement { name; t; polarity; optional; reason }) ->
TupleElement { name; t = f t; polarity; optional; reason })
elements
in
(elements, arity))
TupleView { elements; arity })
tuple_view;
}
| TupleAT { elem_t; elements; arity; react_dro } ->
Expand Down Expand Up @@ -9406,13 +9411,13 @@ struct
let ts1 =
Base.Option.value_map
~default:[]
~f:(fun (elements, _arity) -> tuple_ts_of_elements elements)
~f:(fun (TupleView { elements; arity = _ }) -> tuple_ts_of_elements elements)
tv1
in
let ts2 =
Base.Option.value_map
~default:[]
~f:(fun (elements, _arity) -> tuple_ts_of_elements elements)
~f:(fun (TupleView { elements; arity = _ }) -> tuple_ts_of_elements elements)
tv2
in
array_unify cx trace ~use_op (ts1, t1, ts2, t2)
Expand Down Expand Up @@ -9985,12 +9990,12 @@ struct
let (args_rev, seen_opt) =
match arrtype with
| ArrayAT { tuple_view = None; _ }
| ArrayAT { tuple_view = Some ([], _); _ } ->
| ArrayAT { tuple_view = Some (TupleView { elements = []; _ }); _ } ->
(* The latter case corresponds to the empty array literal. If
* we folded over the empty tuple_types list, then this would
* cause an empty result. *)
(arg :: args_rev, seen_opt)
| ArrayAT { tuple_view = Some (elements, _); _ }
| ArrayAT { tuple_view = Some (TupleView { elements; _ }); _ }
| TupleAT { elements; _ } ->
Base.List.fold_left
~f:(fun (args_rev, seen_opt) (TupleElement ({ optional; t; _ } as elem)) ->
Expand Down Expand Up @@ -10149,7 +10154,14 @@ struct
if valid then
DefT
( reason_op,
ArrT (ArrayAT { elem_t; tuple_view = Some (elements, arity); react_dro = None })
ArrT
(ArrayAT
{
elem_t;
tuple_view = Some (TupleView { elements; arity });
react_dro = None;
}
)
)
else
DefT (reason_op, ArrT (ArrayAT { elem_t; tuple_view = None; react_dro = None }))
Expand Down Expand Up @@ -10200,7 +10212,9 @@ struct
(match resolved with
| ResolvedArg (TupleElement { t; _ }, generic) :: rest ->
flatten cx r ((t, generic) :: args) spread rest
| ResolvedSpreadArg (_, ArrayAT { tuple_view = Some (elements, _); _ }, generic) :: rest
| ResolvedSpreadArg
(_, ArrayAT { tuple_view = Some (TupleView { elements; _ }); _ }, generic)
:: rest
| ResolvedSpreadArg (_, TupleAT { elements; _ }, generic) :: rest ->
let args =
List.rev_append
Expand Down
4 changes: 3 additions & 1 deletion src/typing/flow_js_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,9 @@ let array_elem_check ~write_action cx trace l use_op reason reason_tup arrtype =
let (elem_t, elements, is_index_restricted, is_tuple, react_dro) =
match arrtype with
| ArrayAT { elem_t; tuple_view; react_dro } ->
let elements = Base.Option.map ~f:(fun (elements, _arity) -> elements) tuple_view in
let elements =
Base.Option.map ~f:(fun (TupleView { elements; arity = _ }) -> elements) tuple_view
in
(elem_t, elements, false, false, react_dro)
| TupleAT { elem_t; elements; arity = _; react_dro } ->
(elem_t, Some elements, true, true, react_dro)
Expand Down
24 changes: 21 additions & 3 deletions src/typing/members.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,27 @@ let rec merge_type cx =
mk_object_def_type ~reason ~flags ~call id o1.proto_t
| _ -> create_union (UnionRep.make t1 t2 []))
| ( DefT
(_, ArrT (ArrayAT { elem_t = t1; tuple_view = Some (elements1, arity1); react_dro = dro1 })),
( _,
ArrT
(ArrayAT
{
elem_t = t1;
tuple_view = Some (TupleView { elements = elements1; arity = arity1 });
react_dro = dro1;
}
)
),
DefT
(_, ArrT (ArrayAT { elem_t = t2; tuple_view = Some (elements2, arity2); react_dro = dro2 }))
( _,
ArrT
(ArrayAT
{
elem_t = t2;
tuple_view = Some (TupleView { elements = elements2; arity = arity2 });
react_dro = dro2;
}
)
)
)
when arity1 = arity2
&& List.for_all2
Expand Down Expand Up @@ -261,7 +279,7 @@ let rec merge_type cx =
(ArrayAT
{
elem_t = merge_type cx (t1, t2);
tuple_view = Some (elements, arity1);
tuple_view = Some (TupleView { elements; arity = arity1 });
react_dro =
( if Base.Option.is_some dro1 && Base.Option.is_some dro2 then
dro1
Expand Down
2 changes: 1 addition & 1 deletion src/typing/react_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ module Kit (Flow : Flow_common.S) : REACT = struct
(ArrayAT
{
elem_t = union_of_ts r ts;
tuple_view = Some (elements, (arity, arity));
tuple_view = Some (TupleView { elements; arity = (arity, arity) });
react_dro = None;
}
)
Expand Down
8 changes: 6 additions & 2 deletions src/typing/resolvableTypeJob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ and collect_of_type ?log_unresolved cx acc = function
collect_of_types ?log_unresolved cx acc ts
| DefT (_, ArrT (ArrayAT { elem_t; tuple_view = None; react_dro = _ })) ->
collect_of_type ?log_unresolved cx acc elem_t
| DefT (_, ArrT (ArrayAT { elem_t; tuple_view = Some (elements, _); react_dro = _ }))
| DefT
( _,
ArrT
(ArrayAT { elem_t; tuple_view = Some (TupleView { elements; arity = _ }); react_dro = _ })
)
| DefT (_, ArrT (TupleAT { elem_t; elements; arity = _; react_dro = _ })) ->
collect_of_types ?log_unresolved cx acc (elem_t :: TypeUtil.tuple_ts_of_elements elements)
| DefT (_, ArrT (ROArrayAT (elemt, _))) -> collect_of_type ?log_unresolved cx acc elemt
Expand Down Expand Up @@ -298,7 +302,7 @@ and collect_of_destructor ?log_unresolved cx acc = function
(match arr with
| ArrayAT { elem_t; tuple_view = None; react_dro = _ } ->
collect_of_type ?log_unresolved cx acc elem_t
| ArrayAT { elem_t; tuple_view = Some (elements, _); react_dro = _ }
| ArrayAT { elem_t; tuple_view = Some (TupleView { elements; arity = _ }); react_dro = _ }
| TupleAT { elem_t; elements; arity = _; react_dro = _ } ->
collect_of_types
?log_unresolved
Expand Down
8 changes: 6 additions & 2 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2709,15 +2709,19 @@ module Make
let elem_t = Context.mk_placeholder cx element_reason in
( ( loc,
DefT
(reason, ArrT (ArrayAT { elem_t; tuple_view = Some ([], (0, 0)); react_dro = None }))
( reason,
ArrT (ArrayAT { elem_t; tuple_view = Some empty_tuple_view; react_dro = None })
)
),
Array { Array.elements = []; comments }
)
| [] ->
let (reason, elem_t) = empty_array cx loc in
( ( loc,
DefT
(reason, ArrT (ArrayAT { elem_t; tuple_view = Some ([], (0, 0)); react_dro = None }))
( reason,
ArrT (ArrayAT { elem_t; tuple_view = Some empty_tuple_view; react_dro = None })
)
),
Array { Array.elements = []; comments }
)
Expand Down
6 changes: 3 additions & 3 deletions src/typing/subtyping_kit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1837,13 +1837,13 @@ module Make (Flow : INPUT) : OUTPUT = struct
let ts1 =
Base.Option.value_map
~default:[]
~f:(fun (elements, _arity) -> tuple_ts_of_elements elements)
~f:(fun (TupleView { elements; arity = _ }) -> tuple_ts_of_elements elements)
tv1
in
let ts2 =
Base.Option.value_map
~default:[]
~f:(fun (elements, _arity) -> tuple_ts_of_elements elements)
~f:(fun (TupleView { elements; arity = _ }) -> tuple_ts_of_elements elements)
tv2
in
array_flow cx trace use_op lit1 r1 (ts1, t1, ts2, t2)
Expand Down Expand Up @@ -1948,7 +1948,7 @@ module Make (Flow : INPUT) : OUTPUT = struct
-> begin
match tuple_view with
| None -> add_output cx ~trace (Error_message.ENonLitArrayToTuple ((r1, r2), use_op))
| Some (elements, arity) ->
| Some (TupleView { elements; arity }) ->
rec_flow_t
cx
trace
Expand Down
9 changes: 8 additions & 1 deletion src/typing/ty_normalizer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,14 @@ module Make (I : INPUT) : S = struct
None
in
match (elt_t, desc) with
| (T.ArrayAT { elem_t = _; tuple_view = Some (elements', _); react_dro = _ }, RRestArrayLit _)
| ( T.ArrayAT
{
elem_t = _;
tuple_view = Some (T.TupleView { elements = elements'; arity = _ });
react_dro = _;
},
RRestArrayLit _
)
| (T.TupleAT { elements = elements'; _ }, _) ->
let%map elements =
mapM
Expand Down
10 changes: 9 additions & 1 deletion src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,18 @@ module rec TypeTerm : sig
| Props
| DROAnnot

and tuple_view =
| TupleView of {
elements: tuple_element list;
arity: int * int;
}

and arrtype =
| ArrayAT of {
react_dro: (ALoc.t * dro_type) option;
(* Should elements of this array be treated as propagating read-only, and if so, what location is responsible *)
elem_t: t;
tuple_view: (tuple_element list * (int * int)) option;
tuple_view: tuple_view option;
}
(* TupleAT of elemt * tuple_types. Why do tuples carry around elemt? Well, so
* that they don't need to recompute their general type when you do
Expand Down Expand Up @@ -4629,3 +4635,5 @@ end
type annotated_or_inferred =
| Annotated of TypeTerm.t
| Inferred of TypeTerm.t

let empty_tuple_view = TupleView { elements = []; arity = (0, 0) }
2 changes: 1 addition & 1 deletion src/typing/type_hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ and type_of_hint_decomposition cx op reason t =
~upper_unresolved:false
( DefT
( reason,
ArrT (ArrayAT { elem_t; tuple_view = Some ([], (0, 0)); react_dro = None })
ArrT (ArrayAT { elem_t; tuple_view = Some empty_tuple_view; react_dro = None })
),
t
)
Expand Down
5 changes: 2 additions & 3 deletions src/typing/type_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,12 @@ class virtual ['a] t =
let elem_t' = self#type_ cx map_cx elem_t in
let tuple_view' =
OptionUtils.ident_map
(fun tuple_view ->
let (elements, arity) = tuple_view in
(fun (TupleView { elements; arity } as tuple_view) ->
let elements' = ListUtils.ident_map (self#tuple_element cx map_cx) elements in
if elements' == elements then
tuple_view
else
(elements', arity))
TupleView { elements = elements'; arity })
tuple_view
in
if elem_t' == elem_t && tuple_view' == tuple_view then
Expand Down
2 changes: 1 addition & 1 deletion src/typing/type_visitor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class ['a] t =
method private arr_type cx pole acc =
function
| ArrayAT { elem_t; tuple_view = None; react_dro = _ } -> self#type_ cx P.Neutral acc elem_t
| ArrayAT { elem_t; tuple_view = Some (elements, _); react_dro = _ }
| ArrayAT { elem_t; tuple_view = Some (TupleView { elements; arity = _ }); react_dro = _ }
| TupleAT { elem_t; elements; arity = _; react_dro = _ } ->
let acc = self#type_ cx P.Neutral acc elem_t in
let acc = self#list (self#tuple_element cx pole) acc elements in
Expand Down

0 comments on commit 98ff96c

Please sign in to comment.