Skip to content

Commit dbb4a31

Browse files
authored
Merge pull request ocaml#13813 from Octachron/tune_spellchecker_hint
ocaml#13788: spellchecking hint, keep module context
2 parents fd643d3 + 22ab57d commit dbb4a31

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

Changes

+5
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ Working version
306306
references another recursive module type.
307307
(Stefan Muenzel, review by Florian Angeletti and Gabriel Scherer)
308308

309+
- #13788, #13813: Keep the module context in spellchecking hints.
310+
`Fun.protact` now prompts `Did you mean "Fun.protect?"` rather than
311+
`Did you mean "protect?"`.
312+
(Florian Angeletti, suggestion by Daniel Bünzli, review by Gabriel Scherer)
313+
309314
- #13809: Distinguish `(module M : S)` and `(module M) : (module S)` and
310315
change locations of error messages when `S` is ill-typed in `(module S)`
311316
(Samuel Vivien, review by Florian Angeletti and Gabriel Scherer)

testsuite/tests/messages/spellcheck.ml

+17-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Line 1, characters 8-19:
2222
1 | let _ = Fun.pratect
2323
^^^^^^^^^^^
2424
Error: Unbound value "Fun.pratect"
25-
Hint: Did you mean "protect"?
25+
Hint: Did you mean "Fun.protect"?
2626
|}];;
2727

2828
type 'a t = 'a aray
@@ -40,7 +40,7 @@ Line 1, characters 11-22:
4040
1 | module _ = Stdlib.Aray
4141
^^^^^^^^^^^
4242
Error: Unbound module "Stdlib.Aray"
43-
Hint: Did you mean "Array"?
43+
Hint: Did you mean "Stdlib.Array"?
4444
|}];;
4545

4646
let x = Same 42
@@ -213,3 +213,18 @@ Line 3, characters 18-35:
213213
Error: Unbound instance variable "foobaz"
214214
Hint: Did you mean "foobar"?
215215
|}];;
216+
217+
let closely = ()
218+
module M = struct
219+
let close = ()
220+
end
221+
let () = M.closer
222+
[%%expect {|
223+
val closely : unit = ()
224+
module M : sig val close : unit end
225+
Line 5, characters 9-17:
226+
5 | let () = M.closer
227+
^^^^^^^^
228+
Error: Unbound value "M.closer"
229+
Hint: Did you mean "M.close"?
230+
|}]

typing/env.ml

+7-6
Original file line numberDiff line numberDiff line change
@@ -3536,14 +3536,20 @@ open Format_doc
35363536
let print_path: Path.t printer ref = ref (fun _ _ -> assert false)
35373537
let pp_path ppf l = !print_path ppf l
35383538

3539+
module Style = Misc.Style
3540+
3541+
let quoted_longident = Style.as_inline_code Pprintast.Doc.longident
3542+
let quoted_constr = Style.as_inline_code Pprintast.Doc.constr
3543+
35393544
let spellcheck ppf extract env lid =
35403545
let choices ~path name = Misc.spellcheck (extract path env) name in
35413546
match lid with
35423547
| Longident.Lapply _ -> ()
35433548
| Longident.Lident s ->
35443549
Misc.did_you_mean ppf (fun () -> choices ~path:None s)
35453550
| Longident.Ldot (r, s) ->
3546-
Misc.did_you_mean ppf (fun () -> choices ~path:(Some r) s)
3551+
let pp ppf s = quoted_longident ppf (Longident.Ldot(r,s)) in
3552+
Misc.did_you_mean ~pp ppf (fun () -> choices ~path:(Some r) s)
35473553

35483554
let spellcheck_name ppf extract env name =
35493555
Misc.did_you_mean ppf
@@ -3572,11 +3578,6 @@ let extract_instance_variables env =
35723578
| Val_ivar _ -> name :: acc
35733579
| _ -> acc) None env []
35743580

3575-
module Style = Misc.Style
3576-
3577-
let quoted_longident = Style.as_inline_code Pprintast.Doc.longident
3578-
let quoted_constr = Style.as_inline_code Pprintast.Doc.constr
3579-
35803581
let report_lookup_error_doc _loc env ppf = function
35813582
| Unbound_value(lid, hint) -> begin
35823583
fprintf ppf "Unbound value %a" quoted_longident lid;

typing/typetexp.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ let report_error_doc env ppf = function
876876
| Unbound_type_variable (name, in_scope_names) ->
877877
fprintf ppf "The type variable %a is unbound in this type declaration.@ %a"
878878
Style.inline_code name
879-
did_you_mean (fun () -> Misc.spellcheck in_scope_names name )
879+
(did_you_mean ?pp:None) (fun () -> Misc.spellcheck in_scope_names name )
880880
| No_type_wildcards ->
881881
fprintf ppf "A type wildcard %a is not allowed in this type declaration."
882882
Style.inline_code "_"

utils/misc.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ let spellcheck env name =
10141014
fst (List.fold_left (compare name) ([], max_int) env)
10151015

10161016

1017-
let did_you_mean ppf get_choices =
1017+
let did_you_mean ?(pp=Style.inline_code) ppf get_choices =
10181018
let open Format_doc in
10191019
(* flush now to get the error report early, in the (unheard of) case
10201020
where the search in the get_choices function would take a bit of
@@ -1026,9 +1026,9 @@ let did_you_mean ppf get_choices =
10261026
| choices ->
10271027
let rest, last = split_last choices in
10281028
fprintf ppf "@\n@[@{<hint>Hint@}: Did you mean %a%s%a?@]"
1029-
(pp_print_list ~pp_sep:comma Style.inline_code) rest
1029+
(pp_print_list ~pp_sep:comma pp) rest
10301030
(if rest = [] then "" else " or ")
1031-
Style.inline_code last
1031+
pp last
10321032

10331033
module Error_style = struct
10341034
type setting =

utils/misc.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ val spellcheck : string list -> string -> string list
450450
[name] that it may be a typo for one of them. *)
451451

452452
val did_you_mean :
453-
Format_doc.formatter -> (unit -> string list) -> unit
453+
?pp:string Format_doc.printer -> Format_doc.formatter ->
454+
(unit -> string list) -> unit
454455
(** [did_you_mean ppf get_choices] hints that the user may have meant
455456
one of the option returned by calling [get_choices]. It does nothing
456457
if the returned list is empty.

0 commit comments

Comments
 (0)