Skip to content

Commit adea61a

Browse files
committed
flambda-backend: Import 62df46a from ocaml-jst (PR268)
commit 62df46a Author: Nick Roberts <[email protected]> Date: Tue Oct 31 11:42:25 2023 -0400 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 #1960 * Remove mention of now-deprecated t# syntax for poly variant type --------- Co-authored-by: alanechang <[email protected]>
1 parent b6bcd6a commit adea61a

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,8 @@ Error: The type constructor float# expects 0 argument(s),
284284
type t = #float;;
285285
[%%expect {|
286286

287-
Line 1, characters 10-15:
287+
Line 1, characters 9-15:
288288
1 | type t = #float;;
289-
^^^^^
290-
Error: Unbound class type float
291-
Hint: Did you mean float#?
289+
^^^^^^
290+
Error: float isn't a class type. Did you mean the unboxed type float#?
292291
|}]

typing/env.ml

Lines changed: 1 addition & 6 deletions
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

Lines changed: 20 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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)