Skip to content

Commit 62df46a

Browse files
OCaml 5: Backport flambda-backend#1960 (#268)
* flambda-backend: Error message: add hint for unboxed types (#1960) * generalize hint to all unboxed types * add regression test * revert previous change * extra logic in Ptyp_class * better error wording * Fix backport of ocaml-flambda#1960 * Remove mention of now-deprecated t# syntax for poly variant type --------- Co-authored-by: alanechang <143514551+alanechang@users.noreply.github.com>
1 parent b26b2bd commit 62df46a

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

testsuite/tests/typing-layouts-float64/parsing.ml

+14-3
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,20 @@ Error: The type constructor float# expects 0 argument(s),
255255
(* Hint for #float *)
256256
type t = #float;;
257257
[%%expect {|
258-
Line 1, characters 10-15:
258+
Line 1, characters 9-15:
259259
1 | type t = #float;;
260-
^^^^^
260+
^^^^^^
261+
Error: float isn't a class type. Did you mean the unboxed type float#?
262+
|}]
263+
264+
(* Hint should not show up in this case *)
265+
class type floot = object end
266+
class type c = float
267+
[%%expect {|
268+
class type floot = object end
269+
Line 2, characters 15-20:
270+
2 | class type c = float
271+
^^^^^
261272
Error: Unbound class type float
262-
Hint: Did you mean float#?
273+
Hint: Did you mean floot?
263274
|}]

typing/env.ml

+1-6
Original file line numberDiff line numberDiff line change
@@ -3901,12 +3901,7 @@ let report_lookup_error _loc env ppf = function
39013901
end
39023902
| Unbound_cltype lid ->
39033903
fprintf ppf "Unbound class type %a" !print_longident lid;
3904-
begin match lid with
3905-
| Lident "float" ->
3906-
Misc.did_you_mean ppf (fun () -> ["float#"])
3907-
| Lident _ | Ldot _ | Lapply _ ->
3908-
spellcheck ppf extract_cltypes env lid
3909-
end;
3904+
spellcheck ppf extract_cltypes env lid
39103905
| Unbound_instance_variable s ->
39113906
fprintf ppf "Unbound instance variable %s" s;
39123907
spellcheck_name ppf extract_instance_variables env s;

typing/typetexp.ml

+20-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type error =
7777
| Non_sort of
7878
{vloc : sort_loc; typ : type_expr; err : Jkind.Violation.t}
7979
| Bad_jkind_annot of type_expr * Jkind.Violation.t
80+
| Did_you_mean_unboxed of Longident.t
8081

8182
exception Error of Location.t * Env.t * error
8283
exception Error_forward of Location.error
@@ -730,8 +731,22 @@ and transl_type_aux env ~row_context ~aliased ~policy mode styp =
730731
ctyp (Ttyp_object (fields, o)) (newobj ty)
731732
| Ptyp_class(lid, stl) ->
732733
let (path, decl) =
733-
let path, decl = Env.lookup_cltype ~loc:lid.loc lid.txt env in
734-
(path, decl.clty_hash_type)
734+
match Env.lookup_cltype ~loc:lid.loc lid.txt env with
735+
| (path, decl) -> (path, decl.clty_hash_type)
736+
(* Raise a different error if it matches the name of an unboxed type *)
737+
| exception
738+
(Env.Error (Lookup_error (_, _, Unbound_cltype _)) as exn)
739+
->
740+
let unboxed_lid : Longident.t =
741+
match lid.txt with
742+
| Lident s -> Lident (s ^ "#")
743+
| Ldot (l, s) -> Ldot (l, s ^ "#")
744+
| Lapply _ -> fatal_error "Typetexp.transl_type"
745+
in
746+
match Env.find_type_by_name unboxed_lid env with
747+
| exception Not_found -> raise exn
748+
| (_ : _ * _) ->
749+
raise (Error (styp.ptyp_loc, env, Did_you_mean_unboxed lid.txt))
735750
in
736751
if List.length stl <> decl.type_arity then
737752
raise(Error(styp.ptyp_loc, env,
@@ -1426,6 +1441,9 @@ let report_error env ppf = function
14261441
fprintf ppf "@[<b 2>Bad layout annotation:@ %a@]"
14271442
(Jkind.Violation.report_with_offender
14281443
~offender:(fun ppf -> Printtyp.type_expr ppf ty)) violation
1444+
| Did_you_mean_unboxed lid ->
1445+
fprintf ppf "@[%a isn't a class type.@ \
1446+
Did you mean the unboxed type %a#?@]" longident lid longident lid
14291447

14301448
let () =
14311449
Location.register_error_of_exn

typing/typetexp.mli

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type error =
119119
| Non_sort of
120120
{vloc : sort_loc; typ : type_expr; err : Jkind.Violation.t}
121121
| Bad_jkind_annot of type_expr * Jkind.Violation.t
122+
| Did_you_mean_unboxed of Longident.t
122123

123124
exception Error of Location.t * Env.t * error
124125

0 commit comments

Comments
 (0)