Skip to content

Commit

Permalink
[flow][refactor] Generalize ForcingState
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 18, 2025
1 parent 9eccbcd commit d0c5dc1
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/typing/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -3257,19 +3262,19 @@ 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 ->
false
| ForcedWithCyclicError _ -> true

let get_forced_for_debugging s =
match s.state with
match s.status with
| Unforced
| Forcing ->
None
Expand All @@ -3285,7 +3290,7 @@ module Constraint = struct
t
);
error_reason = s.error_reason;
state = Unforced;
status = Unforced;
}
end

Expand Down

0 comments on commit d0c5dc1

Please sign in to comment.