Try expanding aliases in Ctype.nondep_type_rec (#10005)

master
Stephen Dolan 2020-11-17 11:10:59 +00:00 committed by GitHub
parent 6d1c87a49a
commit 20b7d8b2a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View File

@ -26,6 +26,9 @@ Working version
### Bug fixes:
- #10005: Try expanding aliases in Ctype.nondep_type_rec
(Stephen Dolan, review by Gabriel Scherer, Leo White and Xavier Leroy)
OCaml 4.12.0
------------

View File

@ -35,3 +35,12 @@ module M :
sig module type S = sig type t = float val foo : t X.t end end
module N : sig module type S = sig type t = float val foo : int end end
|}]
type 'a always_int = int
module F (X : sig type t end) = struct type s = X.t always_int end
module M = F (struct type t = T end)
[%%expect{|
type 'a always_int = int
module F : functor (X : sig type t end) -> sig type s = X.t always_int end
module M : sig type s = int end
|}]

View File

@ -4643,22 +4643,24 @@ let rec nondep_type_rec ?(expand_private=false) env ids ty =
ty'.desc <-
begin match ty.desc with
| Tconstr(p, tl, _abbrev) ->
begin match Path.find_free_opt ids p with
| Some id ->
begin try
Tlink (nondep_type_rec ~expand_private env ids
(expand_abbrev env (newty2 ty.level ty.desc)))
(*
The [Tlink] is important. The expanded type may be a
variable, or may not be completely copied yet
(recursive type), so one cannot just take its
description.
*)
with Cannot_expand | Unify _ ->
raise (Nondep_cannot_erase id)
end
| None ->
Tconstr(p, List.map (nondep_type_rec env ids) tl, ref Mnil)
begin try
(* First, try keeping the same type constructor p *)
match Path.find_free_opt ids p with
| Some id ->
raise (Nondep_cannot_erase id)
| None ->
Tconstr(p, List.map (nondep_type_rec env ids) tl, ref Mnil)
with (Nondep_cannot_erase _) as exn ->
(* If that doesn't work, try expanding abbrevs *)
try Tlink (nondep_type_rec ~expand_private env ids
(expand_abbrev env (newty2 ty.level ty.desc)))
(*
The [Tlink] is important. The expanded type may be a
variable, or may not be completely copied yet
(recursive type), so one cannot just take its
description.
*)
with Cannot_expand | Unify _ -> raise exn
end
| Tpackage(p, nl, tl) when Path.exists_free ids p ->
let p' = normalize_package_path env p in