Skip to content

Commit

Permalink
Break out of loop in Printtyp.best_type_path when depth gets big
Browse files Browse the repository at this point in the history
The code in `Printtyp.best_type_path` assumes that if the path is not in the
printing env, it must be the case that adding more to the printing depth will
eventually succeed in putting it there. However, if something has gone wrong,
the path might just be missing from the printing env altogether, in which case
the loop continues searching to arbitrary depths. If _two_ things have gone
wrong, there might be a self-reference lurking in the environment, meaning the
search can go to arbitrary depth without running out of paths to traverse.

We can cut off both scenarios by breaking out of the loop as soon as the
printing depth is as big as the best candidate so far, since from that point
increasing the depth cannot help.

This change also returns the original path in cases where the normalised one is
longer. This may cut down on cases where paths with double underscores are seen
in error messages.
  • Loading branch information
lukemaurer committed Oct 24, 2023
1 parent 6c8abc6 commit e555f0e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ocaml/typing/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,18 @@ let best_type_path p =
then (p, Id)
else
let (p', s) = normalize_type_path !printing_env p in
let get_path () = get_best_path (Path.Map.find p' !printing_map) in
let get_path () =
match Path.Map.find p' !printing_map with
| path -> get_best_path path
| exception Not_found -> if path_size p' < path_size p then p' else p
in
while !printing_cont <> [] &&
try fst (path_size (get_path ())) > !printing_depth with Not_found -> true
fst (path_size (get_path ())) > !printing_depth
do
printing_cont := List.map snd (Env.run_iter_cont !printing_cont);
incr printing_depth;
done;
let p'' = try get_path () with Not_found -> p' in
let p'' = get_path () in
(* Format.eprintf "%a = %a -> %a@." path p path p' path p''; *)
(p'', s)

Expand Down

0 comments on commit e555f0e

Please sign in to comment.