Skip to content

Commit

Permalink
Add support for enum bounds
Browse files Browse the repository at this point in the history
Summary: What it says in the title

Reviewed By: mheiber

Differential Revision: D62713123

fbshipit-source-id: a2fe83d3a4f709f47f17daca556192727e7efe84
  • Loading branch information
Mistral Contrastin authored and facebook-github-bot committed Sep 16, 2024
1 parent 7651c01 commit c8b31f7
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions hphp/hack/src/milner/milner_generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module rec Definition : sig

val case_type : name:string -> Type.t list -> t

val enum : name:string -> Type.t -> value:string -> t
val enum : name:string -> bound:Type.t option -> Type.t -> value:string -> t
end = struct
type t = string

Expand Down Expand Up @@ -237,8 +237,16 @@ end = struct
let rhs = String.concat ~sep:" | " (List.map ~f:Type.show disjuncts) in
Format.sprintf "case type %s = %s;" name rhs

let enum ~name ty ~value =
Format.sprintf "enum %s: %s { A = %s; }" name (Type.show ty) value
let enum ~name ~bound ty ~value =
match bound with
| Some bound ->
Format.sprintf
"enum %s: %s as %s { A = %s; }"
name
(Type.show ty)
(Type.show bound)
value
| None -> Format.sprintf "enum %s: %s { A = %s; }" name (Type.show ty) value
end

and Type : sig
Expand Down Expand Up @@ -879,11 +887,17 @@ end = struct
(Case { name; disjuncts }, case_type_def :: defs)
| Kind.Enum ->
let name = fresh "E" in
let underlying_ty =
select Primitive.[Primitive Arraykey; Primitive String; Primitive Int]
let (bound, underlying_ty) =
if Random.bool () then
let bound = mk_arraykey () in
let underlying_ty = subtypes_of bound ~pick:true |> List.hd_exn in
(Some bound, underlying_ty)
else
let underlying_ty = mk_arraykey () in
(None, underlying_ty)
in
let value = inhabitant_of underlying_ty in
let enum_def = Definition.enum ~name underlying_ty ~value in
let enum_def = Definition.enum ~name ~bound underlying_ty ~value in
(Enum { name }, [enum_def])
| Kind.Container -> begin
match Container.pick () with
Expand Down

0 comments on commit c8b31f7

Please sign in to comment.