Skip to content

Commit

Permalink
Avoid propagating [all_void]
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfirere committed Dec 11, 2024
1 parent f2c53d3 commit ad170e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
22 changes: 15 additions & 7 deletions typing/jkind.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1459,17 +1459,19 @@ let has_mutable_label lbls =
match lbl.ld_mutable with Immutable -> false | Mutable _ -> true)
lbls

let all_void_labels lbls =
List.for_all
(fun (lbl : Types.label_declaration) -> Sort.Const.(equal void lbl.ld_sort))
lbls

let add_labels_as_baggage lbls jkind =
List.fold_right
(fun (lbl : Types.label_declaration) -> add_baggage ~baggage:lbl.ld_type)
lbls jkind

(* CR layouts v2.8: This should take modalities into account. *)
let for_boxed_record lbls =
if List.for_all
(fun (lbl : Types.label_declaration) ->
Sort.Const.(equal void lbl.ld_sort))
lbls
if all_void_labels lbls
then Builtin.immediate ~why:Empty_record
else
let is_mutable = has_mutable_label lbls in
Expand All @@ -1480,11 +1482,17 @@ let for_boxed_record lbls =
add_labels_as_baggage lbls base

(* CR layouts v2.8: This should take modalities into account. *)
let for_boxed_variant ~all_voids cstrs =
if all_voids
let for_boxed_variant cstrs =
let open Types in
if List.for_all
(fun cstr ->
match cstr.cd_args with
| Cstr_tuple args ->
List.for_all (fun arg -> Sort.Const.(equal void arg.ca_sort)) args
| Cstr_record lbls -> all_void_labels lbls)
cstrs
then Builtin.immediate ~why:Enumeration
else
let open Types in
let is_mutable =
List.exists
(fun cstr ->
Expand Down
6 changes: 2 additions & 4 deletions typing/jkind.mli
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,8 @@ val of_type_decl_default :
(** Choose an appropriate jkind for a boxed record type *)
val for_boxed_record : Types.label_declaration list -> jkind_l

(** Choose an appropriate jkind for a boxed variant type, given whether
all of the fields of all of its constructors are [void]. *)
val for_boxed_variant :
all_voids:bool -> Types.constructor_declaration list -> jkind_l
(** Choose an appropriate jkind for a boxed variant type. *)
val for_boxed_variant : Types.constructor_declaration list -> jkind_l

(** The jkind of an arrow type. *)
val for_arrow : jkind_l
Expand Down
12 changes: 6 additions & 6 deletions typing/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1691,8 +1691,8 @@ let update_decl_jkind env dpath decl =
assert false
end
| cstrs, Variant_boxed cstr_shapes ->
let (_,cstrs,all_voids) =
List.fold_left (fun (idx,cstrs,all_voids) cstr ->
let (_,cstrs) =
List.fold_left (fun (idx,cstrs) cstr ->
let arg_sorts =
match cstr_shapes.(idx) with
| Constructor_uniform_value, arg_sorts -> arg_sorts
Expand All @@ -1701,7 +1701,7 @@ let update_decl_jkind env dpath decl =
"Typedecl.update_variant_kind doesn't expect mixed \
constructor as input"
in
let cd_args, all_void, jkinds =
let cd_args, _all_void, jkinds =
update_constructor_arguments_sorts env cstr.Types.cd_loc
cstr.Types.cd_args (Some arg_sorts)
in
Expand All @@ -1716,10 +1716,10 @@ let update_decl_jkind env dpath decl =
| Constructor_mixed _ -> cstr_shapes.(idx) <- cstr_repr, arg_sorts
in
let cstr = { cstr with Types.cd_args } in
(idx+1,cstr::cstrs,all_voids && all_void)
) (0,[],true) cstrs
(idx+1,cstr::cstrs)
) (0,[]) cstrs
in
let jkind = Jkind.for_boxed_variant ~all_voids cstrs in
let jkind = Jkind.for_boxed_variant cstrs in
List.rev cstrs, rep, jkind
| (([] | (_ :: _)), Variant_unboxed | _, Variant_extensible) ->
assert false
Expand Down

0 comments on commit ad170e9

Please sign in to comment.