Skip to content

Commit 718e914

Browse files
authored
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
1 parent c0233bf commit 718e914

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,21 @@ 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 is neither a polymorphic variant nor a class type.
262+
Did you mean the unboxed type float#?
263+
|}]
264+
265+
(* Hint should not show up in this case *)
266+
class type floot = object end
267+
class type c = float
268+
[%%expect {|
269+
class type floot = object end
270+
Line 2, characters 15-20:
271+
2 | class type c = float
272+
^^^^^
261273
Error: Unbound class type float
262-
Hint: Did you mean float#?
274+
Hint: Did you mean floot?
263275
|}]

ocaml/typing/env.ml

+1-6
Original file line numberDiff line numberDiff line change
@@ -3888,12 +3888,7 @@ let report_lookup_error _loc env ppf = function
38883888
end
38893889
| Unbound_cltype lid ->
38903890
fprintf ppf "Unbound class type %a" !print_longident lid;
3891-
begin match lid with
3892-
| Lident "float" ->
3893-
Misc.did_you_mean ppf (fun () -> ["float#"])
3894-
| Lident _ | Ldot _ | Lapply _ ->
3895-
spellcheck ppf extract_cltypes env lid
3896-
end;
3891+
spellcheck ppf extract_cltypes env lid
38973892
| Unbound_instance_variable s ->
38983893
fprintf ppf "Unbound instance variable %s" s;
38993894
spellcheck_name ppf extract_instance_variables env s;

ocaml/typing/typetexp.ml

+14
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
@@ -715,6 +716,16 @@ and transl_type_aux env policy mode styp =
715716
let path, decl = Env.find_type_by_name lid2 env in
716717
ignore(Env.lookup_cltype ~loc:lid.loc lid.txt env);
717718
(path, decl, false)
719+
with Not_found -> try
720+
(* Raise a different error if it matches the name of an unboxed type *)
721+
let lid3 =
722+
match lid.txt with
723+
Longident.Lident s -> Longident.Lident (s ^ "#")
724+
| Longident.Ldot(r, s) -> Longident.Ldot (r, s ^ "#")
725+
| Longident.Lapply(_, _) -> fatal_error "Typetexp.transl_type"
726+
in
727+
ignore (Env.find_type_by_name lid3 env : Path.t * Types.type_declaration);
728+
raise (Error (styp.ptyp_loc, env, Did_you_mean_unboxed lid.txt))
718729
with Not_found ->
719730
ignore (Env.lookup_cltype ~loc:lid.loc lid.txt env); assert false
720731
in
@@ -1388,6 +1399,9 @@ let report_error env ppf = function
13881399
fprintf ppf "@[<b 2>Bad layout annotation:@ %a@]"
13891400
(Jkind.Violation.report_with_offender
13901401
~offender:(fun ppf -> Printtyp.type_expr ppf ty)) violation
1402+
| Did_you_mean_unboxed lid ->
1403+
fprintf ppf "@[%a is neither a polymorphic variant nor a class type.@ \
1404+
Did you mean the unboxed type %a#?@]" longident lid longident lid
13911405

13921406
let () =
13931407
Location.register_error_of_exn

ocaml/typing/typetexp.mli

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

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

0 commit comments

Comments
 (0)