Skip to content

Commit 7d9ef46

Browse files
authored
flambda-backend: Remove Jkind.of_sort (#1890)
1 parent 5ad9591 commit 7d9ef46

File tree

9 files changed

+58
-72
lines changed

9 files changed

+58
-72
lines changed

lambda/translmod.ml

+7-13
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type unsafe_info =
3838
type error =
3939
Circular_dependency of (Ident.t * unsafe_info) list
4040
| Conflicting_inline_attributes
41-
| Non_value_jkind of type_expr * Jkind.Violation.t
41+
| Non_value_jkind of type_expr * Jkind.sort
4242

4343
exception Error of Location.t * error
4444

@@ -55,13 +55,7 @@ exception Error of Location.t * error
5555
some defaulting. *)
5656
let sort_must_not_be_void loc ty sort =
5757
if Jkind.Sort.is_void_defaulting sort then
58-
let violation =
59-
Jkind.(Violation.of_
60-
(Not_a_subjkind
61-
(Jkind.of_sort ~why:V1_safety_check sort,
62-
value ~why:V1_safety_check)))
63-
in
64-
raise (Error (loc, Non_value_jkind (ty, violation)))
58+
raise (Error (loc, Non_value_jkind (ty, sort)))
6559

6660
let cons_opt x_opt xs =
6761
match x_opt with
@@ -1893,12 +1887,12 @@ let report_error loc = function
18931887
print_cycle cycle chapter section
18941888
| Conflicting_inline_attributes ->
18951889
Location.errorf "@[Conflicting 'inline' attributes@]"
1896-
| Non_value_jkind (ty, err) ->
1890+
| Non_value_jkind (ty, sort) ->
18971891
Location.errorf
1898-
"Non-value detected in [translmod]:@ Please report this error to \
1899-
the Jane Street compilers team.@ %a"
1900-
(Jkind.Violation.report_with_offender
1901-
~offender:(fun ppf -> Printtyp.type_expr ppf ty)) err
1892+
"Non-value sort %a detected in [translmod] in type %a:@ \
1893+
Please report this error to the Jane Street compilers team."
1894+
Jkind.Sort.format sort
1895+
Printtyp.type_expr ty
19021896

19031897
let () =
19041898
Location.register_error_of_exn

lambda/translmod.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type unsafe_info =
5353
type error =
5454
Circular_dependency of (Ident.t * unsafe_info) list
5555
| Conflicting_inline_attributes
56-
| Non_value_jkind of Types.type_expr * Jkind.Violation.t
56+
| Non_value_jkind of Types.type_expr * Jkind.sort
5757

5858
exception Error of Location.t * error
5959

typing/ctype.ml

+10-11
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ let new_scoped_ty scope desc = newty3 ~level:!current_level ~scope desc
238238

239239
let newvar ?name jkind =
240240
newty2 ~level:!current_level (Tvar { name; jkind })
241+
let new_rep_var ?name ~why () =
242+
let jkind, sort = Jkind.of_new_sort_var ~why in
243+
newvar ?name jkind, sort
241244
let newvar2 ?name level jkind = newty2 ~level (Tvar { name; jkind })
242245
let new_global_var ?name jkind =
243246
newty2 ~level:!global_level (Tvar { name; jkind })
@@ -2080,10 +2083,8 @@ let type_jkind env ty =
20802083
estimate_type_jkind env (get_unboxed_type_approximation env ty)
20812084

20822085
let type_sort ~why env ty =
2083-
let sort = Jkind.Sort.new_var () in
2084-
match
2085-
constrain_type_jkind env ty (Jkind.of_sort sort ~why)
2086-
with
2086+
let jkind, sort = Jkind.of_new_sort_var ~why in
2087+
match constrain_type_jkind env ty jkind with
20872088
| Ok _ -> Ok sort
20882089
| Error _ as e -> e
20892090

@@ -3817,14 +3818,12 @@ let filter_arrow env t l ~force_tpoly =
38173818
allow both to be any. Separately, the relevant checks on function
38183819
arguments should happen when functions are constructed, not their
38193820
types. *)
3820-
let arg_sort = Jkind.Sort.new_var () in
3821-
let l_arg = Jkind.of_sort ~why:Function_argument arg_sort in
3822-
let ret_sort = Jkind.Sort.new_var () in
3823-
let l_res = Jkind.of_sort ~why:Function_result ret_sort in
3821+
let k_arg, arg_sort = Jkind.of_new_sort_var ~why:Function_argument in
3822+
let k_res, ret_sort = Jkind.of_new_sort_var ~why:Function_result in
38243823
let ty_arg =
38253824
if not force_tpoly then begin
38263825
assert (not (is_optional l));
3827-
newvar2 level l_arg
3826+
newvar2 level k_arg
38283827
end else begin
38293828
let t1 =
38303829
if is_optional l then
@@ -3835,12 +3834,12 @@ let filter_arrow env t l ~force_tpoly =
38353834
[newvar2 level (Jkind.value ~why:Type_argument)],
38363835
ref Mnil))
38373836
else
3838-
newvar2 level l_arg
3837+
newvar2 level k_arg
38393838
in
38403839
newty2 ~level (Tpoly(t1, []))
38413840
end
38423841
in
3843-
let ty_ret = newvar2 level l_res in
3842+
let ty_ret = newvar2 level k_res in
38443843
let arg_mode = Mode.Alloc.newvar () in
38453844
let ret_mode = Mode.Alloc.newvar () in
38463845
let t' =

typing/ctype.mli

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ val create_scope : unit -> int
5555
val newty: type_desc -> type_expr
5656
val new_scoped_ty: int -> type_desc -> type_expr
5757
val newvar: ?name:string -> Jkind.t -> type_expr
58+
val new_rep_var :
59+
?name:string -> why:Jkind.concrete_jkind_reason -> unit ->
60+
type_expr * Jkind.sort
61+
(* Return a fresh representable variable, along with its sort *)
5862
val newvar2: ?name:string -> int -> Jkind.t -> type_expr
5963
(* Return a fresh variable *)
6064
val new_global_var: ?name:string -> Jkind.t -> type_expr

typing/jkind.ml

+5-2
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,12 @@ let get_required_layouts_level (context : annotation_context) (jkind : const) :
471471
(* construction *)
472472

473473
let of_new_sort_var ~why =
474-
fresh_jkind (Sort (Sort.new_var ())) ~why:(Concrete_creation why)
474+
let sort = Sort.new_var () in
475+
fresh_jkind (Sort sort) ~why:(Concrete_creation why), sort
475476

476-
let of_sort ~why s = fresh_jkind (Sort s) ~why:(Concrete_creation why)
477+
let of_new_sort ~why = fst (of_new_sort_var ~why)
478+
479+
let of_sort_for_error ~why s = fresh_jkind (Sort s) ~why:(Concrete_creation why)
477480

478481
let of_const ~why : const -> t = function
479482
| Any -> fresh_jkind Any ~why

typing/jkind.mli

+9-2
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,17 @@ val float64 : why:float64_creation_reason -> t
348348
(******************************)
349349
(* construction *)
350350

351+
(** Create a fresh sort variable, packed into a jkind, returning both
352+
the resulting kind and the sort. *)
353+
val of_new_sort_var : why:concrete_jkind_reason -> t * sort
354+
351355
(** Create a fresh sort variable, packed into a jkind. *)
352-
val of_new_sort_var : why:concrete_jkind_reason -> t
356+
val of_new_sort : why:concrete_jkind_reason -> t
353357

354-
val of_sort : why:concrete_jkind_reason -> sort -> t
358+
(** There should not be a need to convert a sort to a jkind, but this is
359+
occasionally useful for formatting error messages. Do not use in actual
360+
type-checking. *)
361+
val of_sort_for_error : why:concrete_jkind_reason -> sort -> t
355362

356363
val of_const : why:creation_reason -> const -> t
357364

typing/typeclass.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ and class_expr_aux cl_num val_env met_env virt self_scope scl =
13981398
Typecore.escape ~loc ~env:val_env ~reason:Other mode;
13991399
if not (Jkind.Sort.(equate sort value))
14001400
then let viol = Jkind.Violation.of_ (Not_a_subjkind(
1401-
Jkind.of_sort ~why:Let_binding sort,
1401+
Jkind.of_sort_for_error ~why:Let_binding sort,
14021402
Jkind.value ~why:Class_let_binding))
14031403
in
14041404
raise (Error(loc, met_env,

typing/typecore.ml

+19-40
Original file line numberDiff line numberDiff line change
@@ -3298,13 +3298,10 @@ let collect_unknown_apply_args env funct ty_fun mode_fun rev_args sargs ret_tvar
32983298
let ty_fun = expand_head env ty_fun in
32993299
match get_desc ty_fun with
33003300
| Tvar _ ->
3301-
let sort_arg = Jkind.Sort.new_var () in
3302-
let ty_arg_mono =
3303-
newvar (Jkind.of_sort ~why:Function_argument sort_arg)
3304-
in
3301+
let ty_arg_mono, sort_arg = new_rep_var ~why:Function_argument () in
33053302
let ty_arg = newmono ty_arg_mono in
33063303
let ty_res =
3307-
newvar (Jkind.of_new_sort_var ~why:Function_result)
3304+
newvar (Jkind.of_new_sort ~why:Function_result)
33083305
in
33093306
if ret_tvar &&
33103307
not (is_prim ~name:"%identity" funct) &&
@@ -3817,7 +3814,7 @@ let rec approx_type env sty =
38173814
let arg =
38183815
if is_optional p
38193816
then type_option (newvar (Jkind.value ~why:Type_argument))
3820-
else newvar (Jkind.of_new_sort_var ~why:Function_argument)
3817+
else newvar (Jkind.of_new_sort ~why:Function_argument)
38213818
in
38223819
let ret = approx_type env sty in
38233820
let marg = Alloc.of_const arg_mode in
@@ -4912,10 +4909,9 @@ and type_expect_
49124909
tuple_pat_mode mode modes, mode_tuple mode modes
49134910
in
49144911
begin_def ();
4915-
let sort = Jkind.Sort.new_var () in
4912+
let expected_ty, sort = new_rep_var ~why:Match () in
49164913
let arg =
4917-
type_expect env arg_expected_mode sarg
4918-
(mk_expected (newvar (Jkind.of_sort ~why:Match sort)))
4914+
type_expect env arg_expected_mode sarg (mk_expected expected_ty)
49194915
in
49204916
end_def ();
49214917
if maybe_expansive arg then lower_contravariant env arg.exp_type;
@@ -5079,7 +5075,7 @@ and type_expect_
50795075
in
50805076
match expected_opath, opt_exp_opath with
50815077
| None, None ->
5082-
newvar (Jkind.of_new_sort_var ~why:Record_projection), None
5078+
newvar (Jkind.of_new_sort ~why:Record_projection), None
50835079
| Some _, None -> ty_expected, expected_opath
50845080
| Some(_, _, true), Some _ -> ty_expected, expected_opath
50855081
| (None | Some (_, _, false)), Some (_, p', _) ->
@@ -5244,7 +5240,7 @@ and type_expect_
52445240
type_label_access env srecord Env.Mutation lid in
52455241
let ty_record =
52465242
if expected_type = None
5247-
then newvar (Jkind.of_new_sort_var ~why:Record_assignment)
5243+
then newvar (Jkind.of_new_sort ~why:Record_assignment)
52485244
else record.exp_type
52495245
in
52505246
let (label_loc, label, newval) =
@@ -5864,29 +5860,19 @@ and type_expect_
58645860
let spat_params, ty_params, param_sort =
58655861
let initial_jkind, initial_sort = match sands with
58665862
| [] ->
5867-
let sort = Jkind.Sort.new_var () in
5868-
Jkind.of_sort ~why:Function_argument sort, sort
5863+
Jkind.of_new_sort_var ~why:Function_argument
58695864
(* CR layouts v5: eliminate value requirement for tuple elements *)
58705865
| _ -> Jkind.value ~why:Tuple_element, Jkind.Sort.value
58715866
in
58725867
loop slet.pbop_pat (newvar initial_jkind) initial_sort sands
58735868
in
5874-
let body_sort = Jkind.Sort.new_var () in
5875-
let ty_func_result =
5876-
newvar (Jkind.of_sort ~why:Function_result body_sort)
5877-
in
5869+
let ty_func_result, body_sort = new_rep_var ~why:Function_result () in
58785870
let arrow_desc = Nolabel, Alloc.legacy, Alloc.legacy in
58795871
let ty_func =
58805872
newty (Tarrow(arrow_desc, newmono ty_params, ty_func_result, commu_ok))
58815873
in
5882-
let op_result_sort = Jkind.Sort.new_var () in
5883-
let ty_result =
5884-
newvar (Jkind.of_sort ~why:Function_result op_result_sort)
5885-
in
5886-
let sort_andops = Jkind.Sort.new_var () in
5887-
let ty_andops =
5888-
newvar (Jkind.of_sort ~why:Function_argument sort_andops)
5889-
in
5874+
let ty_result, op_result_sort = new_rep_var ~why:Function_result () in
5875+
let ty_andops, sort_andops = new_rep_var ~why:Function_argument () in
58905876
let ty_op =
58915877
newty (Tarrow(arrow_desc, newmono ty_andops,
58925878
newty (Tarrow(arrow_desc, newmono ty_func,
@@ -7072,8 +7058,7 @@ and type_statement ?explanation ?(position=RNontail) env sexp =
70727058
getting a sort variable for its jkind. *)
70737059
(* CR layouts v10: Abstract jkinds will introduce cases where we really
70747060
have [any] and can't get a sort here. *)
7075-
let sort = Jkind.Sort.new_var () in
7076-
let tv = newvar (Jkind.of_sort ~why:Statement sort) in
7061+
let tv, sort = new_rep_var ~why:Statement () in
70777062
if is_Tvar ty && get_level ty > get_level tv then
70787063
Location.prerr_warning
70797064
(final_subexpression exp).exp_loc
@@ -7448,9 +7433,8 @@ and type_let
74487433
attrs, pat_mode, exp_mode, spat)
74497434
spat_sexp_list in
74507435
let is_recursive = (rec_flag = Recursive) in
7451-
let sorts = List.map (fun _ -> Jkind.Sort.new_var ()) spatl in
7452-
let nvs =
7453-
List.map (fun s -> newvar (Jkind.of_sort ~why:Let_binding s)) sorts
7436+
let nvs, sorts =
7437+
List.split (List.map (fun _ -> new_rep_var ~why:Let_binding ()) spatl)
74547438
in
74557439
if is_recursive then begin_def ();
74567440
let (pat_list, new_env, force, pvs, mvs) =
@@ -7713,14 +7697,9 @@ and type_andops env sarg sands expected_sort expected_ty =
77137697
if !Clflags.principal then begin_def ();
77147698
let op_path, op_desc = type_binding_op_ident env sop in
77157699
let op_type = op_desc.val_type in
7716-
let sort_arg = Jkind.Sort.new_var () in
7717-
let ty_arg = newvar (Jkind.of_sort ~why:Function_argument sort_arg) in
7718-
let sort_rest = Jkind.Sort.new_var () in
7719-
let ty_rest = newvar (Jkind.of_sort ~why:Function_argument sort_rest) in
7720-
let op_result_sort = Jkind.Sort.new_var () in
7721-
let ty_result =
7722-
newvar (Jkind.of_sort ~why:Function_result op_result_sort)
7723-
in
7700+
let ty_arg, sort_arg = new_rep_var ~why:Function_argument () in
7701+
let ty_rest, sort_rest = new_rep_var ~why:Function_argument () in
7702+
let ty_result, op_result_sort = new_rep_var ~why:Function_result () in
77247703
let arrow_desc = (Nolabel,Alloc.legacy,Alloc.legacy) in
77257704
let ty_rest_fun =
77267705
newty (Tarrow(arrow_desc, newmono ty_arg, ty_result, commu_ok))
@@ -8239,8 +8218,8 @@ let type_expression env jkind sexp =
82398218
maybe_check_uniqueness_exp exp; exp
82408219
82418220
let type_representable_expression ~why env sexp =
8242-
let sort = Jkind.Sort.new_var () in
8243-
let exp = type_expression env (Jkind.of_sort ~why sort) sexp in
8221+
let jkind, sort = Jkind.of_new_sort_var ~why in
8222+
let exp = type_expression env jkind sexp in
82448223
exp, sort
82458224
82468225
let type_expression env sexp =

typing/typetexp.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ let transl_type_param env path styp =
464464
to ask for it with an annotation. Some restriction here seems necessary
465465
for backwards compatibility (e.g., we wouldn't want [type 'a id = 'a] to
466466
have jkind any). But it might be possible to infer any in some cases. *)
467-
let jkind = Jkind.of_new_sort_var ~why:Unannotated_type_parameter in
467+
let jkind = Jkind.of_new_sort ~why:Unannotated_type_parameter in
468468
let attrs = styp.ptyp_attributes in
469469
match styp.ptyp_desc with
470470
Ptyp_any -> transl_type_param_var env loc attrs None jkind None
@@ -480,7 +480,7 @@ let transl_type_param env path styp =
480480

481481
let get_type_param_jkind path styp =
482482
match Jane_syntax.Core_type.of_ast styp with
483-
| None -> Jkind.of_new_sort_var ~why:Unannotated_type_parameter
483+
| None -> Jkind.of_new_sort ~why:Unannotated_type_parameter
484484
| Some (Jtyp_layout (Ltyp_var { name; jkind }), _attrs) ->
485485
Jkind.of_annotation ~context:(Type_parameter (path, name)) jkind
486486
| Some _ -> Misc.fatal_error "non-type-variable in get_type_param_jkind"

0 commit comments

Comments
 (0)