Skip to content

Commit 91d3de7

Browse files
authored
flambda-backend: Change error message for non-value class lets (ocaml-flambda#1953)
The old way of producing this error required of_sort_for_error, which causes trouble in the modal kinds world. This kills off that terrible function.
1 parent 27e58ec commit 91d3de7

File tree

6 files changed

+13
-16
lines changed

6 files changed

+13
-16
lines changed

testsuite/tests/typing-layouts/basics_alpha.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,8 @@ end
788788
Line 3, characters 11-12:
789789
3 | let VV v = v in
790790
^
791-
Error: Variables bound in a class must have layout value.
792-
v has layout void, which is not a sublayout of value.
791+
Error: The types of variables bound by a 'let' in a class function
792+
must have layout value. Instead, v's type has layout void.
793793
|}];;
794794

795795
(* Hits the Cfk_concrete case of Pcf_val *)

testsuite/tests/typing-layouts/basics_beta.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,8 @@ end
702702
Line 4, characters 8-9:
703703
4 | let d = f u in
704704
^
705-
Error: Variables bound in a class must have layout value.
706-
d has layout float64, which is not a sublayout of value.
705+
Error: The types of variables bound by a 'let' in a class function
706+
must have layout value. Instead, d's type has layout float64.
707707
|}];;
708708

709709
(* Hits the Cfk_concrete case of Pcf_val *)

typing/jkind.ml

-2
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,6 @@ let of_new_sort_var ~why =
490490

491491
let of_new_sort ~why = fst (of_new_sort_var ~why)
492492

493-
let of_sort_for_error ~why s = fresh_jkind (Sort s) ~why:(Concrete_creation why)
494-
495493
let of_const ~why : const -> t = function
496494
| Any -> fresh_jkind Any ~why
497495
| Immediate -> fresh_jkind Immediate ~why

typing/jkind.mli

-5
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,6 @@ val of_new_sort_var : why:concrete_jkind_reason -> t * sort
362362
(** Create a fresh sort variable, packed into a jkind. *)
363363
val of_new_sort : why:concrete_jkind_reason -> t
364364

365-
(** There should not be a need to convert a sort to a jkind, but this is
366-
occasionally useful for formatting error messages. Do not use in actual
367-
type-checking. *)
368-
val of_sort_for_error : why:concrete_jkind_reason -> sort -> t
369-
370365
val of_const : why:creation_reason -> const -> t
371366

372367
(* CR layouts v1.5: remove legacy_immediate when the old attributes mechanism

typing/typeclass.ml

+8-5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type error =
109109
| Closing_self_type of class_signature
110110
| Polymorphic_class_parameter
111111
| Non_value_binding of string * Jkind.Violation.t
112+
| Non_value_let_binding of string * Jkind.sort
112113

113114
exception Error of Location.t * Env.t * error
114115
exception Error_forward of Location.error
@@ -1397,12 +1398,9 @@ and class_expr_aux cl_num val_env met_env virt self_scope scl =
13971398
(fun (loc, mode, sort) ->
13981399
Typecore.escape ~loc ~env:val_env ~reason:Other mode;
13991400
if not (Jkind.Sort.(equate sort value))
1400-
then let viol = Jkind.Violation.of_ (Not_a_subjkind(
1401-
Jkind.of_sort_for_error ~why:Let_binding sort,
1402-
Jkind.value ~why:Class_let_binding))
1403-
in
1401+
then
14041402
raise (Error(loc, met_env,
1405-
Non_value_binding (Ident.name id, viol)))
1403+
Non_value_let_binding (Ident.name id, sort)))
14061404
)
14071405
modes_and_sorts;
14081406
let path = Pident id in
@@ -2248,6 +2246,11 @@ let report_error env ppf = function
22482246
fprintf ppf
22492247
"@[Variables bound in a class must have layout value.@ %a@]"
22502248
(Jkind.Violation.report_with_name ~name:nm) err
2249+
| Non_value_let_binding (nm, sort) ->
2250+
fprintf ppf
2251+
"@[The types of variables bound by a 'let' in a class function@ \
2252+
must have layout value. Instead, %s's type has layout %a.@]"
2253+
nm Jkind.Sort.format sort
22512254

22522255
let report_error env ppf err =
22532256
Printtyp.wrap_printing_env ~error:true

typing/typeclass.mli

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ type error =
125125
| Closing_self_type of class_signature
126126
| Polymorphic_class_parameter
127127
| Non_value_binding of string * Jkind.Violation.t
128+
| Non_value_let_binding of string * Jkind.sort
128129

129130
exception Error of Location.t * Env.t * error
130131
exception Error_forward of Location.error

0 commit comments

Comments
 (0)