From d0c5dc1fb357f25c696120e2ee44cec8e206bc7e Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Sat, 18 Jan 2025 11:54:12 -0800 Subject: [PATCH] [flow][refactor] Generalize ForcingState Summary: In order to kill `ModuleT` as a `Type.t`, we still need the capabilities that Type.t provides: notably, the ability to maintain lazy. Note that wrapping the computation in lazy is not enough, since we need defense again cyclic types. `ForcingState` provides this ability, but right now it's very coupled with Type.t. This diff generalizes it, which allows a new variant `type module_type = ((Reason.t * TypeTerm.moduletype, TypeTerm.t) result, Reason.t) state` to be added in the next diff. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D68312416 fbshipit-source-id: 991620c9a0a9da09dc4e77e585f50279250bae0c --- src/typing/type.ml | 47 +++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/typing/type.ml b/src/typing/type.ml index 44dfc55162d..723bc6f1892 100644 --- a/src/typing/type.ml +++ b/src/typing/type.ml @@ -3210,45 +3210,50 @@ module Constraint = struct module UseTypeMap = Flow_map.Make (UseTypeKey) module ForcingState : sig - type t + type ('a, 'b) state + + type t = (TypeTerm.t, Reason.t) state val of_lazy_t : error_reason:Reason.t -> TypeTerm.t Lazy.t -> t val of_non_lazy_t : TypeTerm.t -> t - val force : on_error:(Reason.t -> TypeTerm.t) -> t -> TypeTerm.t + val force : on_error:('b -> 'a) -> ('a, 'b) state -> 'a - val already_forced_with_cyclic_error : t -> bool + val already_forced_with_cyclic_error : ('a, 'b) state -> bool - val get_forced_for_debugging : t -> TypeTerm.t option + val get_forced_for_debugging : ('a, 'b) state -> 'a option - val copy : on_error:(Reason.t -> TypeTerm.t) -> visit_for_copier:(TypeTerm.t -> unit) -> t -> t + val copy : + on_error:('b -> 'a) -> visit_for_copier:('a -> unit) -> ('a, 'b) state -> ('a, 'b) state end = struct - type state = + type 'a status = | Unforced | Forcing | Forced - | ForcedWithCyclicError of TypeTerm.t + | ForcedWithCyclicError of 'a - type t = { - valid: TypeTerm.t Lazy.t; - error_reason: Reason.t option; - mutable state: state; + type ('a, 'b) state = { + valid: 'a Lazy.t; + error_reason: 'b option; + mutable status: 'a status; } + type t = (TypeTerm.t, Reason.t) state + let of_lazy_t ~error_reason valid = - { valid; error_reason = Some error_reason; state = Unforced } + { valid; error_reason = Some error_reason; status = Unforced } - let of_non_lazy_t t = { valid = lazy t; error_reason = None; state = Forced } + let of_non_lazy_t t = { valid = lazy t; error_reason = None; status = Forced } let force ~on_error s = - match s.state with + match s.status with | Unforced -> - s.state <- Forcing; + s.status <- Forcing; let t = Lazy.force_val s.valid in - (match s.state with + (match s.status with | Forcing -> - s.state <- Forced; + s.status <- Forced; t | ForcedWithCyclicError t -> t | Unforced -> failwith "Invalid state Unforced" @@ -3257,11 +3262,11 @@ module Constraint = struct | ForcedWithCyclicError t -> t | Forcing -> let t = on_error (Base.Option.value_exn s.error_reason) in - s.state <- ForcedWithCyclicError t; + s.status <- ForcedWithCyclicError t; t let already_forced_with_cyclic_error s = - match s.state with + match s.status with | Unforced | Forcing | Forced -> @@ -3269,7 +3274,7 @@ module Constraint = struct | ForcedWithCyclicError _ -> true let get_forced_for_debugging s = - match s.state with + match s.status with | Unforced | Forcing -> None @@ -3285,7 +3290,7 @@ module Constraint = struct t ); error_reason = s.error_reason; - state = Unforced; + status = Unforced; } end