Skip to content

Commit 5555259

Browse files
SamChou19815facebook-github-bot
authored andcommitted
De-functorize ImportTypeTKit and ImportTypeofTKit
Summary: With previous refactors, we cut of a lot of dependency on `Import_export_helper_sig` for these two kits. Instead of relying on a few functions in it, we can just make the caller pass in the required functions. This function opens up the usage of these kits in statement.ml, and the potential to kill `ImportTypeT` and `ImportTypeofT`. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D46205969 fbshipit-source-id: 70b967d8d3a03105ec117a1364bd1d996b7dda46
1 parent d942089 commit 5555259

File tree

5 files changed

+121
-70
lines changed

5 files changed

+121
-70
lines changed

src/typing/annotation_inference.ml

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,13 @@ module rec ConsGen : S = struct
249249
(***********)
250250
(* Imports *)
251251
(***********)
252-
module Import_export_helper = struct
252+
module Import_export_helper : Flow_js_utils.Import_export_helper_sig with type r = Type.t = struct
253253
type r = Type.t
254254

255255
let reposition cx ?trace:_ loc ?desc:_ ?annot_loc:_ t = reposition cx loc t
256256

257257
let return _cx _trace t = t
258258

259-
let with_concretized_type cx r f t =
260-
ConsGen.elab_t cx t (Annot_ConcretizeForImportsExports (r, f))
261-
262259
let export_named cx _trace (reason, named, kind) t = ConsGen.export_named cx reason kind named t
263260

264261
let export_named_fresh_var = export_named
@@ -268,18 +265,17 @@ module rec ConsGen : S = struct
268265

269266
let cjs_extract_named_exports cx _ (reason, local_module) t =
270267
ConsGen.cjs_extract_named_exports cx reason local_module t
268+
end
271269

272-
(* This check is bypassed in annotation inference *)
273-
let assert_import_is_value _cx _trace _reason _name _export_t = ()
270+
let assert_import_is_value _cx _trace _reason _name _export_t = ()
274271

275-
let mk_typeof_annotation = ConsGen.mk_typeof_annotation
276-
end
272+
let with_concretized_type cx r f t = ConsGen.elab_t cx t (Annot_ConcretizeForImportsExports (r, f))
277273

278274
module CJSRequireTKit = Flow_js_utils.CJSRequireT_kit (Import_export_helper)
279275
module ImportModuleNsTKit = Flow_js_utils.ImportModuleNsTKit
280-
module ImportDefaultTKit = Flow_js_utils.ImportDefaultT_kit (Import_export_helper)
281-
module ImportNamedTKit = Flow_js_utils.ImportNamedT_kit (Import_export_helper)
282-
module ImportTypeofTKit = Flow_js_utils.ImportTypeofT_kit (Import_export_helper)
276+
module ImportDefaultTKit = Flow_js_utils.ImportDefaultTKit
277+
module ImportNamedTKit = Flow_js_utils.ImportNamedTKit
278+
module ImportTypeofTKit = Flow_js_utils.ImportTypeofTKit
283279
module ExportNamedTKit = Flow_js_utils.ExportNamedT_kit (Import_export_helper)
284280
module AssertExportIsTypeTKit = Flow_js_utils.AssertExportIsTypeT_kit (Import_export_helper)
285281
module CopyNamedExportsTKit = Flow_js_utils.CopyNamedExportsT_kit (Import_export_helper)
@@ -568,7 +564,13 @@ module rec ConsGen : S = struct
568564
(* `import typeof` *)
569565
(*******************)
570566
| (_, Annot_ImportTypeofT (reason, export_name)) ->
571-
ImportTypeofTKit.on_concrete_type cx dummy_trace reason export_name t
567+
ImportTypeofTKit.on_concrete_type
568+
cx
569+
dummy_trace
570+
~mk_typeof_annotation:ConsGen.mk_typeof_annotation
571+
reason
572+
export_name
573+
t
572574
(******************)
573575
(* Module exports *)
574576
(******************)
@@ -596,11 +598,21 @@ module rec ConsGen : S = struct
596598
| (ModuleT m, Annot_ImportModuleNsT (reason, is_strict)) ->
597599
ImportModuleNsTKit.on_ModuleT cx dummy_trace (reason, is_strict) m
598600
| (ModuleT m, Annot_ImportDefaultT (reason, import_kind, local, is_strict)) ->
599-
ImportDefaultTKit.on_ModuleT cx dummy_trace (reason, import_kind, local, is_strict) m
601+
ImportDefaultTKit.on_ModuleT
602+
cx
603+
dummy_trace
604+
~mk_typeof_annotation:ConsGen.mk_typeof_annotation
605+
~assert_import_is_value
606+
~with_concretized_type
607+
(reason, import_kind, local, is_strict)
608+
m
600609
| (ModuleT m, Annot_ImportNamedT (reason, import_kind, export_name, module_name, is_strict)) ->
601610
ImportNamedTKit.on_ModuleT
602611
cx
603612
dummy_trace
613+
~mk_typeof_annotation:ConsGen.mk_typeof_annotation
614+
~assert_import_is_value
615+
~with_concretized_type
604616
(reason, import_kind, export_name, module_name, is_strict)
605617
m
606618
| (AnyT (lreason, src), (Annot_CJSRequireT { reason; _ } | Annot_ImportModuleNsT (reason, _)))

src/typing/flow_common.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ module type S = sig
305305

306306
val possible_concrete_types_for_inspection : Context.t -> Reason.reason -> Type.t -> Type.t list
307307

308+
val singleton_concrete_type_for_inspection : Context.t -> Reason.reason -> Type.t -> Type.t
309+
308310
val possible_concrete_types_for_computed_props :
309311
Context.t -> Reason.reason -> Type.t -> Type.t list
310312

src/typing/flow_js.ml

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,19 @@ struct
245245
module InstantiationKit = Instantiation_kit (InstantiationHelper)
246246
module ImplicitInstantiationKit = Implicit_instantiation.Kit (FlowJs) (InstantiationHelper)
247247

248-
module Import_export_helper = struct
248+
let assert_import_is_value cx trace reason name export_t =
249+
FlowJs.rec_flow cx trace (export_t, AssertImportIsValueT (reason, name))
250+
251+
let with_concretized_type cx r f t = f (FlowJs.singleton_concrete_type_for_inspection cx r t)
252+
253+
module Import_export_helper :
254+
Flow_js_utils.Import_export_helper_sig with type r = Type.t -> unit = struct
249255
type r = Type.t -> unit
250256

251257
let reposition = FlowJs.reposition
252258

253259
let return cx trace t tout = FlowJs.rec_flow_t cx ~use_op:unknown_use trace (t, tout)
254260

255-
let with_concretized_type cx r f t =
256-
let t' =
257-
match FlowJs.possible_concrete_types_for_inspection cx r t with
258-
| [] -> EmptyT.make r (bogus_trust ())
259-
| [t] -> t
260-
| t1 :: t2 :: ts -> UnionT (r, UnionRep.make t1 t2 ts)
261-
in
262-
f t'
263-
264261
let export_named cx trace (reason, named, kind) module_t tout =
265262
FlowJs.rec_flow cx trace (module_t, Type.ExportNamedT (reason, named, kind, tout))
266263

@@ -278,17 +275,9 @@ struct
278275
Tvar.mk_where cx reason (fun t ->
279276
FlowJs.rec_flow cx trace (proto_t, CJSExtractNamedExportsT (reason, local_module, t))
280277
)
281-
282-
let assert_import_is_value cx trace reason name export_t =
283-
FlowJs.rec_flow cx trace (export_t, AssertImportIsValueT (reason, name))
284-
285-
let mk_typeof_annotation = FlowJs.mk_typeof_annotation
286278
end
287279

288280
module CJSRequireTKit = CJSRequireT_kit (Import_export_helper)
289-
module ImportDefaultTKit = ImportDefaultT_kit (Import_export_helper)
290-
module ImportNamedTKit = ImportNamedT_kit (Import_export_helper)
291-
module ImportTypeofTKit = ImportTypeofT_kit (Import_export_helper)
292281
module ExportNamedTKit = ExportNamedT_kit (Import_export_helper)
293282
module AssertExportIsTypeTKit = AssertExportIsTypeT_kit (Import_export_helper)
294283
module CopyNamedExportsTKit = CopyNamedExportsT_kit (Import_export_helper)
@@ -831,7 +820,15 @@ struct
831820
cx
832821
~use_op:unknown_use
833822
trace
834-
(ImportTypeofTKit.on_concrete_type cx trace reason export_name l, tout)
823+
( ImportTypeofTKit.on_concrete_type
824+
cx
825+
trace
826+
~mk_typeof_annotation:FlowJs.mk_typeof_annotation
827+
reason
828+
export_name
829+
l,
830+
tout
831+
)
835832
(******************)
836833
(* Module exports *)
837834
(******************)
@@ -871,15 +868,33 @@ struct
871868
cx
872869
~use_op:unknown_use
873870
trace
874-
(ImportDefaultTKit.on_ModuleT cx trace (reason, import_kind, local, is_strict) m, tout)
871+
( ImportDefaultTKit.on_ModuleT
872+
cx
873+
trace
874+
~mk_typeof_annotation:FlowJs.mk_typeof_annotation
875+
~assert_import_is_value
876+
~with_concretized_type
877+
(reason, import_kind, local, is_strict)
878+
m,
879+
tout
880+
)
875881
| (ModuleT m, ImportNamedT (reason, import_kind, export_name, module_name, tout, is_strict))
876882
->
877883
let import = (reason, import_kind, export_name, module_name, is_strict) in
878884
FlowJs.rec_flow_t
879885
cx
880886
~use_op:unknown_use
881887
trace
882-
(ImportNamedTKit.on_ModuleT cx trace import m, tout)
888+
( ImportNamedTKit.on_ModuleT
889+
cx
890+
trace
891+
~mk_typeof_annotation:FlowJs.mk_typeof_annotation
892+
~assert_import_is_value
893+
~with_concretized_type
894+
import
895+
m,
896+
tout
897+
)
883898
| (AnyT (lreason, _), CJSRequireT { reason; t_out; _ }) ->
884899
Flow_js_utils.check_untyped_import cx ImportValue lreason reason;
885900
rec_flow_t ~use_op:unknown_use cx trace (reposition_reason cx reason l, t_out)
@@ -9685,6 +9700,12 @@ module rec FlowJs : Flow_common.S = struct
96859700
let possible_concrete_types_for_inspection =
96869701
possible_concrete_types (fun ident -> ConcretizeForInspection ident)
96879702

9703+
let singleton_concrete_type_for_inspection cx reason t =
9704+
match possible_concrete_types_for_inspection cx reason t with
9705+
| [] -> EmptyT.make reason (bogus_trust ())
9706+
| [t] -> t
9707+
| t1 :: t2 :: ts -> UnionT (reason, UnionRep.make t1 t2 ts)
9708+
96889709
let possible_concrete_types_for_computed_props =
96899710
possible_concrete_types (fun ident -> ConcretizeComputedPropsT ident)
96909711
end

src/typing/flow_js.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ val widen_obj_type :
158158

159159
val possible_concrete_types_for_inspection : Context.t -> Reason.reason -> Type.t -> Type.t list
160160

161+
val singleton_concrete_type_for_inspection : Context.t -> Reason.reason -> Type.t -> Type.t
162+
161163
val possible_concrete_types_for_computed_props : Context.t -> Reason.reason -> Type.t -> Type.t list
162164

163165
val resolve_id : Context.t -> int -> Type.t -> unit

src/typing/flow_js_utils.ml

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,6 @@ module type Import_export_helper_sig = sig
11091109
Type.t ->
11101110
Type.t
11111111

1112-
val assert_import_is_value : Context.t -> Type.trace -> Reason.t -> string -> Type.t -> unit
1113-
1114-
val with_concretized_type : Context.t -> Reason.t -> (Type.t -> Type.t) -> Type.t -> Type.t
1115-
11161112
val export_named :
11171113
Context.t ->
11181114
Type.trace ->
@@ -1138,8 +1134,6 @@ module type Import_export_helper_sig = sig
11381134
Context.t -> Type.trace -> Reason.t * (Reason.t * Type.exporttypes * bool) -> Type.t -> Type.t
11391135

11401136
val return : Context.t -> Type.trace -> Type.t -> r
1141-
1142-
val mk_typeof_annotation : Context.t -> ?trace:Type.trace -> reason -> Type.t -> Type.t
11431137
end
11441138

11451139
(*********************************************************************)
@@ -1175,7 +1169,7 @@ end
11751169
Overall, we should be able to (at least conceptually) desugar `import
11761170
type` to `import` followed by `type`.
11771171
*)
1178-
module ImportTypeT_kit (F : Import_export_helper_sig) = struct
1172+
module ImportTypeTKit = struct
11791173
let canonicalize_imported_type cx reason t =
11801174
match t with
11811175
| DefT (_, trust, ClassT inst) -> Some (DefT (reason, trust, TypeT (ImportClassKind, inst)))
@@ -1209,18 +1203,15 @@ module ImportTypeT_kit (F : Import_export_helper_sig) = struct
12091203
| None ->
12101204
add_output cx ~trace (Error_message.EImportValueAsType (reason, export_name));
12111205
AnyT.error reason)
1212-
1213-
let on_type cx trace reason export_name l =
1214-
F.with_concretized_type cx reason (on_concrete_type cx trace reason export_name) l
12151206
end
12161207

12171208
(************************************************************************)
12181209
(* `import typeof` creates a properly-parameterized type alias for the *)
12191210
(* "typeof" the remote export. *)
12201211
(************************************************************************)
12211212

1222-
module ImportTypeofT_kit (F : Import_export_helper_sig) = struct
1223-
let on_concrete_type cx trace reason export_name l =
1213+
module ImportTypeofTKit = struct
1214+
let on_concrete_type cx trace ~mk_typeof_annotation reason export_name l =
12241215
match l with
12251216
| DefT
12261217
( _,
@@ -1233,7 +1224,7 @@ module ImportTypeofT_kit (F : Import_export_helper_sig) = struct
12331224
id;
12341225
}
12351226
) ->
1236-
let typeof_t = F.mk_typeof_annotation cx ~trace reason lower_t in
1227+
let typeof_t = mk_typeof_annotation cx ?trace:(Some trace) reason lower_t in
12371228

12381229
poly_type
12391230
id
@@ -1245,11 +1236,8 @@ module ImportTypeofT_kit (F : Import_export_helper_sig) = struct
12451236
add_output cx ~trace (Error_message.EImportTypeAsTypeof (reason, export_name));
12461237
AnyT.error reason
12471238
| _ ->
1248-
let typeof_t = F.mk_typeof_annotation cx ~trace reason l in
1239+
let typeof_t = mk_typeof_annotation cx ?trace:(Some trace) reason l in
12491240
DefT (reason, bogus_trust (), TypeT (ImportTypeofKind, typeof_t))
1250-
1251-
let on_type cx trace reason export_name l =
1252-
F.with_concretized_type cx reason (on_concrete_type cx trace reason export_name) l
12531241
end
12541242

12551243
module CJSRequireT_kit (F : Import_export_helper_sig) = struct
@@ -1325,14 +1313,14 @@ module ImportModuleNsTKit = struct
13251313
Obj_type.mk_with_proto cx reason ~obj_kind ~frozen:true ~props proto
13261314
end
13271315

1328-
module ImportDefaultT_kit (F : Import_export_helper_sig) = struct
1329-
module ImportTypeTKit = ImportTypeT_kit (F)
1330-
module ImportTypeofTKit = ImportTypeofT_kit (F)
1331-
1316+
module ImportDefaultTKit = struct
13321317
(* import [type] X from 'SomeModule'; *)
13331318
let on_ModuleT
13341319
cx
13351320
trace
1321+
~mk_typeof_annotation
1322+
~assert_import_is_value
1323+
~with_concretized_type
13361324
(reason, import_kind, (local_name, module_name), is_strict)
13371325
(module_reason, exports, imported_is_strict) =
13381326
check_nonstrict_import cx trace is_strict imported_is_strict reason;
@@ -1366,21 +1354,31 @@ module ImportDefaultT_kit (F : Import_export_helper_sig) = struct
13661354
AnyT.error module_reason)
13671355
in
13681356
match import_kind with
1369-
| ImportType -> ImportTypeTKit.on_type cx trace reason "default" export_t
1370-
| ImportTypeof -> ImportTypeofTKit.on_type cx trace reason "default" export_t
1357+
| ImportType ->
1358+
with_concretized_type
1359+
cx
1360+
reason
1361+
(ImportTypeTKit.on_concrete_type cx trace reason "default")
1362+
export_t
1363+
| ImportTypeof ->
1364+
with_concretized_type
1365+
cx
1366+
reason
1367+
(ImportTypeofTKit.on_concrete_type cx trace ~mk_typeof_annotation reason "default")
1368+
export_t
13711369
| ImportValue ->
1372-
F.assert_import_is_value cx trace reason "default" export_t;
1370+
assert_import_is_value cx trace reason "default" export_t;
13731371
export_t
13741372
end
13751373

1376-
module ImportNamedT_kit (F : Import_export_helper_sig) = struct
1377-
module ImportTypeTKit = ImportTypeT_kit (F)
1378-
module ImportTypeofTKit = ImportTypeofT_kit (F)
1379-
1374+
module ImportNamedTKit = struct
13801375
(* import {X} from 'SomeModule'; *)
13811376
let on_ModuleT
13821377
cx
13831378
trace
1379+
~mk_typeof_annotation
1380+
~assert_import_is_value
1381+
~with_concretized_type
13841382
(reason, import_kind, export_name, module_name, is_strict)
13851383
(_, exports, imported_is_strict) =
13861384
check_nonstrict_import cx trace is_strict imported_is_strict reason;
@@ -1399,18 +1397,36 @@ module ImportNamedT_kit (F : Import_export_helper_sig) = struct
13991397
in
14001398
let has_every_named_export = exports.has_every_named_export in
14011399
match (import_kind, NameUtils.Map.find_opt (OrdinaryName export_name) exports_tmap) with
1402-
| (ImportType, Some t) -> ImportTypeTKit.on_type cx trace reason export_name t
1400+
| (ImportType, Some t) ->
1401+
with_concretized_type
1402+
cx
1403+
reason
1404+
(ImportTypeTKit.on_concrete_type cx trace reason export_name)
1405+
t
14031406
| (ImportType, None) when has_every_named_export ->
1404-
ImportTypeTKit.on_type cx trace reason export_name (AnyT.untyped reason)
1405-
| (ImportTypeof, Some t) -> ImportTypeofTKit.on_type cx trace reason export_name t
1407+
with_concretized_type
1408+
cx
1409+
reason
1410+
(ImportTypeTKit.on_concrete_type cx trace reason export_name)
1411+
(AnyT.untyped reason)
1412+
| (ImportTypeof, Some t) ->
1413+
with_concretized_type
1414+
cx
1415+
reason
1416+
(ImportTypeofTKit.on_concrete_type cx trace ~mk_typeof_annotation reason export_name)
1417+
t
14061418
| (ImportTypeof, None) when has_every_named_export ->
1407-
ImportTypeofTKit.on_type cx trace reason export_name (AnyT.untyped reason)
1419+
with_concretized_type
1420+
cx
1421+
reason
1422+
(ImportTypeofTKit.on_concrete_type cx trace ~mk_typeof_annotation reason export_name)
1423+
(AnyT.untyped reason)
14081424
| (ImportValue, Some t) ->
1409-
F.assert_import_is_value cx trace reason export_name t;
1425+
assert_import_is_value cx trace reason export_name t;
14101426
t
14111427
| (ImportValue, None) when has_every_named_export ->
14121428
let t = AnyT.untyped reason in
1413-
F.assert_import_is_value cx trace reason export_name t;
1429+
assert_import_is_value cx trace reason export_name t;
14141430
t
14151431
| (_, None) ->
14161432
let num_exports = NameUtils.Map.cardinal exports_tmap in
@@ -1579,8 +1595,6 @@ end
15791595
* exports one type at a time and it takes the type to be exported as a
15801596
* lower (so that the type can be filtered post-resolution). *)
15811597
module ExportTypeT_kit (F : Import_export_helper_sig) = struct
1582-
module ImportTypeTKit = ImportTypeT_kit (F)
1583-
15841598
let on_concrete_type cx trace (reason, export_name, target_module_t) l =
15851599
let is_type_export =
15861600
match l with

0 commit comments

Comments
 (0)