From 334e26c938cd643ca3f78c43076dd7422b172304 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Tue, 21 Jan 2025 16:52:59 -0800 Subject: [PATCH] [flow][cleanup] Kill ModuleT Summary: With all the previous diff that removes it's usage, the misbehaving ModuleT can finally be killed. It never belongs to the set with other types, as it's only meaningful to describe the import source and never propagate to anything beyond that. It causes nasty case analysis everywhere, including import/export checking, type normalizer, etc. Now it's finally gone! Changelog: [internal] Reviewed By: gkz Differential Revision: D68382129 fbshipit-source-id: 030dccca7bee2b01b7d65c68e349984c83ecc36e --- src/services/coverage/coverage.ml | 1 - src/services/get_def/getDefUtils.ml | 2 +- src/typing/check_polarity.ml | 2 +- src/typing/debug_js.ml | 24 --------------- src/typing/errors/error_message.ml | 2 -- src/typing/flow_js.ml | 6 +--- src/typing/flow_js_utils.ml | 1 - src/typing/members.ml | 45 ++--------------------------- src/typing/members.mli | 12 +++----- src/typing/ty_normalizer.ml | 15 ++-------- src/typing/type.ml | 3 -- src/typing/typeUtil.ml | 9 ------ src/typing/type_filter.ml | 1 - src/typing/type_mapper.ml | 13 --------- src/typing/type_subst.ml | 2 -- src/typing/type_visitor.ml | 8 ----- 16 files changed, 11 insertions(+), 135 deletions(-) diff --git a/src/services/coverage/coverage.ml b/src/services/coverage/coverage.ml index ac89d75e0d9..31dd4e1ba66 100644 --- a/src/services/coverage/coverage.ml +++ b/src/services/coverage/coverage.ml @@ -153,7 +153,6 @@ let visitor = | KeysT _ | StrUtilT _ | MaybeT _ - | ModuleT _ | NamespaceT _ | NullProtoT _ | OpaqueT _ diff --git a/src/services/get_def/getDefUtils.ml b/src/services/get_def/getDefUtils.ml index 8e7d486817f..24449a9508a 100644 --- a/src/services/get_def/getDefUtils.ml +++ b/src/services/get_def/getDefUtils.ml @@ -436,7 +436,7 @@ and extract_def_loc_resolved ~loc_of_aloc cx ty name : (def_loc, string) result | Success (DefT (_, InstanceT { super; _ }) | ThisInstanceT (_, { super; _ }, _, _)) as extracted_type -> extract_def_locs_from_instancet ~loc_of_aloc cx extracted_type super name - | (Success (DefT (_, ObjT _)) | SuccessModule _ | SuccessNamespace _) as extracted_type -> + | (Success (DefT (_, ObjT _)) | SuccessNamespace _) as extracted_type -> get_def_locs_from_extracted_type cx extracted_type name >>| ( function | None -> NoDefFound | Some (loc, []) -> FoundObject (loc_of_aloc loc) diff --git a/src/typing/check_polarity.ml b/src/typing/check_polarity.ml index 2dccbe2e068..20c77feb3d1 100644 --- a/src/typing/check_polarity.ml +++ b/src/typing/check_polarity.ml @@ -235,7 +235,7 @@ end = struct (* TODO *) | EvalT _ -> () (* We only expect types which can appear in annotations. *) - | (DefT (_, TypeT _) | OpaqueT _ | ThisInstanceT _ | ModuleT _) as t -> + | (DefT (_, TypeT _) | OpaqueT _ | ThisInstanceT _) as t -> raise (UnexpectedType (Debug_js.dump_t cx t)) and check_polarity_propmap cx ?trace ?(skip_ctor = false) seen tparams polarity id = diff --git a/src/typing/debug_js.ml b/src/typing/debug_js.ml index a9c408f150e..ddeaa528cf1 100644 --- a/src/typing/debug_js.ml +++ b/src/typing/debug_js.ml @@ -346,29 +346,6 @@ let rec dump_t_ (depth, tvars) cx t = | DefT (_, SingletonNumT (_, s)) -> p ~extra:s t | DefT (_, SingletonBoolT b) -> p ~extra:(spf "%B" b) t | DefT (_, SingletonBigIntT (_, s)) -> p ~extra:s t - | ModuleT - { - module_reason = _; - module_export_types = { value_exports_tmap; type_exports_tmap; _ }; - module_is_strict = _; - module_available_platforms = _; - } -> - let exports_tmap_to_string exports_tmap = - Context.find_exports cx exports_tmap - |> NameUtils.Map.bindings - |> Base.List.map ~f:(fun (name, { preferred_def_locs = _; name_loc = _; type_ }) -> - kid type_ |> spf "%s: %s" (display_string_of_name name) - ) - |> String.concat ", " - in - p - t - ~extra: - (spf - "[%s] [%s]" - (exports_tmap_to_string value_exports_tmap) - (exports_tmap_to_string type_exports_tmap) - ) | NamespaceT { namespace_symbol; values_type; types_tmap } -> p t @@ -1074,7 +1051,6 @@ let dump_error_message = let open Error_message in let string_of_use_op = string_of_use_op_rec in let dump_internal_error = function - | UnexpectedModuleT _ -> "UnexpectedModuleT" | ReadOfUnreachedTvar _ -> "ReadOfUnreachedTvar" | ReadOfUnresolvedTvar _ -> "ReadOfUnresolvedTvar" | ForcedReadOfUnderResolutionTvar _ -> "ForcedReadOfUnderResolutionTvar" diff --git a/src/typing/errors/error_message.ml b/src/typing/errors/error_message.ml index f3aea1ac112..046e0de8089 100644 --- a/src/typing/errors/error_message.ml +++ b/src/typing/errors/error_message.ml @@ -694,7 +694,6 @@ and internal_error = | MissingEnvRead of ALoc.t | MissingEnvWrite of ALoc.t | MissingPredicateParam of int - | UnexpectedModuleT of string | ReadOfUnreachedTvar of Env_api.def_loc_type | ReadOfUnresolvedTvar of Env_api.def_loc_type | ForcedReadOfUnderResolutionTvar of Env_api.def_loc_type @@ -2094,7 +2093,6 @@ let enum_name_of_reason reason = | _ -> None let string_of_internal_error = function - | UnexpectedModuleT s -> spf "unexpected module type: %s" s | ReadOfUnreachedTvar k -> spf "read of %s entry which has not been prepared for typechecking" (Env_api.show_def_loc_type k) | ReadOfUnresolvedTvar k -> diff --git a/src/typing/flow_js.ml b/src/typing/flow_js.ml index 7cb75b8aa10..28228cfcfe8 100644 --- a/src/typing/flow_js.ml +++ b/src/typing/flow_js.ml @@ -5897,8 +5897,6 @@ struct | UseT (_, TypeAppT _) | UseT (_, DefT (_, TypeT _)) | ValueToTypeReferenceT _ - (* Should never occur, so we just defer to __flow to handle errors *) - | UseT (_, ModuleT _) (* Ideally, any would pollute every member of the union. However, it should be safe to only taint the type in the branch that flow picks when generating constraints for this, so this can be handled by the pre-existing rules *) @@ -6016,9 +6014,7 @@ struct | ThisTypeAppT _ -> false (* Should never occur as the lower bound of any *) - | ModuleT _ - | NamespaceT _ -> - false + | NamespaceT _ -> false | StrUtilT _ | DefT _ | AnyT _ -> diff --git a/src/typing/flow_js_utils.ml b/src/typing/flow_js_utils.ml index 3be59a6e804..ac50203aaa0 100644 --- a/src/typing/flow_js_utils.ml +++ b/src/typing/flow_js_utils.ml @@ -135,7 +135,6 @@ end = struct | DefT (_, SingletonBoolT _) | DefT (_, SingletonBigIntT _) | GenericT _ - | ModuleT _ | AnyT _ | FunProtoT _ | ObjProtoT _ diff --git a/src/typing/members.ml b/src/typing/members.ml index ad7cfb72731..5812a930258 100644 --- a/src/typing/members.ml +++ b/src/typing/members.ml @@ -11,9 +11,8 @@ open TypeUtil open Reason open Flow_js -type ('success, 'success_module, 'success_namespace) generic_t = +type ('success, 'success_namespace) generic_t = | Success of 'success - | SuccessModule of 'success_module | SuccessNamespace of 'success_namespace | FailureNullishType | FailureAnyType @@ -23,8 +22,6 @@ type ('success, 'success_module, 'success_namespace) generic_t = type t = ( (* Success *) (ALoc.t Nel.t option * Type.t) SMap.t, - (* SuccessModule *) - (ALoc.t Nel.t option * Type.t) SMap.t * Type.t option, (* SuccessNamespace *) (ALoc.t Nel.t option * Type.t) SMap.t ) @@ -474,7 +471,6 @@ and instantiate_type = function let string_of_extracted_type = function | Success t -> Printf.sprintf "Success (%s)" (Type.string_of_ctor t) - | SuccessModule t -> Printf.sprintf "SuccessModule (%s)" (Type.string_of_ctor t) | SuccessNamespace t -> Printf.sprintf "SuccessNamespace (%s)" (Type.string_of_ctor t) | FailureNullishType -> "FailureNullishType" | FailureAnyType -> "FailureAnyType" @@ -484,11 +480,8 @@ let string_of_extracted_type = function let to_command_result = function | Success map - | SuccessNamespace map - | SuccessModule (map, None) -> + | SuccessNamespace map -> Ok map - | SuccessModule (named_exports, Some cjs_export) -> - Ok (SMap.add "default" (None, cjs_export) named_exports) | FailureNullishType -> Error "autocomplete on possibly null or undefined value" | FailureAnyType -> Error "not enough type information to autocomplete" | FailureUnhandledType t -> @@ -550,7 +543,6 @@ let rec extract_type cx this_t = | DefT (_, ObjT _) as t -> Success t | DefT (_, EnumObjectT _) as t -> Success t | GenericT { bound; _ } -> extract_type cx bound - | ModuleT _ as t -> SuccessModule t | NamespaceT _ as t -> SuccessNamespace t | ThisTypeAppT (_, c, _, ts_opt) -> let c = resolve_type cx c in @@ -690,38 +682,6 @@ let rec extract_members ?(exclude_proto_members = false) cx = function SMap.empty in Success (AugmentableSMap.augment prot_members ~with_bindings:members) - | SuccessModule - (ModuleT - { - module_reason = _; - module_export_types = - { value_exports_tmap; type_exports_tmap; cjs_export; has_every_named_export = _ }; - module_is_strict = _; - module_available_platforms = _; - } - ) -> - let named_exports = - NameUtils.Map.union - (Context.find_exports cx value_exports_tmap) - (Context.find_exports cx type_exports_tmap) - in - let cjs_export = - match cjs_export with - | Some t -> Some (resolve_type cx t) - | None -> None - in - let named_exports = - NameUtils.display_smap_of_namemap named_exports - |> SMap.map (fun { name_loc; preferred_def_locs; type_ } -> - let def_locs = - match preferred_def_locs with - | Some _ -> preferred_def_locs - | None -> Base.Option.map ~f:Nel.one name_loc - in - (def_locs, type_) - ) - in - SuccessModule (named_exports, cjs_export) | SuccessNamespace (NamespaceT { namespace_symbol = _; values_type; types_tmap }) -> let members = SMap.fold @@ -781,7 +741,6 @@ let rec extract_members ?(exclude_proto_members = false) cx = function in Success members | Success t - | SuccessModule t | SuccessNamespace t -> FailureUnhandledMembers t diff --git a/src/typing/members.mli b/src/typing/members.mli index 4f0b45ffabd..06325328387 100644 --- a/src/typing/members.mli +++ b/src/typing/members.mli @@ -5,9 +5,8 @@ * LICENSE file in the root directory of this source tree. *) -type ('success, 'success_module, 'success_namespace) generic_t = +type ('success, 'success_namespace) generic_t = | Success of 'success - | SuccessModule of 'success_module | SuccessNamespace of 'success_namespace | FailureNullishType | FailureAnyType @@ -17,23 +16,20 @@ type ('success, 'success_module, 'success_namespace) generic_t = type t = ( (* Success *) (ALoc.t Nel.t option * Type.t) SMap.t, - (* SuccessModule *) - (ALoc.t Nel.t option * Type.t) SMap.t * Type.t option, (* SuccessNamespace *) (ALoc.t Nel.t option * Type.t) SMap.t ) generic_t (* For debugging purposes *) -val string_of_extracted_type : (Type.t, Type.t, Type.t) generic_t -> string +val string_of_extracted_type : (Type.t, Type.t) generic_t -> string val to_command_result : t -> ((ALoc.t Nel.t option * Type.t) SMap.t, string) result val extract : ?exclude_proto_members:bool -> Context.t -> Type.t -> t -val extract_type : Context.t -> Type.t -> (Type.t, Type.t, Type.t) generic_t +val extract_type : Context.t -> Type.t -> (Type.t, Type.t) generic_t -val extract_members : - ?exclude_proto_members:bool -> Context.t -> (Type.t, Type.t, Type.t) generic_t -> t +val extract_members : ?exclude_proto_members:bool -> Context.t -> (Type.t, Type.t) generic_t -> t val resolve_type : Context.t -> Type.t -> Type.t diff --git a/src/typing/ty_normalizer.ml b/src/typing/ty_normalizer.ml index 24f1a910f46..dad9cbb1fc3 100644 --- a/src/typing/ty_normalizer.ml +++ b/src/typing/ty_normalizer.ml @@ -936,9 +936,7 @@ module Make (I : INPUT) : S = struct let%map symbol = Reason_utils.local_type_alias_symbol env reason in Ty.Generic (symbol, Ty.EnumKind, None) (* Top-level only *) - | DefT (_, TypeT _) - | ModuleT _ -> - terr ~kind:(UnexpectedTypeCtor (string_of_ctor t)) (Some t) + | DefT (_, TypeT _) -> terr ~kind:(UnexpectedTypeCtor (string_of_ctor t)) (Some t) and any_t reason kind = match kind with @@ -1982,16 +1980,7 @@ module Make (I : INPUT) : S = struct match t with (* Polymorphic variants - see singleton_poly *) | DefT (_, PolyT { tparams; t_out; _ }) -> singleton_poly ~env ~orig_t tparams t_out - (* Modules *) - | ModuleT - { - module_reason = reason; - module_export_types = exports; - module_is_strict = _; - module_available_platforms = _; - } -> - let%map m = module_t ~env reason exports in - Ty.Decl m + (* Namespaces *) | NamespaceT { namespace_symbol; values_type = DefT (_, ObjT o); types_tmap } -> (match FlowSymbol.kind_of_symbol namespace_symbol with | FlowSymbol.SymbolModule when not env.Env.keep_only_namespace_name -> diff --git a/src/typing/type.ml b/src/typing/type.ml index d2a4d8806b6..67404dfda11 100644 --- a/src/typing/type.ml +++ b/src/typing/type.ml @@ -173,8 +173,6 @@ module rec TypeTerm : sig * type is defined in a libdef. We also keep track of the name of the opaque type in * opaquetype.name for pretty printing. *) | OpaqueT of reason * opaquetype - (* Stores exports (and potentially other metadata) for a module *) - | ModuleT of moduletype (* Stores both values and types in the same namespace*) | NamespaceT of namespace_type (* Here's to the crazy ones. The misfits. The rebels. The troublemakers. @@ -4018,7 +4016,6 @@ let string_of_ctor = function | GenericT _ -> "GenericT" | KeysT _ -> "KeysT" | StrUtilT _ -> "StrUtilT" - | ModuleT _ -> "ModuleT" | NamespaceT _ -> "NamespaceT" | NullProtoT _ -> "NullProtoT" | ObjProtoT _ -> "ObjProtoT" diff --git a/src/typing/typeUtil.ml b/src/typing/typeUtil.ml index 9217e06f247..1ed593247cb 100644 --- a/src/typing/typeUtil.ml +++ b/src/typing/typeUtil.ml @@ -24,7 +24,6 @@ let rec reason_of_t = function | FunProtoBindT reason -> reason | KeysT (reason, _) -> reason | StrUtilT { reason; _ } -> reason - | ModuleT { module_reason = reason; _ } -> reason | NamespaceT { values_type; _ } -> reason_of_t values_type | NullProtoT reason -> reason | ObjProtoT reason -> reason @@ -133,14 +132,6 @@ let rec mod_reason_of_t f = function | FunProtoBindT reason -> FunProtoBindT (f reason) | KeysT (reason, t) -> KeysT (f reason, t) | StrUtilT { reason; op; remainder } -> StrUtilT { reason = f reason; op; remainder } - | ModuleT { module_reason; module_export_types; module_is_strict; module_available_platforms } -> - ModuleT - { - module_reason = f module_reason; - module_export_types; - module_is_strict; - module_available_platforms; - } | NamespaceT { namespace_symbol; values_type; types_tmap } -> NamespaceT { namespace_symbol; values_type = mod_reason_of_t f values_type; types_tmap } | NullProtoT reason -> NullProtoT (f reason) diff --git a/src/typing/type_filter.ml b/src/typing/type_filter.ml index 3c2f3c17d45..4dce57c6906 100644 --- a/src/typing/type_filter.ml +++ b/src/typing/type_filter.ml @@ -1021,7 +1021,6 @@ and tag_of_t cx t = | OptionalT _ | KeysT (_, _) | OpaqueT (_, _) - | ModuleT _ | AnyT (_, _) -> None diff --git a/src/typing/type_mapper.ml b/src/typing/type_mapper.ml index c27fd1737b0..0d138d58264 100644 --- a/src/typing/type_mapper.ml +++ b/src/typing/type_mapper.ml @@ -143,19 +143,6 @@ class virtual ['a] t = t else OpaqueT (r, { opaquetype with underlying_t; super_t; opaque_type_args }) - | ModuleT { module_reason; module_export_types; module_is_strict; module_available_platforms } - -> - let module_export_types' = self#export_types cx map_cx module_export_types in - if module_export_types == module_export_types' then - t - else - ModuleT - { - module_reason; - module_export_types = module_export_types'; - module_is_strict; - module_available_platforms; - } | NamespaceT namespace_t -> let namespace_t' = self#namespace_type cx map_cx namespace_t in if namespace_t' == namespace_t then diff --git a/src/typing/type_subst.ml b/src/typing/type_subst.ml index 6ae7fc98f33..446fa488ef1 100644 --- a/src/typing/type_subst.ml +++ b/src/typing/type_subst.ml @@ -334,8 +334,6 @@ let substituter = * were substituted. *) let use_op = Base.Option.value use_op ~default:op in Flow_cache.Eval.id cx x' (TypeDestructorT (use_op, r, d')) - | ModuleT _ -> - failwith (Utils_js.spf "Unhandled type ctor: %s" (string_of_ctor t)) (* TODO *) | UnionT (r, urep) -> union_ident_map_and_dedup cx (self#type_ cx map_cx) t r urep | t -> super#type_ cx map_cx t in diff --git a/src/typing/type_visitor.ml b/src/typing/type_visitor.ml index 525d41f84ab..1d3b6ab0be7 100644 --- a/src/typing/type_visitor.ml +++ b/src/typing/type_visitor.ml @@ -56,14 +56,6 @@ class ['a] t = let acc = self#opt (self#type_ cx pole) acc underlying_t in let acc = self#opt (self#type_ cx pole) acc super_t in acc - | ModuleT - { - module_reason = _; - module_export_types = exporttypes; - module_is_strict = _; - module_available_platforms = _; - } -> - self#export_types cx pole acc exporttypes | NamespaceT namespace_t -> self#namespace_type cx pole acc namespace_t | ThisInstanceT (_, t, _, _) -> self#instance_type cx pole acc t | ThisTypeAppT (_, t, this, ts_opt) ->