Skip to content

Commit

Permalink
[flow][cleanup] Kill ModuleT
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 22, 2025
1 parent 7acc095 commit 334e26c
Show file tree
Hide file tree
Showing 16 changed files with 11 additions and 135 deletions.
1 change: 0 additions & 1 deletion src/services/coverage/coverage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ let visitor =
| KeysT _
| StrUtilT _
| MaybeT _
| ModuleT _
| NamespaceT _
| NullProtoT _
| OpaqueT _
Expand Down
2 changes: 1 addition & 1 deletion src/services/get_def/getDefUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/typing/check_polarity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
24 changes: 0 additions & 24 deletions src/typing/debug_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions src/typing/errors/error_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
6 changes: 1 addition & 5 deletions src/typing/flow_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down Expand Up @@ -6016,9 +6014,7 @@ struct
| ThisTypeAppT _ ->
false
(* Should never occur as the lower bound of any *)
| ModuleT _
| NamespaceT _ ->
false
| NamespaceT _ -> false
| StrUtilT _
| DefT _
| AnyT _ ->
Expand Down
1 change: 0 additions & 1 deletion src/typing/flow_js_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ end = struct
| DefT (_, SingletonBoolT _)
| DefT (_, SingletonBigIntT _)
| GenericT _
| ModuleT _
| AnyT _
| FunProtoT _
| ObjProtoT _
Expand Down
45 changes: 2 additions & 43 deletions src/typing/members.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down Expand Up @@ -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"
Expand All @@ -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 ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 4 additions & 8 deletions src/typing/members.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
15 changes: 2 additions & 13 deletions src/typing/ty_normalizer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
3 changes: 0 additions & 3 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -4018,7 +4016,6 @@ let string_of_ctor = function
| GenericT _ -> "GenericT"
| KeysT _ -> "KeysT"
| StrUtilT _ -> "StrUtilT"
| ModuleT _ -> "ModuleT"
| NamespaceT _ -> "NamespaceT"
| NullProtoT _ -> "NullProtoT"
| ObjProtoT _ -> "ObjProtoT"
Expand Down
9 changes: 0 additions & 9 deletions src/typing/typeUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/typing/type_filter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,6 @@ and tag_of_t cx t =
| OptionalT _
| KeysT (_, _)
| OpaqueT (_, _)
| ModuleT _
| AnyT (_, _) ->
None

Expand Down
13 changes: 0 additions & 13 deletions src/typing/type_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/typing/type_subst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/typing/type_visitor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down

0 comments on commit 334e26c

Please sign in to comment.