Skip to content

Break out of loop in Printtyp.best_type_path when depth gets big #1961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions ocaml/typing/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,14 @@ let is_unambiguous path env =
List.for_all (fun p -> lid_of_path p = id) rem &&
Path.same p (fst (Env.find_type_by_name id env))

let rec get_best_path r =
let ambiguity_penalty path env =
if is_unambiguous path env then 0 else 10

let path_size path env =
let l, s = path_size path in
l + ambiguity_penalty path env, s

let rec get_best_path r env =
match !r with
Best p' -> p'
| Paths [] -> raise Not_found
Expand All @@ -855,11 +862,10 @@ let rec get_best_path r =
(fun p ->
(* Format.eprintf "evaluating %a@." path p; *)
match !r with
Best p' when path_size p >= path_size p' -> ()
| _ -> if is_unambiguous p !printing_env then r := Best p)
(* else Format.eprintf "%a ignored as ambiguous@." path p *)
Best p' when path_size p env >= path_size p' env -> ()
| _ -> r := Best p)
l;
get_best_path r
get_best_path r env

let best_type_path p =
if !printing_env == Env.empty
Expand All @@ -868,14 +874,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 () =
try
get_best_path (Path.Map.find p' !printing_map) !printing_env
with Not_found -> p'
in
while !printing_cont <> [] &&
try fst (path_size (get_path ())) > !printing_depth with Not_found -> true
fst (path_size (get_path ()) !printing_env) > !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