Skip to content

Commit 740a8d0

Browse files
Octachronpanglesd
authored andcommitted
Basic fixes for OCaml 5.2.0:
- Typedtree changes - Shape request API changes - Shape uid map changes - Basic support for open in types - Unit_info changes for typemod
1 parent 1cfb484 commit 740a8d0

File tree

8 files changed

+108
-6
lines changed

8 files changed

+108
-6
lines changed

src/loader/cmi.ml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,11 @@ let mark_constructor_args =
316316
#endif
317317

318318
let mark_type_kind = function
319+
#if OCAML_VERSION >= (5,2,0)
320+
| Type_abstract _ -> ()
321+
#else
319322
| Type_abstract -> ()
323+
#endif
320324
#if OCAML_VERSION >= (4,13,0)
321325
| Type_variant (cds,_) ->
322326
#else
@@ -639,7 +643,12 @@ let read_constructor_declaration env parent cd =
639643

640644
let read_type_kind env parent =
641645
let open TypeDecl.Representation in function
642-
| Type_abstract -> None
646+
#if OCAML_VERSION >= (5,2,0)
647+
| Type_abstract _ ->
648+
#else
649+
| Type_abstract ->
650+
#endif
651+
None
643652
#if OCAML_VERSION >= (4,13,0)
644653
| Type_variant (cstrs,_) ->
645654
#else
@@ -716,7 +725,11 @@ let read_type_declaration env parent id decl =
716725
let representation = read_type_kind env (id :> Identifier.DataType.t) decl.type_kind in
717726
let abstr =
718727
match decl.type_kind with
719-
Type_abstract ->
728+
#if OCAML_VERSION >= (5,2,0)
729+
| Type_abstract _ ->
730+
#else
731+
| Type_abstract ->
732+
#endif
720733
decl.type_manifest = None || decl.type_private = Private
721734
| Type_record _ ->
722735
decl.type_private = Private

src/loader/cmt.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,22 @@ let rec read_pattern env parent doc pat =
3333
let open Signature in
3434
match pat.pat_desc with
3535
| Tpat_any -> []
36+
#if OCAML_VERSION < (5,2,0)
3637
| Tpat_var(id, _) ->
38+
#else
39+
| Tpat_var(id,_,_uid) ->
40+
#endif
3741
let open Value in
3842
let id = Env.find_value_identifier env id in
3943
Cmi.mark_type_expr pat.pat_type;
4044
let type_ = Cmi.read_type_expr env pat.pat_type in
4145
let value = Abstract in
4246
[Value {id; source_loc; doc; type_; value}]
47+
#if OCAML_VERSION < (5,2, 0)
4348
| Tpat_alias(pat, id, _) ->
49+
#else
50+
| Tpat_alias(pat, id, _,_) ->
51+
#endif
4452
let open Value in
4553
let id = Env.find_value_identifier env id in
4654
Cmi.mark_type_expr pat.pat_type;

src/loader/cmti.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ let rec read_core_type env container ctyp =
9898
Class(p, params)
9999
| Ttyp_alias(typ, var) ->
100100
let typ = read_core_type env container typ in
101-
Alias(typ, var)
101+
#if OCAML_VERSION >= (5,2,0)
102+
Alias(typ, var.txt)
103+
#else
104+
Alias(typ, var)
105+
#endif
102106
| Ttyp_variant(fields, closed, present) ->
103107
let open TypeExpr.Polymorphic_variant in
104108
let elements =
@@ -142,6 +146,11 @@ let rec read_core_type env container ctyp =
142146
pack_fields
143147
in
144148
Package {path; substitutions}
149+
#if OCAML_VERSION >= (5,2,0)
150+
| Ttyp_open (_p,_l,t) ->
151+
(* TODO: adjust model *)
152+
read_core_type env container t
153+
#endif
145154

146155
let read_value_description env parent vd =
147156
let open Signature in

src/loader/ident_env.cppo.ml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ let rec extract_signature_type_items vis items =
9191
then extract_signature_type_items vis rest
9292
else
9393
let constrs = match td.type_kind with
94+
#if OCAML_VERSION < (5,2,0)
9495
| Types.Type_abstract -> []
96+
#else
97+
| Types.Type_abstract _ -> []
98+
#endif
9599
| Type_record (_, _) -> []
96100
#if OCAML_VERSION < (4,13,0)
97101
| Type_variant cstrs ->
@@ -274,8 +278,18 @@ let rec extract_signature_tree_items : bool -> Typedtree.signature_item list ->
274278
let rec read_pattern hide_item pat =
275279
let open Typedtree in
276280
match pat.pat_desc with
277-
| Tpat_var(id, loc) -> [`Value(id, hide_item, Some loc.loc)]
278-
| Tpat_alias(pat, id, loc) -> `Value(id, hide_item, Some loc.loc) :: read_pattern hide_item pat
281+
#if OCAML_VERSION < (5,2,0)
282+
| Tpat_var(id, loc) ->
283+
#else
284+
| Tpat_var(id, loc, _) ->
285+
#endif
286+
[`Value(id, hide_item, Some loc.loc)]
287+
#if OCAML_VERSION < (5,2,0)
288+
| Tpat_alias(pat, id, loc) ->
289+
#else
290+
| Tpat_alias(pat, id, loc, _) ->
291+
#endif
292+
`Value(id, hide_item, Some loc.loc) :: read_pattern hide_item pat
279293
| Tpat_record(pats, _) ->
280294
List.concat (List.map (fun (_, _, pat) -> read_pattern hide_item pat) pats)
281295
#if OCAML_VERSION < (4,13,0)

src/loader/typedtree_traverse.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ module Analysis = struct
2525
in
2626
let () =
2727
match pat_desc with
28+
#if OCAML_VERSION >= (5, 2, 0)
29+
| Tpat_var (id, loc, _uid) -> (
30+
#else
2831
| Tpat_var (id, loc) -> (
32+
#endif
2933
match maybe_localvalue id loc.loc with
3034
| Some x -> poses := x :: !poses
3135
| None -> ())
36+
#if OCAML_VERSION >= (5, 2, 0)
37+
| Tpat_alias (_, id, loc, _uid) -> (
38+
#else
3239
| Tpat_alias (_, id, loc) -> (
40+
#endif
3341
match maybe_localvalue id loc.loc with
3442
| Some x -> poses := x :: !poses
3543
| None -> ())

src/model/compat.cppo.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,30 @@ type 'a shape_uid_map = 'a Shape.Uid.Map.t
235235
type uid_to_loc = Warnings.loc Types.Uid.Tbl.t
236236
let empty_map = Shape.Uid.Map.empty
237237

238+
#if OCAML_VERSION < (5,2,0)
238239
let shape_info_of_cmt_infos : Cmt_format.cmt_infos -> (shape * uid_to_loc) option =
239240
fun x -> Option.map (fun s -> (s, x.cmt_uid_to_loc)) x.cmt_impl_shape
241+
#else
242+
let loc_of_declaration =
243+
let open Typedtree in
244+
function
245+
| Value v -> v.val_loc
246+
| Value_binding vb -> vb.vb_pat.pat_loc
247+
| Type t -> t.typ_loc
248+
| Constructor c -> c.cd_loc
249+
| Extension_constructor e -> e.ext_loc
250+
| Label l -> l.ld_loc
251+
| Module m -> m.md_loc
252+
| Module_substitution ms -> ms.ms_loc
253+
| Module_binding mb -> mb.mb_loc
254+
| Module_type mt -> mt.mtd_loc
255+
| Class cd -> cd.ci_id_name.loc
256+
| Class_type ctd -> ctd.ci_id_name.loc
257+
258+
259+
let shape_info_of_cmt_infos : Cmt_format.cmt_infos -> (shape * uid_to_loc) option =
260+
fun x -> Option.map (fun s -> (s, Shape.Uid.Tbl.map x.cmt_uid_to_decl loc_of_declaration)) x.cmt_impl_shape
261+
#endif
240262

241263
#else
242264

src/xref2/shape_tools.cppo.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ let unit_of_uid uid =
109109

110110
let lookup_shape : Env.t -> Shape.t -> Identifier.SourceLocation.t option =
111111
fun env query ->
112+
#if OCAML_VERSION < (5,2,0)
112113
let module Reduce = Shape.Make_reduce (struct
113114
type env = unit
114115
let fuel = 10
@@ -124,6 +125,26 @@ let lookup_shape : Env.t -> Shape.t -> Identifier.SourceLocation.t option =
124125
let result = try Some (Reduce.reduce () query) with Not_found -> None in
125126
result >>= fun result ->
126127
result.uid >>= fun uid ->
128+
#else
129+
let module Reduce = Shape_reduce.Make(struct
130+
let fuel = 10
131+
let read_unit_shape ~unit_name =
132+
match Env.lookup_impl unit_name env with
133+
| Some impl -> (
134+
match impl.shape_info with
135+
| Some (shape, _) -> Some shape
136+
| None -> None)
137+
| _ -> None
138+
end) in
139+
let result = try Some (Reduce.reduce_for_uid Ocaml_env.empty query) with Not_found -> None in
140+
result >>= (function
141+
| Resolved uid -> Some uid
142+
| Resolved_alias (_::_ as l) ->
143+
let last = List.hd (List.rev l) in
144+
Some last
145+
| Approximated x -> x
146+
| _ -> None) >>= fun uid ->
147+
#endif
127148
unit_of_uid uid >>= fun unit_name ->
128149
match Env.lookup_impl unit_name env with
129150
| None -> None

test/xref2/lib/common.cppo.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ let cmt_of_string s =
3232
let env = Compmisc.initial_env () in
3333
let l = Lexing.from_string s in
3434
let p = Parse.implementation l in
35+
#if OCAML_VERSION < (5,2,0)
3536
Typemod.type_implementation "" "" "" env p
37+
#else
38+
Typemod.type_implementation (Unit_info.make ~source_file:"" "") env p
39+
#endif
3640

3741
let parent = Odoc_model.Paths.Identifier.Mk.page (None, Odoc_model.Names.PageName.make_std "None")
3842
let id = Odoc_model.Paths.Identifier.Mk.root (Some parent, Odoc_model.Names.ModuleName.make_std "Root")
@@ -623,7 +627,10 @@ let mkresolver () =
623627
Odoc_odoc.Resolver.create
624628
~important_digests:false
625629
~directories:(List.map Odoc_odoc.Fs.Directory.of_string
626-
#if OCAML_VERSION >= (4,8,0)
630+
#if OCAML_VERSION >= (5,2,0)
631+
(let paths = Load_path.get_paths () in
632+
List.filter (fun s -> s <> "") (paths.visible @ paths.hidden))
633+
#elif OCAML_VERSION >= (4,8,0)
627634
(Load_path.get_paths () |> List.filter (fun s -> s <> ""))
628635
#else
629636
!Config.load_path

0 commit comments

Comments
 (0)