Skip to content

Commit 1f5bea5

Browse files
authored
5.5 support: Backport parser code changes (#2782)
Backport refactors and other changes made upstream that do not change the language syntax. The goal is to reduce the diff with upstream to make future updates easier. * Reduce diff in arg_label The location is different but no comment moved in the tests.
1 parent 69e6ee4 commit 1f5bea5

19 files changed

Lines changed: 1150 additions & 571 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ profile. This started with version 0.26.0.
88

99
### Highlight
1010

11-
- \* Support OCaml 5.5 syntax (#2772, #2774, #2775, #2777, #2780, #2781, @Julow)
11+
- \* Support OCaml 5.5 syntax
12+
(#2772, #2774, #2775, #2777, #2780, #2781, #2782, @Julow)
1213
The update brings several tiny changes, they are listed below.
1314

1415
- \* Update Odoc's parser to 3.0 (#2757, @Julow)

lib/Ast.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,9 @@ end = struct
966966
| Pconstraint t -> f t
967967
| Pcoerce (t1, t2) -> Option.exists t1 ~f || f t2
968968
in
969-
let check_package_type ptyp = List.exists ptyp.ppt_cstrs ~f:snd_f in
969+
let check_package_type ptyp =
970+
List.exists ptyp.ppt_constraints ~f:snd_f
971+
in
970972
match ctx with
971973
| Pld (PTyp t1) -> assert (typ == t1)
972974
| Pld _ -> assert false
@@ -995,10 +997,10 @@ end = struct
995997
| Ptyp_class (_, l) -> assert (List.exists l ~f)
996998
| Ptyp_functor (_, _, ptyp, rhs) ->
997999
assert (rhs == typ || check_package_type ptyp) )
998-
| Td {ptype_params; ptype_cstrs; ptype_kind; ptype_manifest; _} ->
1000+
| Td {ptype_params; ptype_constraints; ptype_kind; ptype_manifest; _} ->
9991001
assert (
10001002
List.exists ptype_params ~f:fst_f
1001-
|| List.exists ptype_cstrs ~f:(fun (t1, t2, _) ->
1003+
|| List.exists ptype_constraints ~f:(fun (t1, t2, _) ->
10021004
typ == t1 || typ == t2 )
10031005
|| ( match ptype_kind with
10041006
| Ptype_variant cd1N ->

lib/Exposed.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module Right = struct
6060

6161
let type_declaration = function
6262
| {ptype_attributes= {attrs_after= _ :: _; _}; _} -> false
63-
| {ptype_cstrs= _ :: _ as cstrs; _} ->
63+
| {ptype_constraints= _ :: _ as cstrs; _} ->
6464
(* type a = ... constraint left = < ... > *)
6565
list ~elt:(fun (_left, right, _loc) -> core_type right) cstrs
6666
| {ptype_kind= Ptype_open | Ptype_record _ | Ptype_external _; _} ->

lib/Fmt_ast.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,12 +1099,12 @@ and fmt_package_type_cnstrs c ctx cnstrs =
10991099
list_fl cnstrs fmt_cstr
11001100

11011101
and fmt_package_type c ctx ~parens ~pro ptyp =
1102-
let {ppt_path; ppt_cstrs; ppt_attrs; ppt_loc} = ptyp in
1102+
let {ppt_path; ppt_constraints; ppt_attrs; ppt_loc} = ptyp in
11031103
Cmts.fmt c ppt_loc
11041104
(hvbox 2
11051105
(Params.parens_if parens c.conf
11061106
( hovbox 0 (pro $ fmt_longident_loc c ppt_path)
1107-
$ fmt_package_type_cnstrs c ctx ppt_cstrs
1107+
$ fmt_package_type_cnstrs c ctx ppt_constraints
11081108
$ fmt_attributes c ppt_attrs ) ) )
11091109

11101110
and fmt_row_field c ctx {prf_desc; prf_attributes; prf_loc} =
@@ -3478,7 +3478,7 @@ and fmt_type_declaration c ?(kw = "") ?(nonrec_kw = "") ?name ?(eq = "=")
34783478
@@
34793479
let { ptype_name= {txt; loc}
34803480
; ptype_params
3481-
; ptype_cstrs
3481+
; ptype_constraints
34823482
; ptype_kind
34833483
; ptype_private= priv
34843484
; ptype_manifest= m
@@ -3574,7 +3574,7 @@ and fmt_type_declaration c ?(kw = "") ?(nonrec_kw = "") ?name ?(eq = "=")
35743574
( doc_before
35753575
$ hvbox 0
35763576
( hvbox c.conf.fmt_opts.type_decl_indent.v
3577-
(fmt_manifest_kind $ fmt_cstrs ptype_cstrs)
3577+
(fmt_manifest_kind $ fmt_cstrs ptype_constraints)
35783578
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after )
35793579
$ doc_after )
35803580

@@ -4549,7 +4549,8 @@ and fmt_module_expr ?(dock_struct = true) c ({ast= m; ctx= ctx0} as xmod) =
45494549
$ after ) }
45504550
| Pmod_unpack (e, ty1, ty2) ->
45514551
let package_type sep
4552-
{ppt_path= lid; ppt_cstrs= cstrs; ppt_attrs= attrs; ppt_loc} =
4552+
{ppt_path= lid; ppt_constraints= cstrs; ppt_attrs= attrs; ppt_loc}
4553+
=
45534554
(* TODO: Use [fmt_package_type]. *)
45544555
break 1 (Params.Indent.mod_unpack_annot c.conf)
45554556
$ hovbox 0

vendor/ocaml-common/location.ml

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type 'a loc = {
7373
let mkloc txt loc = { txt ; loc }
7474
let mknoloc txt = mkloc txt none
7575

76+
let map f x = { x with txt = f x.txt }
77+
7678
(******************************************************************************)
7779
(* Input info *)
7880

@@ -749,22 +751,15 @@ let batch_mode_printer : report_printer =
749751
| Misc.Error_style.Short ->
750752
()
751753
in
752-
Format.fprintf ppf "@[<v>%a:@ %a@]" print_loc loc
754+
Format.fprintf ppf "%a:@ %a" print_loc loc
753755
(Fmt.compat highlight) loc
754756
in
755-
let pp_txt ppf txt = Format.fprintf ppf "@[%a@]" Fmt.Doc.format txt in
757+
let pp_txt ppf txt = Format.fprintf ppf "%a" Fmt.Doc.format txt in
756758
let pp_footnote ppf f =
757759
Option.iter (Format.fprintf ppf "@,%a" pp_txt) f
758760
in
759-
let pp self ppf report =
760-
setup_tags ();
761-
separate_new_message ppf;
762-
(* Make sure we keep [num_loc_lines] updated.
763-
The tabulation box is here to give submessage the option
764-
to be aligned with the main message box
765-
*)
766-
print_updating_num_loc_lines ppf (fun ppf () ->
767-
Format.fprintf ppf "@[<v>%a%a%a: %a%a%a%a%a@]@."
761+
let error_format self ppf report =
762+
Format.fprintf ppf "@[<v>%a%a%a: %a@[%a@]%a%a%a@]@."
768763
Format.pp_open_tbox ()
769764
(self.pp_main_loc self report) report.main.loc
770765
(self.pp_report_kind self report) report.kind
@@ -773,7 +768,30 @@ let batch_mode_printer : report_printer =
773768
(self.pp_submsgs self report) report.sub
774769
pp_footnote report.footnote
775770
Format.pp_close_tbox ()
776-
) ()
771+
in
772+
let warning_format self ppf report =
773+
Format.fprintf ppf "@[<v>%a@[<b 2>%a: %a@]%a%a@]@."
774+
(self.pp_main_loc self report) report.main.loc
775+
(self.pp_report_kind self report) report.kind
776+
(self.pp_main_txt self report) report.main.txt
777+
(self.pp_submsgs self report) report.sub
778+
pp_footnote report.footnote
779+
in
780+
let pp self ppf report =
781+
setup_tags ();
782+
separate_new_message ppf;
783+
let printer ppf () = match report.kind with
784+
| Report_warning _
785+
| Report_warning_as_error _
786+
| Report_alert _ | Report_alert_as_error _ ->
787+
warning_format self ppf report
788+
| Report_error -> error_format self ppf report
789+
in
790+
(* Make sure we keep [num_loc_lines] updated.
791+
The tabulation box is here to give submessage the option
792+
to be aligned with the main message box
793+
*)
794+
print_updating_num_loc_lines ppf printer ()
777795
in
778796
let pp_report_kind _self _ ppf = function
779797
| Report_error -> Format.fprintf ppf "@{<error>Error@}"
@@ -796,9 +814,12 @@ let batch_mode_printer : report_printer =
796814
) msgs
797815
in
798816
let pp_submsg self report ppf { loc; txt } =
799-
Format.fprintf ppf "@[%a %a@]"
800-
(self.pp_submsg_loc self report) loc
801-
(self.pp_submsg_txt self report) txt
817+
if loc.loc_ghost then
818+
Format.fprintf ppf "@[%a@]" (self.pp_submsg_txt self report) txt
819+
else
820+
Format.fprintf ppf "%a @[%a@]"
821+
(self.pp_submsg_loc self report) loc
822+
(self.pp_submsg_txt self report) txt
802823
in
803824
let pp_submsg_loc self report ppf loc =
804825
if not loc.loc_ghost then
@@ -864,6 +885,18 @@ let mkerror loc sub footnote txt =
864885
let errorf ?(loc = none) ?(sub = []) ?(footnote=Fun.const None) =
865886
Fmt.kdoc_printf (mkerror loc sub footnote)
866887

888+
(* Removed because it's unused and pulls more dependencies.
889+
let aligned_error_hint
890+
?(loc = none) ?(sub = []) ?(footnote=Fun.const None) fmt =
891+
Fmt.kdoc_printf (fun main hint ->
892+
match hint with
893+
| None -> mkerror loc sub footnote main
894+
| Some hint ->
895+
let main, hint = Misc.align_error_hint ~main ~hint in
896+
mkerror loc (mknoloc hint :: sub) footnote main
897+
) fmt
898+
*)
899+
867900
let error ?(loc = none) ?(sub = []) ?(footnote=Fun.const None) msg_str =
868901
mkerror loc sub footnote Fmt.Doc.(string msg_str empty)
869902

@@ -881,11 +914,10 @@ let default_warning_alert_reporter report mk (loc: t) w : report option =
881914
match report w with
882915
| `Inactive -> None
883916
| `Active { Warnings.id; message; is_error; sub_locs } ->
884-
let msg_of_str str = Format_doc.Doc.(empty |> string str) in
885917
let kind = mk is_error id in
886-
let main = { loc; txt = msg_of_str message } in
918+
let main = { loc; txt = message } in
887919
let sub = List.map (fun (loc, sub_message) ->
888-
{ loc; txt = msg_of_str sub_message }
920+
{ loc; txt = sub_message }
889921
) sub_locs in
890922
Some { kind; main; sub; footnote=None }
891923

@@ -953,7 +985,7 @@ let auto_include_alert lib =
953985
{Warnings.kind="ocaml_deprecated_auto_include"; use=none; def=none;
954986
message = Format.asprintf "@[@\n%a@]" Format.pp_print_text message}
955987
in
956-
prerr_alert none alert
988+
prerr_alert (in_file !input_name) alert
957989

958990
let deprecated_script_alert program =
959991
let message = Fmt.asprintf "\

vendor/ocaml-common/location.mli

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,31 @@ type t = Warnings.loc = {
2626
loc_start: Lexing.position;
2727
loc_end: Lexing.position;
2828
loc_ghost: bool;
29-
}
29+
}
30+
(** [t] represents a range of characters in the source code.
31+
32+
loc_ghost=false whenever the AST described by the location can be parsed
33+
from the location. In all other cases, loc_ghost must be true. Most
34+
locations produced by the parser have loc_ghost=false.
35+
When loc_ghost=true, the location is usually a best effort approximation.
36+
37+
This info is used by tools like merlin that want to relate source code with
38+
parsetrees or later asts. ocamlprof skips instrumentation of ghost nodes.
39+
40+
Example: in `let f x = x`, we have:
41+
- a structure item at location "let f x = x"
42+
- a pattern "f" at location "f"
43+
- an expression "fun x -> x" at location "x = x" with loc_ghost=true
44+
- a pattern "x" at location "x"
45+
- an expression "x" at location "x"
46+
In this case, every node has loc_ghost=false, except the node "fun x -> x",
47+
since [Parser.expression (Lexing.from_string "x = x")] would fail to parse.
48+
By contrast, in `let f = fun x -> x`, every node has loc_ghost=false.
49+
50+
Line directives can modify the filenames and line numbers arbitrarily,
51+
which is orthogonal to loc_ghost, which describes the range of characters
52+
from loc_start.pos_cnum to loc_end.pos_cnum in the parsed string.
53+
*)
3054

3155
(** Note on the use of Lexing.position in this module.
3256
If [pos_fname = ""], then use [!input_name] instead.
@@ -71,6 +95,7 @@ type 'a loc = {
7195

7296
val mknoloc : 'a -> 'a loc
7397
val mkloc : 'a -> t -> 'a loc
98+
val map : ('a -> 'b) -> 'a loc -> 'b loc
7499

75100

76101
(** {1 Input info} *)
@@ -233,7 +258,7 @@ type report_printer = {
233258
Format.formatter -> Format_doc.t -> unit;
234259
}
235260
(** A printer for [report]s, defined using open-recursion.
236-
The goal is to make it easy to define new printers by re-using code from
261+
The goal is to make it easy to define new printers by reusing code from
237262
existing ones.
238263
*)
239264

@@ -338,6 +363,15 @@ val error: ?loc:t -> ?sub:msg list -> ?footnote:delayed_msg -> string -> error
338363
val errorf: ?loc:t -> ?sub:msg list -> ?footnote:delayed_msg ->
339364
('a, Format_doc.formatter, unit, error) format4 -> 'a
340365

366+
(*
367+
val aligned_error_hint:
368+
?loc:t -> ?sub:msg list -> ?footnote:delayed_msg ->
369+
('a, Format_doc.formatter, unit, Format_doc.t option -> error) format4 -> 'a
370+
(** [aligned_error_hint ?loc ?sub ?footnote fmt ... aligned_hint] produces an
371+
error report where the potential [aligned_hint] message has been aligned
372+
with the main error message before being added to the list of submessages.*)
373+
*)
374+
341375
val error_of_printer: ?loc:t -> ?sub:msg list -> ?footnote:delayed_msg ->
342376
(Format_doc.formatter -> 'a -> unit) -> 'a -> error
343377

0 commit comments

Comments
 (0)