-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
flow_js.ml
9346 lines (9065 loc) · 377 KB
/
flow_js.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
(* This module describes the subtyping algorithm that forms the core of
typechecking. The algorithm (in its basic form) is described in Francois
Pottier's thesis. The main data structures maintained by the algorithm are:
(1) for every type variable, which type variables form its lower and upper
bounds (i.e., flow in and out of the type variable); and (2) for every type
variable, which concrete types form its lower and upper bounds. Every new
subtyping constraint added to the system is deconstructed into its subparts,
until basic flows between type variables and other type variables or concrete
types remain; these flows are then viewed as links in a chain, bringing
together further concrete types and type variables to participate in
subtyping. This process continues till a fixpoint is reached---which itself
is guaranteed to exist, and is usually reached in very few steps. *)
open Flow_js_utils
open Utils_js
open Reason
open Type
open TypeUtil
open Constraint
open Debug_js.Verbose
module FlowError = Flow_error
module IICheck = Implicit_instantiation_check
(**************************************************************)
(* Check that id1 is not linked to id2. *)
let not_linked (id1, _bounds1) (_id2, bounds2) =
(* It suffices to check that id1 is not already in the lower bounds of
id2. Equivalently, we could check that id2 is not already in the upper
bounds of id1. *)
not (IMap.mem id1 bounds2.lowertvars)
(********************************************************************)
module ImplicitTypeArgument = Instantiation_utils.ImplicitTypeArgument
module TypeAppExpansion = Instantiation_utils.TypeAppExpansion
module Cache = Flow_cache
(********************)
(* subtype relation *)
(********************)
(* Sometimes we expect types to be def types. For example, when we see a flow
constraint from type l to type u, we expect l to be a def type. As another
example, when we see a unification constraint between t1 and t2, we expect
both t1 and t2 to be def types. *)
(* Recursion limiter. We proxy recursion depth with trace depth,
which is either equal or pretty close.
When check is called with a trace whose depth exceeds a constant
limit, we throw a LimitExceeded exception.
*)
module RecursionCheck : sig
exception LimitExceeded
val check : Context.t -> Type.DepthTrace.t -> unit
end = struct
exception LimitExceeded
(* check trace depth as a proxy for recursion depth
and throw when limit is exceeded *)
let check cx trace =
if DepthTrace.depth trace >= Context.recursion_limit cx then raise LimitExceeded
end
(* The main problem with constant folding is infinite recursion. Consider a loop
* that keeps adding 1 to a variable x, which is initialized to 0. If we
* constant fold x naively, we'll recurse forever, inferring that x has the type
* (0 | 1 | 2 | 3 | 4 | etc). What we need to do is recognize loops and stop
* doing constant folding.
*
* One solution is for constant-folding-location to keep count of how many times
* we have seen a reason at a given position in the array.
* Then, when we've seen it multiple times in the same place, we can decide
* to stop doing constant folding.
*)
module ConstFoldExpansion : sig
val guard : Context.t -> int -> reason * int -> (int -> 't) -> 't
end = struct
let get_rmap cache id = IMap.find_opt id cache |> Base.Option.value ~default:ConstFoldMap.empty
let increment reason_with_pos rmap =
match ConstFoldMap.find_opt reason_with_pos rmap with
| None -> (0, ConstFoldMap.add reason_with_pos 1 rmap)
| Some count -> (count, ConstFoldMap.add reason_with_pos (count + 1) rmap)
let guard cx id reason_with_pos f =
let cache = Context.const_fold_cache cx in
let (count, rmap) = get_rmap !cache id |> increment reason_with_pos in
cache := IMap.add id rmap !cache;
f count
end
let subst = Type_subst.subst ~placeholder_no_infer:false
let check_canceled =
let count = ref 0 in
fun () ->
let n = (!count + 1) mod 128 in
count := n;
if n = 0 then WorkerCancel.check_should_cancel ()
let is_concrete t =
match t with
| EvalT _
| AnnotT _
| MaybeT _
| OptionalT _
| TypeAppT _
| ThisTypeAppT _
| OpenT _ ->
false
| _ -> true
let is_literal_type t =
match t with
| DefT (_, SingletonStrT _)
| DefT (_, SingletonNumT _)
| DefT (_, SingletonBoolT _)
| DefT (_, SingletonBigIntT _) ->
true
| _ -> false
let inherited_method = function
| OrdinaryName "constructor" -> false
| _ -> true
let find_resolved_opt cx ~default ~f id =
let constraints = Context.find_graph cx id in
match constraints with
| Resolved t -> f t
| FullyResolved s -> f (Context.force_fully_resolved_tvar cx s)
| Unresolved _ -> default
let rec drop_resolved cx t =
match t with
| GenericT { reason; name; id = g_id; bound = OpenT (_, id); no_infer } ->
find_resolved_opt cx id ~default:t ~f:(fun t ->
GenericT { reason; name; id = g_id; bound = drop_resolved cx t; no_infer }
)
| OpenT (_, id) -> find_resolved_opt cx id ~default:t ~f:(drop_resolved cx)
| _ -> t
(********************** start of slab **********************************)
module M__flow
(FlowJs : Flow_common.S)
(ReactJs : React_kit.REACT)
(ObjectKit : Object_kit.OBJECT)
(SpeculationKit : Speculation_kit.OUTPUT)
(SubtypingKit : Subtyping_kit.OUTPUT) =
struct
open SubtypingKit
module InstantiationHelper = struct
let mk_targ = ImplicitTypeArgument.mk_targ
let is_subtype = FlowJs.rec_flow_t
let unify cx trace ~use_op (t1, t2) = FlowJs.rec_unify cx trace ~use_op ~unify_any:true t1 t2
let reposition = FlowJs.reposition ?desc:None ?annot_loc:None
end
module InstantiationKit = Instantiation_kit (InstantiationHelper)
module ImplicitInstantiationKit = Implicit_instantiation.Kit (FlowJs) (InstantiationHelper)
module Import_export_helper :
Flow_js_utils.Import_export_helper_sig with type r = Type.t -> unit = struct
type r = Type.t -> unit
let reposition = FlowJs.reposition ~trace:DepthTrace.dummy_trace ?desc:None ?annot_loc:None
let return cx t tout = FlowJs.rec_flow_t cx ~use_op:unknown_use DepthTrace.dummy_trace (t, tout)
let export_named cx (reason, value_exports_tmap, type_exports_tmap, export_kind) module_t tout =
FlowJs.rec_flow
cx
DepthTrace.dummy_trace
( module_t,
Type.ExportNamedT { reason; value_exports_tmap; type_exports_tmap; export_kind; tout }
)
let export_named_fresh_var
cx (reason, value_exports_tmap, type_exports_tmap, export_kind) module_t =
Tvar.mk_where cx reason (fun tout ->
FlowJs.rec_flow
cx
DepthTrace.dummy_trace
( module_t,
Type.ExportNamedT { reason; value_exports_tmap; type_exports_tmap; export_kind; tout }
)
)
let export_type cx (reason, name_loc, preferred_def_locs, export_name, target_module_t) export_t
=
Tvar.mk_where cx reason (fun tout ->
FlowJs.rec_flow
cx
DepthTrace.dummy_trace
( export_t,
ExportTypeT
{ reason; name_loc; preferred_def_locs; export_name; target_module_t; tout }
)
)
let cjs_extract_named_exports cx (reason, local_module) proto_t =
Tvar.mk_where cx reason (fun t ->
FlowJs.rec_flow
cx
DepthTrace.dummy_trace
(proto_t, CJSExtractNamedExportsT (reason, local_module, t))
)
end
module CopyNamedExportsTKit = CopyNamedExportsT_kit (Import_export_helper)
module CopyTypeExportsTKit = CopyTypeExportsT_kit (Import_export_helper)
module ExportTypeTKit = ExportTypeT_kit (Import_export_helper)
module CJSExtractNamedExportsTKit = CJSExtractNamedExportsT_kit (Import_export_helper)
include InstantiationKit
(* get prop *)
let perform_lookup_action cx trace propref p target_kind lreason ureason =
let open FlowJs in
function
| LookupProp (use_op, up) -> rec_flow_p cx ~trace ~use_op lreason ureason propref (p, up)
| SuperProp (use_op, lp) -> rec_flow_p cx ~trace ~use_op ureason lreason propref (lp, p)
| ReadProp { use_op; obj_t; tout } ->
let react_dro =
match obj_t with
| OpenT _ -> failwith "Expected concrete type"
| DefT (_, InstanceT inst) -> inst.inst.inst_react_dro
| DefT (_, ObjT o) -> o.flags.react_dro
| _ -> None
in
FlowJs.perform_read_prop_action cx trace use_op propref p ureason react_dro tout
| WriteProp { use_op; obj_t = _; tin; write_ctx; prop_tout; mode } -> begin
match (Property.write_t_of_property_type ~ctx:write_ctx p, target_kind, mode) with
| (Some t, IndexerProperty, Delete) ->
(* Always OK to delete a property we found via an indexer *)
let void = VoidT.why (reason_of_t t) in
Base.Option.iter
~f:(fun prop_tout -> rec_flow_t cx trace ~use_op:unknown_use (void, prop_tout))
prop_tout
| (Some t, _, _) ->
rec_flow cx trace (tin, UseT (use_op, t));
Base.Option.iter
~f:(fun prop_tout -> rec_flow_t cx trace ~use_op:unknown_use (t, prop_tout))
prop_tout
| (None, _, _) ->
let reason_prop = reason_of_propref propref in
let prop_name = name_of_propref propref in
let msg = Error_message.EPropNotWritable { reason_prop; prop_name; use_op } in
add_output cx msg
end
| MatchProp { use_op; drop_generic = drop_generic_; prop_t = tin } -> begin
match Property.read_t_of_property_type p with
| Some t ->
let t =
if drop_generic_ then
drop_generic t
else
t
in
rec_flow cx trace (tin, UseT (use_op, t))
| None ->
let reason_prop = reason_of_propref propref in
let prop_name = name_of_propref propref in
add_output cx (Error_message.EPropNotReadable { reason_prop; prop_name; use_op })
end
let mk_react_dro cx use_op dro t =
let id = Eval.generate_id () in
FlowJs.mk_possibly_evaluated_destructor cx use_op (reason_of_t t) t (ReactDRO dro) id
let mk_hooklike cx use_op t =
let id = Eval.generate_id () in
FlowJs.mk_possibly_evaluated_destructor cx use_op (reason_of_t t) t MakeHooklike id
module Get_prop_helper = struct
type r = Type.tvar -> unit
let error_type cx trace reason tout =
FlowJs.rec_flow_t cx ~use_op:unknown_use trace (AnyT.error reason, OpenT tout)
let return cx ~use_op trace t tout = FlowJs.rec_flow_t cx ~use_op trace (t, OpenT tout)
let dict_read_check = FlowJs.rec_flow_t
let reposition = FlowJs.reposition ?desc:None ?annot_loc:None
let cg_lookup
cx trace ~obj_t ~method_accessible t (reason_op, lookup_kind, propref, use_op, ids) tout =
FlowJs.rec_flow
cx
trace
( t,
LookupT
{
reason = reason_op;
lookup_kind;
try_ts_on_failure = [];
propref;
lookup_action = ReadProp { use_op; obj_t; tout };
method_accessible;
ids = Some ids;
ignore_dicts = false;
}
)
let cg_get_prop cx trace t (use_op, access_reason, id, (prop_reason, name)) tout =
FlowJs.rec_flow
cx
trace
( t,
GetPropT
{
use_op;
reason = access_reason;
id;
from_annot = false;
propref = mk_named_prop ~reason:prop_reason name;
tout;
hint = hint_unavailable;
}
)
let mk_react_dro = mk_react_dro
let mk_hooklike = mk_hooklike
end
module GetPropTKit = GetPropT_kit (Get_prop_helper)
(** NOTE: Do not call this function directly. Instead, call the wrapper
functions `rec_flow`, `join_flow`, or `flow_opt` (described below) inside
this module, and the function `flow` outside this module. **)
let rec __flow cx ((l : Type.t), (u : Type.use_t)) trace =
if ground_subtype (l, u) then
print_types_if_verbose cx trace (l, u)
else if Cache.FlowConstraint.get cx (l, u) then
print_types_if_verbose cx trace ~note:"(cached)" (l, u)
else (
print_types_if_verbose cx trace (l, u);
(* limit recursion depth *)
RecursionCheck.check cx trace;
(* Check if this worker has been told to cancel *)
check_canceled ();
(* Expect that l is a def type. On the other hand, u may be a use type or a
def type: the latter typically when we have annotations. *)
if
match l with
| AnyT _ ->
(* Either propagate AnyT through the use type, or short-circuit because any <: u trivially *)
any_propagated cx trace l u
| GenericT { bound; name; reason; id; no_infer } ->
handle_generic cx trace ~no_infer bound reason id name u
| _ -> false
(* Either propagate AnyT through the def type, or short-circuit because l <: any trivially *)
then
()
else if
match u with
| UseT (use_op, (AnyT _ as any)) -> any_propagated_use cx trace use_op any l
| _ -> false
then
()
else if
match l with
| DefT (_, EmptyT) -> empty_success u
| _ -> false
then
()
else
(* START OF PATTERN MATCH *)
match (l, u) with
(********)
(* eval *)
(********)
| (EvalT (_, _, id1), UseT (_, EvalT (_, _, id2))) when Type.Eval.equal_id id1 id2 ->
if Context.is_verbose cx then prerr_endline "EvalT ~> EvalT fast path"
| (EvalT (t, TypeDestructorT (use_op', reason, d), id), _) ->
let result = mk_type_destructor cx ~trace use_op' reason t d id in
rec_flow cx trace (result, u)
| (_, UseT (use_op, EvalT (t, TypeDestructorT (use_op', reason, d), id))) ->
let result = mk_type_destructor cx ~trace use_op' reason t d id in
rec_flow cx trace (result, ReposUseT (reason, false, use_op, l))
(******************)
(* process X ~> Y *)
(******************)
| (OpenT (_, tvar1), UseT (use_op, OpenT (_, tvar2))) ->
let (id1, constraints1) = Context.find_constraints cx tvar1 in
let (id2, constraints2) = Context.find_constraints cx tvar2 in
(match (constraints1, constraints2) with
| (Unresolved bounds1, Unresolved bounds2) ->
if not_linked (id1, bounds1) (id2, bounds2) then (
add_upper_edges ~new_use_op:use_op cx trace (id1, bounds1) (id2, bounds2);
add_lower_edges cx trace ~new_use_op:use_op (id1, bounds1) (id2, bounds2);
flows_across cx trace ~use_op bounds1.lower bounds2.upper
)
| (Unresolved bounds1, Resolved t2) ->
let t2_use = flow_use_op cx unknown_use (UseT (use_op, t2)) in
edges_and_flows_to_t cx trace (id1, bounds1) t2_use
| (Unresolved bounds1, FullyResolved s2) ->
let t2_use =
flow_use_op cx unknown_use (UseT (use_op, Context.force_fully_resolved_tvar cx s2))
in
edges_and_flows_to_t cx trace (id1, bounds1) t2_use
| (Resolved t1, Unresolved bounds2) ->
edges_and_flows_from_t cx trace ~new_use_op:use_op t1 (id2, bounds2)
| (FullyResolved s1, Unresolved bounds2) ->
edges_and_flows_from_t
cx
trace
~new_use_op:use_op
(Context.force_fully_resolved_tvar cx s1)
(id2, bounds2)
| (Resolved t1, Resolved t2) ->
let t2_use = flow_use_op cx unknown_use (UseT (use_op, t2)) in
rec_flow cx trace (t1, t2_use)
| (Resolved t1, FullyResolved s2) ->
let t2_use =
flow_use_op cx unknown_use (UseT (use_op, Context.force_fully_resolved_tvar cx s2))
in
rec_flow cx trace (t1, t2_use)
| (FullyResolved s1, Resolved t2) ->
let t2_use = flow_use_op cx unknown_use (UseT (use_op, t2)) in
rec_flow cx trace (Context.force_fully_resolved_tvar cx s1, t2_use)
| (FullyResolved s1, FullyResolved s2) ->
let t2_use =
flow_use_op cx unknown_use (UseT (use_op, Context.force_fully_resolved_tvar cx s2))
in
rec_flow cx trace (Context.force_fully_resolved_tvar cx s1, t2_use))
(******************)
(* process Y ~> U *)
(******************)
| (OpenT (r, tvar), t2) ->
if
(* We have some simple tvar id based concretization. Bad cyclic types can only
* come from indirections through OpenT, most of them are already defended with
* Flow_js_utils.InvalidCyclicTypeValidation and turned to any, but there are gaps
* (especially EvalT from type sig), so we defend it again here. *)
match t2 with
| ConcretizeT { reason = _; kind = _; seen; collector = _ } ->
ISet.mem tvar !seen
||
( seen := ISet.add tvar !seen;
false
)
| _ -> false
then
()
else
let t2 =
match desc_of_reason r with
| RTypeParam _ -> mod_use_op_of_use_t (fun op -> Frame (ImplicitTypeParam, op)) t2
| _ -> t2
in
let (id1, constraints1) = Context.find_constraints cx tvar in
(match constraints1 with
| Unresolved bounds1 -> edges_and_flows_to_t cx trace (id1, bounds1) t2
| Resolved t1 -> rec_flow cx trace (t1, t2)
| FullyResolved s1 -> rec_flow cx trace (Context.force_fully_resolved_tvar cx s1, t2))
(******************)
(* process L ~> X *)
(******************)
| (t1, UseT (use_op, OpenT (_, tvar))) ->
let (id2, constraints2) = Context.find_constraints cx tvar in
(match constraints2 with
| Unresolved bounds2 ->
edges_and_flows_from_t cx trace ~new_use_op:use_op t1 (id2, bounds2)
| Resolved t2 -> rec_flow cx trace (t1, UseT (use_op, t2))
| FullyResolved s2 ->
rec_flow cx trace (t1, UseT (use_op, Context.force_fully_resolved_tvar cx s2)))
(************************)
(* Eval type destructor *)
(************************)
| (l, EvalTypeDestructorT { destructor_use_op; reason; repos; destructor; tout }) ->
let l =
match repos with
| None -> l
| Some (reason, use_desc) -> reposition_reason cx ~trace reason ~use_desc l
in
eval_destructor cx ~trace destructor_use_op reason l destructor tout
(************)
(* Subtyping *)
(*************)
| (_, UseT (use_op, u)) -> rec_sub_t cx use_op l u trace
| ( UnionT (_, _),
ConcretizeT { reason = _; kind = ConcretizeForSentinelPropTest; seen = _; collector }
)
(* For l.key !== sentinel when sentinel has a union type, don't split the union. This
prevents a drastic blowup of cases which can cause perf problems. *)
| ( UnionT (_, _),
ConcretizeT
{
reason = _;
kind = ConcretizeForPredicate ConcretizeRHSForLiteralPredicateTest;
seen = _;
collector;
}
) ->
TypeCollector.add collector l
| ( UnionT (_, rep),
ConcretizeT
{
reason = _;
kind = ConcretizeForPredicate ConcretizeForMaybeOrExistPredicateTest;
seen = _;
collector;
}
)
when UnionRep.is_optimized_finally rep ->
TypeCollector.add collector l
| (UnionT (_, urep), ConcretizeT _) -> flow_all_in_union cx trace urep u
| (MaybeT (lreason, t), ConcretizeT _) ->
let lreason = replace_desc_reason RNullOrVoid lreason in
rec_flow cx trace (NullT.make lreason, u);
rec_flow cx trace (VoidT.make lreason, u);
rec_flow cx trace (t, u)
| (OptionalT { reason = r; type_ = t; use_desc }, ConcretizeT _) ->
rec_flow cx trace (VoidT.why_with_use_desc ~use_desc r, u);
rec_flow cx trace (t, u)
| (AnnotT (r, t, use_desc), ConcretizeT _) ->
(* TODO: directly derive loc and desc from the reason of tvar *)
let loc = loc_of_reason r in
let desc =
if use_desc then
Some (desc_of_reason r)
else
None
in
rec_flow cx trace (reposition ~trace cx loc ?annot_loc:(annot_loc_of_reason r) ?desc t, u)
| (DefT (reason, EmptyT), ConvertEmptyPropsToMixedT (_, tout)) ->
rec_flow_t cx trace ~use_op:unknown_use (MixedT.make reason, tout)
| (_, ConvertEmptyPropsToMixedT (_, tout)) ->
rec_flow_t cx trace ~use_op:unknown_use (l, tout)
(***************)
(* annotations *)
(***************)
(* Special cases where we want to recursively concretize types within the
lower bound. *)
| (UnionT (r, rep), ReposUseT (reason, use_desc, use_op, l)) ->
let rep =
UnionRep.ident_map
(annot ~in_implicit_instantiation:(Context.in_implicit_instantiation cx) use_desc)
rep
in
let loc = loc_of_reason reason in
let annot_loc = annot_loc_of_reason reason in
let r = opt_annot_reason ?annot_loc @@ repos_reason loc r in
let r =
if use_desc then
replace_desc_reason (desc_of_reason reason) r
else
r
in
rec_flow cx trace (l, UseT (use_op, UnionT (r, rep)))
| (MaybeT (r, u), ReposUseT (reason, use_desc, use_op, l)) ->
let loc = loc_of_reason reason in
let annot_loc = annot_loc_of_reason reason in
let r = opt_annot_reason ?annot_loc @@ repos_reason loc r in
let r =
if use_desc then
replace_desc_reason (desc_of_reason reason) r
else
r
in
rec_flow
cx
trace
( l,
UseT
( use_op,
MaybeT
( r,
annot
~in_implicit_instantiation:(Context.in_implicit_instantiation cx)
use_desc
u
)
)
)
| ( OptionalT { reason = r; type_ = u; use_desc = use_desc_optional_t },
ReposUseT (reason, use_desc, use_op, l)
) ->
let loc = loc_of_reason reason in
let annot_loc = annot_loc_of_reason reason in
let r = opt_annot_reason ?annot_loc @@ repos_reason loc r in
let r =
if use_desc then
replace_desc_reason (desc_of_reason reason) r
else
r
in
rec_flow
cx
trace
( l,
UseT
( use_op,
OptionalT
{
reason = r;
type_ =
annot
~in_implicit_instantiation:(Context.in_implicit_instantiation cx)
use_desc
u;
use_desc = use_desc_optional_t;
}
)
)
| ( DefT
( r,
RendersT
(StructuralRenders { renders_variant; renders_structural_type = UnionT (_, rep) })
),
ReposUseT (reason, use_desc, use_op, l)
) ->
let rep =
UnionRep.ident_map
(annot ~in_implicit_instantiation:(Context.in_implicit_instantiation cx) use_desc)
rep
in
let loc = loc_of_reason reason in
let annot_loc = annot_loc_of_reason reason in
let r = opt_annot_reason ?annot_loc @@ repos_reason loc r in
let r =
if use_desc then
replace_desc_reason (desc_of_reason reason) r
else
r
in
rec_flow
cx
trace
( l,
UseT
( use_op,
DefT
( r,
RendersT
(StructuralRenders
{ renders_variant; renders_structural_type = UnionT (r, rep) }
)
)
)
)
(* Waits for a def type to become concrete, repositions it as an upper UseT
using the stored reason. This can be used to store a reason as it flows
through a tvar. *)
| (u_def, ReposUseT (reason, use_desc, use_op, l)) ->
let u = reposition_reason cx ~trace reason ~use_desc u_def in
rec_flow cx trace (l, UseT (use_op, u))
(* The source component of an annotation flows out of the annotated
site to downstream uses. *)
| (AnnotT (r, t, use_desc), u) ->
let t = reposition_reason ~trace cx r ~use_desc t in
rec_flow cx trace (t, u)
(***************************)
(* type cast e.g. `(x: T)` *)
(***************************)
| (DefT (reason, EnumValueT enum_info), TypeCastT (use_op, cast_to_t)) ->
rec_flow cx trace (cast_to_t, EnumCastT { use_op; enum = (reason, enum_info) })
| (UnionT (_, rep), TypeCastT (use_op, (UnionT _ as u))) ->
union_to_union cx trace use_op l rep u
| (UnionT _, TypeCastT (use_op, AnnotT (r, t, use_desc))) ->
rec_flow cx trace (t, ReposUseT (r, use_desc, use_op, l))
| (UnionT (_, rep1), TypeCastT _) -> flow_all_in_union cx trace rep1 u
| (_, TypeCastT (use_op, cast_to_t)) ->
(match FlowJs.singleton_concrete_type_for_inspection cx (reason_of_t l) l with
| DefT (reason, EnumValueT enum_info) ->
rec_flow cx trace (cast_to_t, EnumCastT { use_op; enum = (reason, enum_info) })
| _ -> rec_flow cx trace (l, UseT (use_op, cast_to_t)))
(**********************************************************************)
(* enum cast e.g. `(x: T)` where `x` is an `EnumValueT` *)
(* We allow enums to be explicitly cast to their representation type. *)
(* When we specialize `TypeCastT` when the LHS is an `EnumValueT`, the *)
(* `cast_to_t` of `TypeCastT` must then be resolved. So we call flow *)
(* with it on the LHS, and `EnumCastT` on the RHS. When we actually *)
(* turn this into a `UseT`, it must placed back on the RHS. *)
(**********************************************************************)
| ( cast_to_t,
EnumCastT
{
use_op;
enum =
(_, (ConcreteEnum { representation_t; _ } | AbstractEnum { representation_t }));
}
)
when TypeUtil.quick_subtype representation_t cast_to_t ->
rec_flow cx trace (representation_t, UseT (use_op, cast_to_t))
| (cast_to_t, EnumCastT { use_op; enum = (reason, enum) }) ->
rec_flow cx trace (DefT (reason, EnumValueT enum), UseT (use_op, cast_to_t))
(******************)
(* Module exports *)
(******************)
| ( ModuleT m,
ExportNamedT { reason = _; value_exports_tmap; type_exports_tmap; export_kind; tout }
) ->
ExportNamedTKit.mod_ModuleT cx (value_exports_tmap, type_exports_tmap, export_kind) m;
rec_flow_t cx ~use_op:unknown_use trace (l, tout)
| (ModuleT m, CopyNamedExportsT (reason, target_module_t, t_out)) ->
CopyNamedExportsTKit.on_ModuleT cx (reason, target_module_t) m t_out
| (ModuleT m, CopyTypeExportsT (reason, target_module_t, t_out)) ->
CopyTypeExportsTKit.on_ModuleT cx (reason, target_module_t) m t_out
| ( _,
ExportTypeT { reason; name_loc; preferred_def_locs; export_name; target_module_t; tout }
) ->
ExportTypeTKit.on_concrete_type
cx
(reason, name_loc, preferred_def_locs, export_name, target_module_t)
l
tout
| (AnyT (_, _), CopyNamedExportsT (_, target_module, t)) ->
CopyNamedExportsTKit.on_AnyT cx target_module t
| (AnyT (_, _), CopyTypeExportsT (_, target_module, t)) ->
CopyTypeExportsTKit.on_AnyT cx target_module t
| (_, CJSExtractNamedExportsT (reason, local_module, t_out)) ->
CJSExtractNamedExportsTKit.on_concrete_type cx (reason, local_module) l t_out
(******************************)
(* optional chaining - part A *)
(******************************)
| (DefT (_, VoidT), OptionalChainT { reason; lhs_reason; voided_out; t_out; _ }) ->
CalleeRecorder.add_callee_use cx CalleeRecorder.Tast l t_out;
Context.mark_optional_chain cx (loc_of_reason reason) lhs_reason ~useful:true;
rec_flow_t ~use_op:unknown_use cx trace (l, voided_out)
| (DefT (r, NullT), OptionalChainT { reason; lhs_reason; voided_out; t_out; _ }) ->
CalleeRecorder.add_callee_use cx CalleeRecorder.Tast l t_out;
let void =
match desc_of_reason r with
| RNull ->
(* to avoid error messages like "null is incompatible with null",
give VoidT that arise from `null` annotations a new description
explaining why it is void and not null *)
DefT (replace_desc_reason RVoidedNull r, VoidT)
| _ -> DefT (r, VoidT)
in
Context.mark_optional_chain cx (loc_of_reason reason) lhs_reason ~useful:true;
rec_flow_t ~use_op:unknown_use cx trace (void, voided_out)
(***************************)
(* optional indexed access *)
(***************************)
| (DefT (r, (EmptyT | VoidT | NullT)), OptionalIndexedAccessT { use_op; tout_tvar; _ }) ->
rec_flow_t ~use_op cx trace (EmptyT.why r, OpenT tout_tvar)
| ((MaybeT (_, t) | OptionalT { type_ = t; _ }), OptionalIndexedAccessT _) ->
rec_flow cx trace (t, u)
| (UnionT (_, rep), OptionalIndexedAccessT { use_op; reason; index; tout_tvar }) ->
let (t0, (t1, ts)) = UnionRep.members_nel rep in
let f t =
Tvar.mk_no_wrap_where cx reason (fun tvar ->
rec_flow
cx
trace
(t, OptionalIndexedAccessT { use_op; reason; index; tout_tvar = tvar })
)
in
let rep = UnionRep.make (f t0) (f t1) (Base.List.map ts ~f) in
rec_unify cx trace ~use_op:unknown_use (UnionT (reason, rep)) (OpenT tout_tvar)
| (_, OptionalIndexedAccessT { use_op; reason; index; tout_tvar })
when match l with
| IntersectionT _ -> false
| _ -> true ->
let u =
match index with
| OptionalIndexedAccessStrLitIndex name ->
let reason_op = replace_desc_reason (RProperty (Some name)) reason in
GetPropT
{
use_op;
reason;
id = None;
from_annot = true;
propref = mk_named_prop ~reason:reason_op ~from_indexed_access:true name;
tout = tout_tvar;
hint = hint_unavailable;
}
| OptionalIndexedAccessTypeIndex key_t ->
GetElemT
{
use_op;
reason;
id = None;
from_annot = true;
access_iterables = false;
key_t;
tout = tout_tvar;
}
in
rec_flow cx trace (l, u)
(*************)
(* DRO and hooklike *)
(*************)
| (OptionalT ({ type_; _ } as o), DeepReadOnlyT (((r, _) as tout), (dro_loc, dro_type))) ->
rec_flow_t
cx
trace
~use_op:unknown_use
( OptionalT
{
o with
type_ =
Tvar.mk_no_wrap_where cx r (fun tvar ->
rec_flow cx trace (type_, DeepReadOnlyT (tvar, (dro_loc, dro_type)))
);
},
OpenT tout
)
| (OptionalT ({ type_; _ } as o), HooklikeT ((r, _) as tout)) ->
rec_flow_t
cx
trace
~use_op:unknown_use
( OptionalT
{
o with
type_ =
Tvar.mk_no_wrap_where cx r (fun tvar ->
rec_flow cx trace (type_, HooklikeT tvar)
);
},
OpenT tout
)
| (MaybeT (rl, t), DeepReadOnlyT (((r, _) as tout), (dro_loc, dro_type))) ->
rec_flow_t
cx
trace
~use_op:unknown_use
( MaybeT
( rl,
Tvar.mk_no_wrap_where cx r (fun tvar ->
rec_flow cx trace (t, DeepReadOnlyT (tvar, (dro_loc, dro_type)))
)
),
OpenT tout
)
| (MaybeT (rl, t), HooklikeT ((r, _) as tout)) ->
rec_flow_t
cx
trace
~use_op:unknown_use
( MaybeT
(rl, Tvar.mk_no_wrap_where cx r (fun tvar -> rec_flow cx trace (t, HooklikeT tvar))),
OpenT tout
)
| (UnionT (reason, rep), DeepReadOnlyT (tout, (dro_loc, dro_type))) ->
if not (UnionRep.is_optimized_finally rep) then
UnionRep.optimize_enum_only ~flatten:(Type_mapper.union_flatten cx) rep;
if Option.is_some (UnionRep.check_enum rep) then
rec_flow_t ~use_op:unknown_use cx trace (l, OpenT tout)
else
let dro_union =
map_union
~f:(fun cx trace t tout ->
let tout = open_tvar tout in
rec_flow cx trace (t, DeepReadOnlyT (tout, (dro_loc, dro_type))))
cx
trace
rep
reason
in
rec_flow_t ~use_op:unknown_use cx trace (dro_union, OpenT tout)
| (UnionT (reason, rep), HooklikeT tout) ->
if not (UnionRep.is_optimized_finally rep) then
UnionRep.optimize_enum_only ~flatten:(Type_mapper.union_flatten cx) rep;
if Option.is_some (UnionRep.check_enum rep) then
rec_flow_t ~use_op:unknown_use cx trace (l, OpenT tout)
else
let hook_union =
map_union
~f:(fun cx trace t tout ->
let tout = open_tvar tout in
rec_flow cx trace (t, HooklikeT tout))
cx
trace
rep
reason
in
rec_flow_t ~use_op:unknown_use cx trace (hook_union, OpenT tout)
| (IntersectionT (reason, rep), DeepReadOnlyT (tout, (dro_loc, dro_type))) ->
let dro_inter =
map_inter
~f:(fun cx trace t tout ->
let tout = open_tvar tout in
rec_flow cx trace (t, DeepReadOnlyT (tout, (dro_loc, dro_type))))
cx
trace
rep
reason
in
rec_flow_t ~use_op:unknown_use cx trace (dro_inter, OpenT tout)
| (DefT (r, ObjT ({ Type.flags; _ } as o)), DeepReadOnlyT (tout, (dro_loc, dro_type))) ->
rec_flow_t
~use_op:unknown_use
cx
trace
( DefT
(r, ObjT { o with Type.flags = { flags with react_dro = Some (dro_loc, dro_type) } }),
OpenT tout
)
| ( DefT (r, ArrT (TupleAT { elem_t; elements; arity; inexact; react_dro = _ })),
DeepReadOnlyT (tout, (dro_loc, dro_type))
) ->
rec_flow_t
~use_op:unknown_use
cx
trace
( DefT
( r,
ArrT
(TupleAT
{ elem_t; elements; arity; inexact; react_dro = Some (dro_loc, dro_type) }
)
),
OpenT tout
)
| ( DefT (r, ArrT (ArrayAT { elem_t; tuple_view; react_dro = _ })),
DeepReadOnlyT (tout, (dro_loc, dro_type))
) ->
rec_flow_t
~use_op:unknown_use
cx
trace
( DefT (r, ArrT (ArrayAT { elem_t; tuple_view; react_dro = Some (dro_loc, dro_type) })),
OpenT tout
)
| (DefT (r, ArrT (ROArrayAT (t, _))), DeepReadOnlyT (tout, (dro_loc, dro_type))) ->
rec_flow_t
~use_op:unknown_use
cx
trace
(DefT (r, ArrT (ROArrayAT (t, Some (dro_loc, dro_type)))), OpenT tout)
| (DefT (r, InstanceT ({ inst; _ } as instance)), DeepReadOnlyT (tout, react_dro)) ->
rec_flow_t
~use_op:unknown_use
cx
trace
( DefT
(r, InstanceT { instance with inst = { inst with inst_react_dro = Some react_dro } }),
OpenT tout
)
| (DefT (r, FunT (s, ({ effect = ArbitraryEffect; _ } as funtype))), HooklikeT tout) ->
rec_flow_t
~use_op:unknown_use
cx
trace
(DefT (r, FunT (s, { funtype with effect = AnyEffect })), OpenT tout)
| ( DefT
( rp,
PolyT
( { t_out = DefT (r, FunT (s, ({ effect = ArbitraryEffect; _ } as funtype))); _ }
as poly
)
),
HooklikeT tout
) ->
rec_flow_t
~use_op:unknown_use
cx
trace
( DefT
( rp,
PolyT
{ poly with t_out = DefT (r, FunT (s, { funtype with effect = AnyEffect })) }
),
OpenT tout
)
| (DefT (r, ObjT ({ call_t = Some id; _ } as obj)), HooklikeT tout) ->
let t =
match Context.find_call cx id with
| DefT (rf, FunT (s, ({ effect = ArbitraryEffect; _ } as funtype))) ->
let call = DefT (rf, FunT (s, { funtype with effect = AnyEffect })) in
let id = Context.make_call_prop cx call in
DefT (r, ObjT { obj with call_t = Some id })
| DefT
( rp,
PolyT
( {
t_out = DefT (rf, FunT (s, ({ effect = ArbitraryEffect; _ } as funtype)));
_;
} as poly
)
) ->
let call =
DefT
( rp,