-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
annotation_inference.ml
1418 lines (1299 loc) · 56.5 KB
/
annotation_inference.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.
*)
open Reason
open Type_subst
open Type
open Type.AConstraint
open TypeUtil
let object_like_op = function
| Annot_SpecializeT _
| Annot_ThisSpecializeT _
| Annot_UseT_TypeT _
| Annot_ConcretizeForImportsExports _
| Annot_CJSRequireT _
| Annot_ImportTypeofT _
| Annot_ImportNamedT _
| Annot_ImportDefaultT _
| Annot_ImportModuleNsT _
| Annot_CJSExtractNamedExportsT _
| Annot_ExportNamedT _
| Annot_ExportTypeT _
| Annot_AssertExportIsTypeT _
| Annot_CopyNamedExportsT _
| Annot_CopyTypeExportsT _
| Annot_ElemT _
| Annot_GetStaticsT _
| Annot_MakeExactT _
| Annot_MixinT _
| Annot_ObjKitT _
| Annot_ObjTestProtoT _
| Annot_ArithT _
| Annot_UnaryArithT _
| Annot_NotT _
| Annot_ObjKeyMirror _
| Annot_ObjMapConst _
| Annot_GetKeysT _
| Annot_GetEnumT _
| Annot_DeepReadOnlyT _
| Annot_ToStringT _
| Annot__Future_added_value__ _ ->
false
| Annot_GetTypeFromNamespaceT _
| Annot_GetPropT _
| Annot_GetElemT _
| Annot_LookupT _
| Annot_ObjRestT _
| Annot_GetValuesT _ ->
true
let primitive_promoting_op = function
| Annot_GetPropT _
| Annot_GetElemT _
| Annot_LookupT _ ->
true
(* TODO: enumerate all use types *)
| _ -> false
let function_like_op op = object_like_op op
let get_fully_resolved_type cx id =
let (_, constraints) = Context.find_constraints cx id in
match constraints with
| Constraint.FullyResolved s -> Context.force_fully_resolved_tvar cx s
| Constraint.Resolved _
| Constraint.Unresolved _ ->
failwith "unexpected unresolved constraint in annotation inference"
let get_builtin_typeapp cx reason x targs =
let t = Flow_js_utils.lookup_builtin_type cx x reason in
TypeUtil.typeapp ~from_value:false ~use_desc:false reason t targs
module type S = sig
val mk_type_reference : Context.t -> type_t_kind:Type.type_t_kind -> Reason.t -> Type.t -> Type.t
val mk_instance :
Context.t -> ?type_t_kind:Type.type_t_kind -> reason -> ?use_desc:bool -> Type.t -> Type.t
val reposition : Context.t -> ALoc.t -> Type.t -> Type.t
val get_prop :
Context.t -> Type.use_op -> Reason.t -> ?op_reason:Reason.t -> Reason.name -> Type.t -> Type.t
val get_elem : Context.t -> Type.use_op -> Reason.t -> key:Type.t -> Type.t -> Type.t
val get_builtin_type : Context.t -> reason -> ?use_desc:bool -> string -> Type.t
val qualify_type :
Context.t -> Type.use_op -> Reason.t -> op_reason:Reason.t -> Reason.name -> Type.t -> Type.t
val assert_export_is_type : Context.t -> Reason.t -> string -> Type.t -> Type.t
val mk_sig_tvar : Context.t -> Reason.t -> Type.t Lazy.t -> Type.t
val cjs_require : Context.t -> Type.t -> Reason.t -> bool -> bool -> Type.t
val export_named :
Context.t ->
Reason.reason ->
Type.export_kind ->
Type.named_symbol NameUtils.Map.t ->
Type.named_symbol NameUtils.Map.t ->
Type.t ->
Type.t
val cjs_extract_named_exports : Context.t -> Reason.reason -> Type.moduletype -> Type.t -> Type.t
val import_default :
Context.t -> Reason.t -> Type.import_kind -> string -> string -> bool -> Type.t -> Type.t
val import_named :
Context.t -> Reason.t -> Type.import_kind -> string -> string -> bool -> Type.t -> Type.t
val import_ns : Context.t -> Reason.t -> bool -> Type.t -> Type.t
val import_typeof : Context.t -> Reason.t -> string -> Type.t -> Type.t
val specialize :
Context.t ->
Type.t ->
Type.use_op ->
Reason.t ->
Reason.t ->
Type.t list Base.Option.t ->
Type.t
val copy_named_exports : Context.t -> from_ns:Type.t -> Reason.t -> module_t:Type.t -> Type.t
val copy_type_exports : Context.t -> from_ns:Type.t -> Reason.t -> module_t:Type.t -> Type.t
val arith : Context.t -> Reason.t -> Type.t -> Type.t -> Type.ArithKind.t -> Type.t
val unary_arith : Context.t -> Reason.t -> Type.t -> Type.UnaryArithKind.t -> Type.t
val unary_not : Context.t -> Reason.t -> Type.t -> Type.t
val mixin : Context.t -> Reason.t -> Type.t -> Type.t
val object_spread :
Context.t ->
Type.use_op ->
Reason.reason ->
Type.Object.Spread.target ->
Type.Object.Spread.state ->
Type.t ->
Type.t
val obj_test_proto : Context.t -> Reason.t -> Type.t -> Type.t
val obj_rest : Context.t -> Reason.t -> string list -> Type.t -> Type.t
val arr_rest : Context.t -> Type.use_op -> Reason.t -> int -> Type.t -> Type.t
val set_dst_cx : Context.t -> unit
val elab_t : Context.t -> ?seen:ISet.t -> Type.t -> Type.AConstraint.op -> Type.t
end
module rec ConsGen : S = struct
(* Annotation inference is performed in the context of the definition module (this
* is what the input `cx` in elab_t etc. represents). However, in order to be
* able to raise errors during annotation inference, we need to have access to the
* destination context. This is what this reference is for. `dst_cx_ref` is set
* Check_serivce.mk_check_file once per file right after the destination context
* is created. *)
let dst_cx_ref = ref None
let set_dst_cx cx = dst_cx_ref := Some cx
(* Errors created with [error_unsupported] are actually reported. Compare this to
* errors created with Flow_js_utils.add_output which are recorded in the context
* of the source of the annotations, and are therefore ignored. This function checks
* that dst_cx_ref has been set and uses that as the target context.
*
* The only kind of errors that are reported here are "unsupported" cases. These
* are mostly cases that rely on subtyping, which is not implemented here; most
* commonly evaluating call-like EvalTs and speculation. *)
let error_unsupported_reason ?suggestion cx reason reason_op =
let loc = Reason.loc_of_reason reason_op in
let msg = Error_message.EAnnotationInference (loc, reason_op, reason, suggestion) in
(match !dst_cx_ref with
| None -> assert false
| Some dst_cx -> Flow_js_utils.add_annot_inference_error ~src_cx:cx ~dst_cx msg);
AnyT.error reason_op
let error_unsupported ?suggestion cx reason op =
let reason_op = AConstraint.display_reason_of_op op in
error_unsupported_reason ?suggestion cx reason reason_op
let error_recursive cx reason =
let loc = Reason.loc_of_reason reason in
let msg = Error_message.ETrivialRecursiveDefinition (loc, reason) in
(match !dst_cx_ref with
| None -> assert false
| Some dst_cx -> Flow_js_utils.add_annot_inference_error ~src_cx:cx ~dst_cx msg);
AnyT.error reason
let error_internal_reason cx msg reason_op =
let loc = Reason.loc_of_reason reason_op in
let msg = Error_message.(EInternal (loc, UnexpectedAnnotationInference msg)) in
(match !dst_cx_ref with
| None -> assert false
| Some dst_cx -> Flow_js_utils.add_annot_inference_error ~src_cx:cx ~dst_cx msg);
AnyT.error reason_op
let error_internal cx msg op =
let reason_op = AConstraint.display_reason_of_op op in
error_internal_reason cx msg reason_op
let dummy_trace = DepthTrace.dummy_trace
(* Repositioning does not seem to have any perceptible impact in annotation
* inference. Instead of replicating the convoluted implementation of Flow_js
* here, we just return the same type intact. *)
let reposition _cx _loc t = t
(*****************)
(* Instantiation *)
(*****************)
module Instantiation_helper = struct
let cache_instantiate _cx _trace ~use_op:_ ?cache:_ _typeparam _reason_op _reason_tapp t = t
(* We will not be solving implicit instantiation problems here. The only case
* where we will need to use this function is when a PolyT needs to be used
* as a monomorphic type. In this case, the only sensible thing to do is to
* use the bound of each parameter as the argument to the intantiation. *)
let mk_targ _cx typeparam _reason_op _reason_tapp = typeparam.Type.bound
let is_subtype _cx _trace ~use_op:_ (_t1, _t2) = ()
let unify _cx _trace ~use_op:_ (_t1, _t2) = ()
let reposition cx ?trace:_ loc t = reposition cx loc t
end
module InstantiationKit = Flow_js_utils.Instantiation_kit (Instantiation_helper)
let instantiate_poly cx = InstantiationKit.instantiate_poly cx dummy_trace
let mk_typeapp_of_poly cx = InstantiationKit.mk_typeapp_of_poly cx dummy_trace
(***********)
(* Imports *)
(***********)
module Import_export_helper : Flow_js_utils.Import_export_helper_sig with type r = Type.t = struct
type r = Type.t
let reposition cx loc t = reposition cx loc t
let return _cx t = t
let export_named cx (reason, values, types, kind) t =
ConsGen.export_named cx reason kind values types t
let export_named_fresh_var = export_named
let export_type cx (reason, name_loc, preferred_def_locs, export_name, target_module_t) export_t
=
ConsGen.elab_t
cx
export_t
(Annot_ExportTypeT { reason; name_loc; preferred_def_locs; export_name; target_module_t })
let cjs_extract_named_exports cx (reason, local_module) t =
ConsGen.cjs_extract_named_exports cx reason local_module t
end
let assert_import_is_value _cx _reason _name _export_t = ()
let with_concretized_type cx r f t = ConsGen.elab_t cx t (Annot_ConcretizeForImportsExports (r, f))
module CJSRequireTKit = Flow_js_utils.CJSRequireTKit
module ImportModuleNsTKit = Flow_js_utils.ImportModuleNsTKit
module ImportDefaultTKit = Flow_js_utils.ImportDefaultTKit
module ImportNamedTKit = Flow_js_utils.ImportNamedTKit
module ImportTypeofTKit = Flow_js_utils.ImportTypeofTKit
module ExportNamedTKit = Flow_js_utils.ExportNamedTKit
module AssertExportIsTypeTKit = Flow_js_utils.AssertExportIsTypeT_kit (Import_export_helper)
module CopyNamedExportsTKit = Flow_js_utils.CopyNamedExportsT_kit (Import_export_helper)
module CopyTypeExportsTKit = Flow_js_utils.CopyTypeExportsT_kit (Import_export_helper)
module ExportTypeTKit = Flow_js_utils.ExportTypeT_kit (Import_export_helper)
module CJSExtractNamedExportsTKit =
Flow_js_utils.CJSExtractNamedExportsT_kit (Import_export_helper)
(***********)
(* GetProp *)
(***********)
module Get_prop_helper = struct
type r = Type.t
let cg_lookup_ cx use_op t reason_op propref objt =
ConsGen.elab_t cx t (Annot_LookupT (reason_op, use_op, propref, objt))
let error_type _ _ = AnyT.error
(* We could have just returned `t` here. The OpenT indirection is for compatibility
* with Flow_js. Specifically, without the OpenT the transformation in
* https://github.com/facebook/flow/blob/8c3825a1be188e9ade4ad4ed515361bb28c65d8a/src/typing/flow_js.ml#L1744-L1755
* would fire, causing a divergence in the behavior of this module and Flow_js. *)
let return cx ~use_op:_ _trace t =
match t with
| OpenT _ -> t
| _ -> Tvar.mk_fully_resolved cx (reason_of_t t) t
(* We will not be doing subtyping checks in annotation inference. *)
let dict_read_check _ _ ~use_op:_ _ = ()
let reposition cx ?trace:_ loc t = reposition cx loc t
let cg_lookup cx _trace ~obj_t ~method_accessible:_ t (reason_op, _kind, propref, use_op, _ids)
=
cg_lookup_ cx use_op t reason_op propref obj_t
let cg_get_prop cx _trace t (use_op, access_reason, _, (prop_reason, name)) =
ConsGen.elab_t
cx
t
(Annot_GetPropT (access_reason, use_op, mk_named_prop ~reason:prop_reason name))
let mk_react_dro cx _use_op (props_loc, dro_t) t =
ConsGen.elab_t cx t (Annot_DeepReadOnlyT (reason_of_t t, props_loc, dro_t))
let mk_hooklike _cx _use_op t = t
end
module GetPropTKit = Flow_js_utils.GetPropT_kit (Get_prop_helper)
(** [ensure_annot_resolved cx reason id] ensures that the annotation constraint
* associated with [id] has been resolved. If the respective constraint is already
* resolved then it returns immediately. Otherwise, it resolves [id] immediately
* to the 'any' type. In the case of an [Anno_op (_, _, dep_id)] constraint we also
* update the "dependents" set of [dep_id], so that we don't attempt to resolve
* [id] once again when [dep_id] gets resolved.
*)
let rec ensure_annot_resolved cx reason id =
let module A = Type.AConstraint in
match Context.find_avar_opt cx id with
| None -> get_fully_resolved_type cx id
| Some (A.Annot_unresolved _) ->
let t = error_recursive cx reason in
resolve_id cx reason id t;
t
| Some (A.Annot_op { id = dep_id; _ }) ->
let dep_constraint = Context.find_avar cx dep_id in
A.update_deps_of_constraint dep_constraint ~f:(fun deps ->
ISet.filter (fun id2 -> id <> id2) deps
);
let t = error_recursive cx reason in
resolve_id cx reason id t;
t
and mk_lazy_tvar cx reason f =
let id = Reason.mk_id () in
let tvar = OpenT (reason, id) in
let t =
lazy
( Avar.unresolved_with_id cx id reason;
f id;
(* Before forcing the type constraint of [id] we need to make sure the
* respective annotation constraint has been processed. If not we infer
* the empty type. *)
ensure_annot_resolved cx reason id
)
in
let node =
Constraint.create_root
(Constraint.FullyResolved (Constraint.ForcingState.of_lazy_t ~error_reason:reason t))
in
Context.add_tvar cx id node;
tvar
and mk_sig_tvar cx reason (resolved : Type.t Lazy.t) =
let f id =
let t = Lazy.force resolved in
resolve_id cx reason id t
in
mk_lazy_tvar cx reason f
(** [resolve_id cx id1 t] resolves an annotation tvar [id1] to a type [t] *
* - If [t] is a concrete type, we mark [id1] as a resolved annotation tvar and
* record it as fully resolved in the type graph. *
* - If [t] is an OpenT (_, id2), then we unify [id1] and [id2]. (See merge_ids.)
*)
and resolve_id cx reason id t =
match Context.find_avar_opt cx id with
| None ->
(* The avar is already resolved. This happens when the avar is recursively
* reachable and becomes resolved to any. *)
()
| Some constraints1 ->
let t =
match t with
| Type.OpenT (_, id2) -> ensure_annot_resolved cx reason id2
| _ -> t
in
Context.remove_avar cx id;
Context.add_tvar
cx
id
Type.Constraint.(create_root (FullyResolved (ForcingState.of_non_lazy_t t)));
let dependents1 = deps_of_constraint constraints1 in
resolve_dependent_set cx reason dependents1 t
and resolve_dependent_set cx reason dependents t =
Context.iter_annot_dependent_set
cx
(fun id op -> resolve_id cx reason id (elab_t cx t op))
dependents
and elab_open cx ~seen reason id op =
if ISet.mem id seen then
error_recursive cx reason
else
let module A = Type.AConstraint in
match Context.find_avar_opt cx id with
| None ->
(* [id] may refer to a lazily resolved constraint (e.g. created through
* [mk_lazy_tvar]). To protect against trying to force recursive lazy
* structures, we introduce a lazy indirection around the resulting
* constraint. An example that would have cause this unwanted behavior is
*
* declare var x: {
* p: number;
* q: typeof (x.p);
* };
*
* This lazy indirection allows the type of `x` to be resolved, before we
* attempt to force the constraint for `x.p`. *)
let resolved =
lazy
((* resolved ids definitelly appear in the type graph *)
let t = get_fully_resolved_type cx id in
elab_t cx ~seen:(ISet.add id seen) t op
)
in
mk_sig_tvar cx (AConstraint.reason_of_op op) resolved
| Some (A.Annot_unresolved _)
| Some (A.Annot_op _) ->
let fresh_id = Avar.constrained cx op id in
OpenT (reason, fresh_id)
and elab_t cx ?(seen = ISet.empty) t op =
match (t, op) with
| (EvalT (t, TypeDestructorT (use_op, reason, ReadOnlyType), _), _) ->
let t = make_readonly cx use_op reason t in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, reason, ReactDRO (dro_loc, dro_kind)), _), _) ->
let t = elab_t cx t (Annot_DeepReadOnlyT (reason, dro_loc, dro_kind)) in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, _, MakeHooklike), _), _) -> t
| (EvalT (t, TypeDestructorT (use_op, reason, PartialType), _), _) ->
let t = make_partial cx use_op reason t in
elab_t cx t op
| (EvalT (t, TypeDestructorT (use_op, reason, RequiredType), _), _) ->
let t = make_required cx use_op reason t in
elab_t cx t op
| (EvalT (t, TypeDestructorT (use_op, reason, SpreadType (target, todo_rev, head_slice)), _), _)
->
let state =
{
Object.Spread.todo_rev;
acc =
Base.Option.value_map ~f:(fun x -> [Object.Spread.InlineSlice x]) ~default:[] head_slice;
spread_id = Reason.mk_id ();
union_reason = None;
curr_resolve_idx = 0;
}
in
let t = object_spread cx use_op reason target state t in
elab_t cx t op
| (EvalT (t, TypeDestructorT (use_op, reason, RestType (options, r)), _), _) ->
let state = Object.Rest.One r in
let t = object_rest cx use_op reason options state t in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, reason, TypeMap ObjectKeyMirror), _), _) ->
let t = elab_t cx t (Annot_ObjKeyMirror reason) in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, reason, TypeMap (ObjectMapConst t')), _), _) ->
let t = elab_t cx t (Annot_ObjMapConst (reason, t')) in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, reason, ValuesType), _), _) ->
let t = elab_t cx t (Annot_GetValuesT reason) in
elab_t cx t op
| (EvalT (t, TypeDestructorT (use_op, reason, PropertyType { name }), _), _) ->
let reason_op = replace_desc_reason (RProperty (Some name)) reason in
let t =
elab_t
cx
t
(Annot_GetPropT (reason_op, use_op, Named { reason; name; from_indexed_access = true }))
in
elab_t cx t op
| (EvalT (t, TypeDestructorT (use_op, reason, ElementType { index_type }), _), _) ->
let t = elab_t cx t (Annot_GetElemT (reason, use_op, index_type)) in
elab_t cx t op
| (EvalT (t, TypeDestructorT (_, reason, EnumType), _), _) ->
let t = elab_t cx t (Annot_GetEnumT reason) in
elab_t cx t op
| (EvalT (_, TypeDestructorT (_, reason, TypeMap (ObjectMap _)), _), _) ->
error_unsupported ~suggestion:"$ObjMapConst" cx reason op
| (EvalT (_, TypeDestructorT (_, reason, TypeMap (ObjectMapi _)), _), _) ->
error_unsupported ~suggestion:"$KeyMirror" cx reason op
| (EvalT (_, TypeDestructorT (_, reason, _), _), _) -> error_unsupported cx reason op
| (OpenT (reason, id), _) -> elab_open cx ~seen reason id op
| (InternalT (ExtendsT (reason, _, _) | EnforceUnionOptimized reason), _) ->
error_unsupported cx reason op
| (AnnotT (r, t, _), _) ->
let t = reposition cx (loc_of_reason r) t in
elab_t cx ~seen t op
(*********************************************************************)
(* UseT TypeT (runtime types derive static types through annotation) *)
(*********************************************************************)
(* First handle catch-all cases of subtyping_kit.ml *)
| ((MaybeT (reason, _) | OptionalT { reason; _ }), Annot_UseT_TypeT _) ->
error_unsupported cx reason op
| (ThisTypeAppT (reason_tapp, c, this, ts), Annot_UseT_TypeT _) ->
let reason_op = Type.AConstraint.reason_of_op op in
let tc = specialize_class cx c reason_op reason_tapp ts in
let t = this_specialize cx reason_tapp this tc in
elab_t cx t op
| ( TypeAppT
{ reason = reason_tapp; use_op = typeapp_use_op; type_; targs; from_value; use_desc = _ },
Annot_UseT_TypeT _
) ->
(* NOTE omitting TypeAppExpansion.push_unless_loop check. *)
let reason_op = Type.AConstraint.reason_of_op op in
let t =
mk_typeapp_instance
cx
~use_op:typeapp_use_op
~reason_op
~reason_tapp
~from_value
type_
targs
in
elab_t cx t op
| ( DefT (_, PolyT { tparams = ids; t_out = DefT (_, ReactAbstractComponentT _) as t; _ }),
Annot_UseT_TypeT (reason_op, RenderTypeKind)
) ->
let subst_map =
Nel.fold_left
(fun acc tparam -> Subst_name.Map.add tparam.name (AnyT.untyped reason_op) acc)
Subst_name.Map.empty
ids
in
let t_ = subst cx subst_map t in
elab_t cx t_ op
| (DefT (reason_tapp, PolyT { tparams_loc; tparams = ids; _ }), Annot_UseT_TypeT (reason, _)) ->
Flow_js_utils.add_output
cx
(Error_message.EMissingTypeArgs
{
reason_op = reason;
reason_tapp;
arity_loc = tparams_loc;
min_arity = Flow_js_utils.poly_minimum_arity ids;
max_arity = Nel.length ids;
}
);
AnyT.error reason
| ( DefT (class_r, ClassT (ThisInstanceT (r, i, is_this, this_name))),
Annot_UseT_TypeT (reason, _)
) ->
let c =
DefT (class_r, ClassT (Flow_js_utils.fix_this_instance cx reason (r, i, is_this, this_name)))
in
elab_t cx c op
| (DefT (_, ClassT it), Annot_UseT_TypeT (reason, _)) ->
(* a class value annotation becomes the instance type *)
reposition cx (loc_of_reason reason) it
| ((DefT (_, ReactAbstractComponentT _) as l), Annot_UseT_TypeT (reason, _)) ->
(* a component syntax value annotation becomes an element of that component *)
get_builtin_typeapp cx reason "React$Element" [l]
| (DefT (_, TypeT (_, l)), Annot_UseT_TypeT _) -> l
| (DefT (_, EnumObjectT { enum_value_t; _ }), Annot_UseT_TypeT _) ->
(* an enum object value annotation becomes the enum value type *)
enum_value_t
| (DefT (enum_reason, EnumValueT _), Annot_UseT_TypeT (reason, _)) ->
Flow_js_utils.add_output cx Error_message.(EEnumMemberUsedAsType { reason; enum_reason });
AnyT.error reason
| (l, Annot_UseT_TypeT (reason_use, _)) ->
(match l with
(* Short-circut as we already error on the unresolved name. *)
| AnyT (_, AnyError _) -> ()
| AnyT _ -> Flow_js_utils.add_output cx Error_message.(EAnyValueUsedAsType { reason_use })
| _ -> Flow_js_utils.add_output cx Error_message.(EValueUsedAsType { reason_use }));
AnyT.error reason_use
| (l, Annot_ConcretizeForImportsExports (_, f)) -> f l
(*******************)
(* `import typeof` *)
(*******************)
| (_, Annot_ImportTypeofT (reason, export_name)) ->
ImportTypeofTKit.on_concrete_type cx reason export_name t
(******************)
(* Module exports *)
(******************)
| ( ModuleT m,
Annot_ExportNamedT { reason = _; value_exports_tmap; type_exports_tmap; export_kind }
) ->
ExportNamedTKit.mod_ModuleT cx (value_exports_tmap, type_exports_tmap, export_kind) m;
ModuleT m
| (_, Annot_AssertExportIsTypeT (_, name)) -> AssertExportIsTypeTKit.on_concrete_type cx name t
| (ModuleT m, Annot_CopyNamedExportsT (reason, target_module_t)) ->
CopyNamedExportsTKit.on_ModuleT cx (reason, target_module_t) m
| (ModuleT m, Annot_CopyTypeExportsT (reason, target_module_t)) ->
CopyTypeExportsTKit.on_ModuleT cx (reason, target_module_t) m
| (_, Annot_ExportTypeT { reason; name_loc; preferred_def_locs; export_name; target_module_t })
->
ExportTypeTKit.on_concrete_type
cx
(reason, name_loc, preferred_def_locs, export_name, target_module_t)
t
| (AnyT (lreason, _), Annot_CopyNamedExportsT (reason, target_module)) ->
CopyNamedExportsTKit.on_AnyT cx lreason (reason, target_module)
| (AnyT (lreason, _), Annot_CopyTypeExportsT (reason, target_module)) ->
CopyTypeExportsTKit.on_AnyT cx lreason (reason, target_module)
| (_, Annot_CJSExtractNamedExportsT (reason, local_module)) ->
CJSExtractNamedExportsTKit.on_concrete_type cx (reason, local_module) t
(******************)
(* Module imports *)
(******************)
| (ModuleT m, Annot_CJSRequireT { reason; is_strict; legacy_interop }) ->
CJSRequireTKit.on_ModuleT
cx
~reposition:(fun _ _ t -> t)
(reason, is_strict, legacy_interop)
m
| (ModuleT m, Annot_ImportModuleNsT (reason, is_strict)) ->
ImportModuleNsTKit.on_ModuleT cx (reason, is_strict) m
| (ModuleT m, Annot_ImportDefaultT (reason, import_kind, local, is_strict)) ->
let (_name_loc_opt, t) =
ImportDefaultTKit.on_ModuleT
cx
~assert_import_is_value
~with_concretized_type
(reason, import_kind, local, is_strict)
m
in
t
| (ModuleT m, Annot_ImportNamedT (reason, import_kind, export_name, module_name, is_strict)) ->
let (_name_loc_opt, t) =
ImportNamedTKit.on_ModuleT
cx
~assert_import_is_value
~with_concretized_type
(reason, import_kind, export_name, module_name, is_strict)
m
in
t
| (AnyT (lreason, src), (Annot_CJSRequireT { reason; _ } | Annot_ImportModuleNsT (reason, _)))
->
Flow_js_utils.check_untyped_import cx ImportValue lreason reason;
AnyT.why src reason
| (AnyT (lreason, src), Annot_ImportDefaultT (reason, import_kind, _, _)) ->
Flow_js_utils.check_untyped_import cx import_kind lreason reason;
AnyT.why src reason
| (AnyT (lreason, src), Annot_ImportNamedT (reason, import_kind, _, _, _)) ->
Flow_js_utils.check_untyped_import cx import_kind lreason reason;
AnyT.why src reason
(************************************)
(* Wildcards (idx, maybe, optional) *)
(************************************)
| (MaybeT (reason, _), _)
| (OptionalT { reason; _ }, _) ->
(* These are rare in practice. Will consider adding support if we hit this
* error case. *)
error_unsupported cx reason op
(*********************)
(* Type applications *)
(*********************)
| (ThisTypeAppT (reason_tapp, c, this, ts), _) ->
let reason_op = Type.AConstraint.reason_of_op op in
let tc = specialize_class cx c reason_op reason_tapp ts in
let t = this_specialize cx reason_tapp this tc in
elab_t cx t op
| ( TypeAppT
{ reason = reason_tapp; use_op = typeapp_use_op; type_; targs; from_value; use_desc = _ },
_
) ->
(* NOTE omitting TypeAppExpansion.push_unless_loop check. *)
let reason_op = Type.AConstraint.reason_of_op op in
let t =
mk_typeapp_instance
cx
~use_op:typeapp_use_op
~reason_op
~reason_tapp
~from_value
type_
targs
in
elab_t cx t op
(****************)
(* Opaque types *)
(****************)
| (OpaqueT (_, { super_t = Some super_t; _ }), Annot_ToStringT { reason; _ }) ->
elab_t cx super_t (Annot_ToStringT { orig_t = Some t; reason })
| (OpaqueT (r, { underlying_t = Some t; _ }), _)
when ALoc.source (loc_of_reason r) = ALoc.source (def_loc_of_reason r) ->
elab_t cx ~seen t op
(********)
(* Keys *)
(********)
| (KeysT _, Annot_ToStringT _) -> t
| (KeysT (reason, t), _) ->
let t = elab_t cx t (Annot_GetKeysT reason) in
elab_t cx t op
| (DefT (_, ObjT { flags; props_tmap; _ }), Annot_GetKeysT reason_op) ->
let dict_t = Obj_type.get_dict_opt flags.obj_kind in
(* flow the union of keys of l to keys *)
let keylist = Flow_js_utils.keylist_of_props (Context.find_props cx props_tmap) reason_op in
let keylist =
match dict_t with
| None -> keylist
| Some { key; _ } ->
let key = elab_t cx key (Annot_ToStringT { orig_t = None; reason = reason_op }) in
key :: keylist
in
union_of_ts reason_op keylist
| (DefT (_, InstanceT { inst; _ }), Annot_GetKeysT reason_op) ->
(* methods are not enumerable, so only walk fields *)
let own_props = Context.find_props cx inst.own_props in
let keylist = Flow_js_utils.keylist_of_props own_props reason_op in
union_of_ts reason_op keylist
| (AnyT _, Annot_GetKeysT reason_op) -> StrT.why reason_op
(***********)
(* $Values *)
(***********)
| (DefT (_, ObjT o), Annot_GetValuesT reason) ->
Flow_js_utils.get_values_type_of_obj_t cx o reason
| (DefT (_, InstanceT { inst = { own_props; inst_dict; _ }; _ }), Annot_GetValuesT reason) ->
Flow_js_utils.get_values_type_of_instance_t cx own_props inst_dict reason
(* Any will always be ok *)
| (AnyT (_, src), Annot_GetValuesT reason) -> AnyT.why src reason
(********************************)
(* Union and intersection types *)
(********************************)
| (UnionT (reason, rep), Annot_MakeExactT reason_op) ->
let ts = UnionRep.members rep in
let f t = ExactT (reason_op, t) in
let ts' = Base.List.map ts ~f in
let reason' = repos_reason (loc_of_reason reason_op) reason in
union_of_ts reason' ts'
| (UnionT _, Annot_ObjKitT (reason, use_op, resolve_tool, tool)) ->
object_kit_concrete cx use_op op reason resolve_tool tool t
| (UnionT (_, rep), _) ->
let reason = Type.AConstraint.reason_of_op op in
let ts = UnionRep.members rep in
let ts = Base.List.map ~f:(fun t -> elab_t cx ~seen t op) ts in
union_of_ts reason ts
| (IntersectionT _, Annot_ObjKitT (reason, use_op, resolve_tool, tool)) ->
object_kit_concrete cx use_op op reason resolve_tool tool t
| (IntersectionT (reason, _), _) ->
(* Handling intersections as inputs would require use of speculation. Instead,
* we ask the user to provide a simpler type. *)
error_unsupported cx reason op
(*************)
(* Unary not *)
(*************)
(* any propagation *)
| (AnyT _, Annot_NotT _) -> t
(* !x when x is of unknown truthiness *)
| (DefT (_, BoolT None), Annot_NotT reason)
| (DefT (_, StrT AnyLiteral), Annot_NotT reason)
| (DefT (_, NumT AnyLiteral), Annot_NotT reason) ->
BoolT.at (loc_of_reason reason)
(* !x when x is falsy *)
| (DefT (_, BoolT (Some false)), Annot_NotT reason)
| (DefT (_, SingletonBoolT false), Annot_NotT reason)
| (DefT (_, StrT (Literal (_, OrdinaryName ""))), Annot_NotT reason)
| (DefT (_, SingletonStrT (OrdinaryName "")), Annot_NotT reason)
| (DefT (_, NumT (Literal (_, (0., _)))), Annot_NotT reason)
| (DefT (_, SingletonNumT (0., _)), Annot_NotT reason)
| (DefT (_, NullT), Annot_NotT reason)
| (DefT (_, VoidT), Annot_NotT reason) ->
let reason = replace_desc_reason (RBooleanLit true) reason in
DefT (reason, BoolT (Some true))
(* !x when x is truthy *)
| (_, Annot_NotT reason) ->
let reason = replace_desc_reason (RBooleanLit false) reason in
DefT (reason, BoolT (Some false))
(*****************************)
(* Singleton primitive types *)
(*****************************)
| (DefT (reason, NumericStrKeyT (_, s)), _) ->
elab_t cx (DefT (reason, StrT (Literal (None, OrdinaryName s)))) op
| (DefT (reason, SingletonStrT key), _) ->
elab_t cx (DefT (reason, StrT (Literal (None, key)))) op
| (DefT (reason, SingletonNumT lit), _) ->
elab_t cx (DefT (reason, NumT (Literal (None, lit)))) op
| (DefT (reason, SingletonBoolT b), _) -> elab_t cx (DefT (reason, BoolT (Some b))) op
| (NullProtoT reason, _) -> elab_t cx (DefT (reason, NullT)) op
(*********)
(* Exact *)
(*********)
| (ExactT (r, t), _) ->
let t = push_type_alias_reason r t in
let t = make_exact cx r t in
elab_t cx t op
| (DefT (reason_obj, ObjT obj), Annot_MakeExactT reason_op) ->
TypeUtil.make_exact_object ~reason_obj obj ~reason_op
| (AnyT (_, src), Annot_MakeExactT reason_op) -> AnyT.why src reason_op
| (DefT (_, VoidT), Annot_MakeExactT reason_op) -> VoidT.why reason_op
| (DefT (_, EmptyT), Annot_MakeExactT reason_op) -> EmptyT.why reason_op
| (_, Annot_MakeExactT reason_op) ->
Flow_js_utils.add_output cx (Error_message.EUnsupportedExact (reason_op, reason_of_t t));
AnyT.error reason_op
(**********)
(* Mixins *)
(**********)
| ( DefT (class_r, ClassT (ThisInstanceT (inst_r, { inst; _ }, is_this, this_name))),
Annot_MixinT r
) ->
(* A class can be viewed as a mixin by extracting its immediate properties,
* and "erasing" its static and super *)
let static = ObjProtoT r in
let super = ObjProtoT r in
DefT
( class_r,
ClassT
(ThisInstanceT (inst_r, { static; super; implements = []; inst }, is_this, this_name))
)
| ( DefT
( _,
PolyT
{
tparams_loc;
tparams = xs;
t_out =
DefT (class_r, ClassT (ThisInstanceT (inst_r, { inst; _ }, is_this, this_name)));
_;
}
),
Annot_MixinT r
) ->
let static = ObjProtoT r in
let super = ObjProtoT r in
let instance = { static; super; implements = []; inst } in
poly_type
(Type.Poly.generate_id ())
tparams_loc
xs
(DefT (class_r, ClassT (ThisInstanceT (inst_r, instance, is_this, this_name))))
| (AnyT (_, src), Annot_MixinT r) -> AnyT.why src r
(***********************)
(* Type specialization *)
(***********************)
| ( DefT (_, PolyT { tparams_loc; tparams = xs; t_out = t; id }),
Annot_SpecializeT (use_op, reason_op, reason_tapp, ts)
) ->
let ts = Base.Option.value ts ~default:[] in
mk_typeapp_of_poly cx ~use_op ~reason_op ~reason_tapp id tparams_loc xs t ts
| (DefT (_, ClassT _), Annot_SpecializeT (_, _, _, None)) -> t
| (AnyT _, Annot_SpecializeT _) -> t
| (DefT (_, ClassT (ThisInstanceT (r, i, _, this_name))), Annot_ThisSpecializeT (reason, this))
->
let i = subst_instance_type cx (Subst_name.Map.singleton this_name this) i in
reposition cx (loc_of_reason reason) (DefT (r, InstanceT i))
(* this-specialization of non-this-abstracted classes is a no-op *)
| (DefT (_, ClassT i), Annot_ThisSpecializeT (reason, _this)) ->
reposition cx (loc_of_reason reason) i
| (AnyT _, Annot_ThisSpecializeT (reason, _)) -> reposition cx (loc_of_reason reason) t
(**********************)
(* Type instantiation *)
(**********************)
| (DefT (reason_tapp, PolyT { tparams_loc; tparams = ids; t_out = t; _ }), _) ->
let use_op = unknown_use in
let reason_op = Type.AConstraint.reason_of_op op in
let (t, _) = instantiate_poly cx ~use_op ~reason_op ~reason_tapp (tparams_loc, ids, t) in
elab_t cx t op
| (ThisInstanceT (r, i, is_this, this_name), _) ->
let reason = Type.AConstraint.reason_of_op op in
let t = Flow_js_utils.fix_this_instance cx reason (r, i, is_this, this_name) in
elab_t cx t op
(*****************************)
(* React Abstract Components *)
(*****************************)
| (DefT (r, ReactAbstractComponentT _), (Annot_GetPropT _ | Annot_GetElemT _)) ->
let statics = Flow_js_utils.lookup_builtin_type cx "React$AbstractComponentStatics" r in
elab_t cx statics op
(****************)
(* Custom types *)
(****************)
| (DefT (reason, CharSetT _), _) -> elab_t cx (StrT.why reason) op
| (CustomFunT (r, _), _) when function_like_op op -> elab_t cx (FunProtoT r) op
(*****************)
(* ObjTestProtoT *)
(*****************)
| (AnyT (_, src), Annot_ObjTestProtoT reason_op) -> AnyT.why src reason_op
| (DefT (_, NullT), Annot_ObjTestProtoT reason_op) -> NullProtoT.why reason_op
| (_, Annot_ObjTestProtoT reason_op) ->
if Flow_js_utils.object_like t then
reposition cx (loc_of_reason reason_op) t
else
let () =
Flow_js_utils.add_output
cx
(Error_message.EInvalidPrototype (loc_of_reason reason_op, reason_of_t t))
in
ObjProtoT.why reason_op
(***************)
(* Get statics *)
(***************)
| (DefT (_, InstanceT { static; _ }), Annot_GetStaticsT reason_op) ->
reposition cx (loc_of_reason reason_op) static
| (AnyT (_, src), Annot_GetStaticsT reason_op) -> AnyT.why src reason_op
| (ObjProtoT _, Annot_GetStaticsT reason_op) ->
(* ObjProtoT not only serves as the instance type of the root class, but
* also as the statics of the root class. *)
reposition cx (loc_of_reason reason_op) t
(***************)
(* LookupT pt1 *)
(***************)
| ( DefT (_lreason, InstanceT { super; inst; _ }),
Annot_LookupT (reason_op, use_op, (Named _ as propref), objt)
) ->
let react_dro =
match objt with
| DefT (_, ObjT o) -> o.flags.react_dro
| _ -> None
in
(match
GetPropTKit.get_instance_prop
cx
dummy_trace
~use_op
~ignore_dicts:true
inst
propref
reason_op
with
| Some (p, _) ->
GetPropTKit.perform_read_prop_action
cx
dummy_trace
use_op
propref
(Property.type_ p)
reason_op
react_dro
| None -> Get_prop_helper.cg_lookup_ cx use_op super reason_op propref objt)
| (DefT (reason, InstanceT _), Annot_LookupT (_, _, Computed _, _)) ->
error_unsupported cx reason op
| (DefT (_, ObjT o), Annot_LookupT (reason_op, use_op, propref, objt)) ->
let react_dro =
match objt with
| DefT (_, ObjT o) -> o.flags.react_dro
| _ -> None
in
(match GetPropTKit.get_obj_prop cx dummy_trace o propref reason_op with
| Some (p, _) ->
GetPropTKit.perform_read_prop_action cx dummy_trace use_op propref p reason_op react_dro
| None -> Get_prop_helper.cg_lookup_ cx use_op o.proto_t reason_op propref objt)
| (AnyT _, Annot_LookupT (reason_op, _use_op, _propref, _)) -> AnyT.untyped reason_op
(************)
(* DRO *)
(************)
| (DefT (r, ObjT ({ Type.flags; _ } as o)), Annot_DeepReadOnlyT (_, dro_loc, dro_kind)) ->
DefT (r, ObjT { o with Type.flags = { flags with react_dro = Some (dro_loc, dro_kind) } })
| ( DefT (r, ArrT (TupleAT { elem_t; elements; arity; react_dro = _ })),
Annot_DeepReadOnlyT (_, dro_loc, dro_kind)
) ->
DefT (r, ArrT (TupleAT { elem_t; elements; arity; react_dro = Some (dro_loc, dro_kind) }))
| ( DefT (r, ArrT (ArrayAT { elem_t; tuple_view; react_dro = _ })),
Annot_DeepReadOnlyT (_, dro_loc, dro_kind)
) ->
DefT (r, ArrT (ArrayAT { elem_t; tuple_view; react_dro = Some (dro_loc, dro_kind) }))
| (DefT (r, ArrT (ROArrayAT (t, _))), Annot_DeepReadOnlyT (_, dro_loc, dro_kind)) ->
DefT (r, ArrT (ROArrayAT (t, Some (dro_loc, dro_kind))))
(************)
(* ObjRestT *)
(************)
| ( DefT (reason_obj, ObjT { props_tmap; flags = { obj_kind; _ }; _ }),
Annot_ObjRestT (reason_op, xs)
) ->
Flow_js_utils.objt_to_obj_rest
cx
props_tmap
~reachable_targs:[]
~obj_kind
~reason_op
~reason_obj
xs
| (DefT (reason, InstanceT _), Annot_ObjRestT _) ->
(* This implementation relies on unsealed objects and set-prop logic that is
* hard to implement in annotation inference. *)
error_unsupported cx reason op
| (AnyT (_, src), Annot_ObjRestT (reason, _)) -> AnyT.why src reason
| (ObjProtoT _, Annot_ObjRestT (reason, _)) ->
Obj_type.mk_with_proto cx reason ~obj_kind:Exact t
| (DefT (_, (NullT | VoidT)), Annot_ObjRestT (reason, _)) ->
Obj_type.mk ~obj_kind:Exact cx reason
(************************************)