Skip to content

Commit 42d708b

Browse files
authored
flambda-backend: Fix a "bug" in Ctype.constrain_type_jkind (#2671)
1 parent a3d9cd6 commit 42d708b

File tree

7 files changed

+37
-24
lines changed

7 files changed

+37
-24
lines changed

typing/ctype.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,9 @@ let instance_prim_layout (desc : Primitive.description) ty =
16581658
else
16591659
let new_sort_and_jkind = ref None in
16601660
let get_jkind () =
1661+
(* CR layouts v2.8: This should replace only the layout component of the
1662+
jkind. It's possible that we might want a primitive that accepts a
1663+
mode-crossing, layout-polymorphic parameter. *)
16611664
match !new_sort_and_jkind with
16621665
| Some (_, jkind) ->
16631666
jkind
@@ -1675,10 +1678,10 @@ let instance_prim_layout (desc : Primitive.description) ty =
16751678
from an outer scope *)
16761679
if level = generic_level && try_mark_node ty then begin
16771680
begin match get_desc ty with
1678-
| Tvar ({ jkind; _ } as r) when Jkind.is_any jkind ->
1681+
| Tvar ({ jkind; _ } as r) when Jkind.has_layout_any jkind ->
16791682
For_copy.redirect_desc copy_scope ty
16801683
(Tvar {r with jkind = get_jkind ()})
1681-
| Tunivar ({ jkind; _ } as r) when Jkind.is_any jkind ->
1684+
| Tunivar ({ jkind; _ } as r) when Jkind.has_layout_any jkind ->
16821685
For_copy.redirect_desc copy_scope ty
16831686
(Tunivar {r with jkind = get_jkind ()})
16841687
| _ -> ()
@@ -2205,7 +2208,7 @@ let constrain_type_jkind ~fixed env ty jkind =
22052208
let constrain_type_jkind ~fixed env ty jkind =
22062209
(* An optimization to avoid doing any work if we're checking against
22072210
any. *)
2208-
if Jkind.is_any jkind then Ok ()
2211+
if Jkind.is_max jkind then Ok ()
22092212
else constrain_type_jkind ~fixed env ty jkind
22102213

22112214
let check_type_jkind env ty jkind =

typing/jkind.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,12 @@ let is_void_defaulting = function
12321232
| { jkind = { layout = Sort s; _ }; _ } -> Sort.is_void_defaulting s
12331233
| _ -> false
12341234

1235-
let is_any jkind = match jkind.jkind.layout with Any -> true | _ -> false
1235+
(* This doesn't do any mutation because mutating a sort variable can't make it
1236+
any, and modal upper bounds are constant. *)
1237+
let is_max jkind = sub any_dummy_jkind jkind
1238+
1239+
let has_layout_any jkind =
1240+
match jkind.jkind.layout with Any -> true | _ -> false
12361241

12371242
(*********************************)
12381243
(* debugging *)

typing/jkind.mli

+6-2
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,12 @@ val sub_or_error : t -> t -> (unit, Violation.t) result
346346
(** Like [sub], but returns the subjkind with an updated history. *)
347347
val sub_with_history : t -> t -> (t, Violation.t) result
348348

349-
(** Checks to see whether a jkind is any. Never does any mutation. *)
350-
val is_any : t -> bool
349+
(** Checks to see whether a jkind is the maximum jkind. Never does any
350+
mutation. *)
351+
val is_max : t -> bool
352+
353+
(** Checks to see whether a jkind is has layout. Never does any mutation. *)
354+
val has_layout_any : t -> bool
351355

352356
(*********************************)
353357
(* debugging *)

typing/primitive.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ let prim_has_valid_reprs ~loc prim =
700700
raise (Error (loc,
701701
Invalid_native_repr_for_primitive (prim.prim_name)))
702702

703-
let prim_can_contain_jkind_any prim =
703+
let prim_can_contain_layout_any prim =
704704
match prim.prim_name with
705705
| "%array_length"
706706
| "%array_safe_get"

typing/primitive.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ val native_name_is_external : description -> bool
119119
fails. *)
120120
val prim_has_valid_reprs : loc:Location.t -> description -> unit
121121

122-
(** Check if a primitive can have jkind [any] anywhere within its type
122+
(** Check if a primitive can have layout [any] anywhere within its type
123123
declaration. Returns [false] for built-in primitives that inspect
124124
the layout of type parameters ([%array_length] for example). *)
125-
val prim_can_contain_jkind_any : description -> bool
125+
val prim_can_contain_layout_any : description -> bool
126126

127127
type error =
128128
| Old_style_float_with_native_repr_attribute

typing/typedecl.ml

+15-14
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ type error =
131131
| Nonrec_gadt
132132
| Invalid_private_row_declaration of type_expr
133133
| Local_not_enabled
134-
| Unexpected_jkind_any_in_primitive of string
134+
| Unexpected_layout_any_in_primitive of string
135135
| Useless_layout_poly
136136
| Modalities_on_value_description
137137
| Zero_alloc_attr_unsupported of Builtin_attributes.zero_alloc_attribute
@@ -2653,7 +2653,7 @@ let make_native_repr env core_type ty ~global_repr ~is_layout_poly ~why =
26532653
error_if_has_deep_native_repr_attributes core_type;
26542654
let sort_or_poly =
26552655
match get_desc (Ctype.get_unboxed_type_approximation env ty) with
2656-
(* This only captures tvars with jkind [any] explicitly quantified within
2656+
(* This only captures tvars with layout [any] explicitly quantified within
26572657
the declaration.
26582658
26592659
This is sufficient since [transl_type_scheme] promises that:
@@ -2662,7 +2662,7 @@ let make_native_repr env core_type ty ~global_repr ~is_layout_poly ~why =
26622662
transl)
26632663
*)
26642664
| Tvar {jkind} when is_layout_poly
2665-
&& Jkind.is_any jkind
2665+
&& Jkind.has_layout_any jkind
26662666
&& get_level ty = Btype.generic_level -> Poly
26672667
| _ ->
26682668
let sort =
@@ -2793,18 +2793,18 @@ let check_unboxable env loc ty =
27932793
all_unboxable_types
27942794
()
27952795

2796-
let has_ty_var_with_jkind_any env ty =
2796+
let has_ty_var_with_layout_any env ty =
27972797
List.exists
2798-
(fun ty -> Jkind.is_any (Ctype.estimate_type_jkind env ty))
2798+
(fun ty -> Jkind.has_layout_any (Ctype.estimate_type_jkind env ty))
27992799
(Ctype.free_variables ty)
28002800

2801-
let unexpected_jkind_any_check prim env cty ty =
2802-
if Primitive.prim_can_contain_jkind_any prim ||
2801+
let unexpected_layout_any_check prim env cty ty =
2802+
if Primitive.prim_can_contain_layout_any prim ||
28032803
prim.prim_is_layout_poly then ()
28042804
else
2805-
if has_ty_var_with_jkind_any env ty then
2805+
if has_ty_var_with_layout_any env ty then
28062806
raise(Error (cty.ctyp_loc,
2807-
Unexpected_jkind_any_in_primitive(prim.prim_name)))
2807+
Unexpected_layout_any_in_primitive(prim.prim_name)))
28082808

28092809
(* Note regarding jkind checks on external declarations
28102810
@@ -2852,13 +2852,14 @@ let unexpected_jkind_any_check prim env cty ty =
28522852
point to the source of the mistake which is, in fact, the external
28532853
declaration.
28542854
2855-
For this reason, we have [unexpected_jkind_any_check]. It's here to point
2856-
out this type of mistake early and suggest the use of [@layout_poly].
2855+
For this reason, we have [unexpected_layout_any_check]. It's here to
2856+
point out this type of mistake early and suggest the use of
2857+
[@layout_poly].
28572858
28582859
An exception is raised if any of these checks fails. *)
28592860
let error_if_containing_unexpected_jkind prim env cty ty =
28602861
Primitive.prim_has_valid_reprs ~loc:cty.ctyp_loc prim;
2861-
unexpected_jkind_any_check prim env cty ty
2862+
unexpected_layout_any_check prim env cty ty
28622863

28632864
(* Translate a value declaration *)
28642865
let transl_value_decl env loc valdecl =
@@ -2918,7 +2919,7 @@ let transl_value_decl env loc valdecl =
29182919
Builtin_attributes.has_layout_poly valdecl.pval_attributes
29192920
in
29202921
if is_layout_poly &&
2921-
not (has_ty_var_with_jkind_any env ty) then
2922+
not (has_ty_var_with_layout_any env ty) then
29222923
raise(Error(valdecl.pval_type.ptyp_loc, Useless_layout_poly));
29232924
let native_repr_args, native_repr_res =
29242925
parse_native_repr_attributes
@@ -3688,7 +3689,7 @@ let report_error ppf = function
36883689
| Local_not_enabled ->
36893690
fprintf ppf "@[The local extension is disabled@ \
36903691
To enable it, pass the '-extension local' flag@]"
3691-
| Unexpected_jkind_any_in_primitive name ->
3692+
| Unexpected_layout_any_in_primitive name ->
36923693
fprintf ppf
36933694
"@[The primitive [%s] doesn't work well with type variables of@ \
36943695
layout any. Consider using [@@layout_poly].@]" name

typing/typedecl.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ type error =
171171
| Nonrec_gadt
172172
| Invalid_private_row_declaration of type_expr
173173
| Local_not_enabled
174-
| Unexpected_jkind_any_in_primitive of string
174+
| Unexpected_layout_any_in_primitive of string
175175
| Useless_layout_poly
176176
| Modalities_on_value_description
177177
| Zero_alloc_attr_unsupported of Builtin_attributes.zero_alloc_attribute

0 commit comments

Comments
 (0)