Skip to content

Commit d051dee

Browse files
authored
Add debug printer for typing env (#124)
Signed-off-by: Nathan Rebours <nathan.p.rebours@gmail.com>
1 parent b3ccaee commit d051dee

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/typing_env.ml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,83 @@ let for_diff ~reference ~current =
4545
let env = Env.in_signature true env in
4646
let env = Env.add_signature reference env in
4747
Env.add_signature modified_current env
48+
49+
let pp fmt t =
50+
let summary = Env.summary t in
51+
Format.fprintf fmt "@[<hv 2>[@;";
52+
let pp_in_box kind id f =
53+
Format.fprintf fmt "%s %s:@[<hov 2>@;" kind (Ident.unique_toplevel_name id);
54+
f ();
55+
Format.fprintf fmt "@]@;"
56+
in
57+
let rec pp_rec s =
58+
match (s : Env.summary) with
59+
| Env_empty -> ()
60+
| Env_value (s, id, vd) ->
61+
pp_in_box "value" id (fun () ->
62+
Format.fprintf fmt "%a" Printtyp.(value_description id) vd);
63+
pp_rec s
64+
| Env_type (s, id, td) ->
65+
pp_in_box "type" id (fun () ->
66+
Format.fprintf fmt "%a" Printtyp.(type_declaration id) td);
67+
pp_rec s
68+
| Env_extension (s, id, ec) ->
69+
pp_in_box "extension" id (fun () ->
70+
Format.fprintf fmt "%a" Printtyp.(extension_constructor id) ec);
71+
pp_rec s
72+
| Env_module (s, id, mp, { md_type; _ }) ->
73+
pp_in_box "module" id (fun () ->
74+
Format.fprintf fmt "%s@;"
75+
(match mp with
76+
| Mp_present -> "Mp_present"
77+
| Mp_absent -> "Mp_absent");
78+
Format.fprintf fmt "%a" Printtyp.modtype md_type);
79+
pp_rec s
80+
| Env_modtype (s, id, mtyp) ->
81+
pp_in_box "module type" id (fun () ->
82+
Format.fprintf fmt "%a" (Printtyp.modtype_declaration id) mtyp);
83+
pp_rec s
84+
| Env_class (s, id, cd) ->
85+
pp_in_box "class" id (fun () ->
86+
Format.fprintf fmt "%a" (Printtyp.class_declaration id) cd);
87+
pp_rec s
88+
| Env_cltype (s, id, ctd) ->
89+
pp_in_box "class type" id (fun () ->
90+
Format.fprintf fmt "%a" (Printtyp.cltype_declaration id) ctd);
91+
pp_rec s
92+
| Env_open (s, path) ->
93+
Format.fprintf fmt "open %a@;" Printtyp.path path;
94+
pp_rec s
95+
| Env_functor_arg (s, id) ->
96+
Format.fprintf fmt "functor arg %s@;" (Ident.unique_toplevel_name id);
97+
pp_rec s
98+
| Env_constraints (s, td_map) ->
99+
Format.fprintf fmt "constraints@[<hv 2>@;";
100+
Path.Map.iter
101+
(fun path td ->
102+
Format.fprintf fmt "%a@[<hov 2>@;" Printtyp.path path;
103+
Printtyp.type_declaration (Path.head path) fmt td;
104+
Format.fprintf fmt "@]@;")
105+
td_map;
106+
Format.fprintf fmt "@]@;";
107+
pp_rec s
108+
| Env_copy_types s ->
109+
Format.fprintf fmt "copy_types@;";
110+
pp_rec s
111+
| Env_persistent (s, id) ->
112+
Format.fprintf fmt "persistent %s@;" (Ident.unique_toplevel_name id);
113+
pp_rec s
114+
| Env_value_unbound (s, name, vu_reason) ->
115+
Format.fprintf fmt "value unbound %s: %s@;" name
116+
(match vu_reason with
117+
| Val_unbound_instance_variable -> "instance variable"
118+
| Val_unbound_self -> "self"
119+
| Val_unbound_ancestor -> "ancestor"
120+
| Val_unbound_ghost_recursive _ -> "ghost recursive");
121+
pp_rec s
122+
| Env_module_unbound (s, name, Mod_unbound_illegal_recursion) ->
123+
Format.fprintf fmt "module unbound %s: illegal recursion@;" name;
124+
pp_rec s
125+
in
126+
pp_rec summary;
127+
Format.fprintf fmt "@]@;]@;"

lib/typing_env.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ val for_diff : reference:signature -> current:signature -> Env.t
1818
It can be the case that the types are structuraly incompatible because they
1919
were modified between the two versions but the compiler will accept those.
2020
*)
21+
22+
val pp : Format.formatter -> Env.t -> unit
23+
(** Use for debugging *)

0 commit comments

Comments
 (0)