Skip to content

Commit 06b27e8

Browse files
authored
flambda-backend: Look through Tpoly in unbox_once (#2002)
1 parent 373d067 commit 06b27e8

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

testsuite/tests/typing-layouts/datatypes.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ Error: Layout immediate is more experimental than allowed by -extension layouts.
8686
(* Test 8: Type parameters in the presence of recursive concrete usage *)
8787

8888
(* CR layouts: copy test from datatypes_alpha with float64 when available *)
89+
90+
(*****************************************************************************)
91+
(* Test 9: Looking through polytypes in mutually recursive type declarations *)
92+
93+
(* CR layouts: copy test from datatypes_beta float64 is available. *)

testsuite/tests/typing-layouts/datatypes_alpha.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,8 @@ and 'a t8_6 = 'a void_t;;
334334
type ('a : void) t8_5 = { x : 'a t8_6; y : string; }
335335
and ('a : void) t8_6 = 'a void_t
336336
|}]
337+
338+
(*****************************************************************************)
339+
(* Test 9: Looking through polytypes in mutually recursive type declarations *)
340+
341+
(* Doesn't need layouts_alpha. *)

testsuite/tests/typing-layouts/datatypes_beta.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,25 @@ Error: This expression has type float but an expression was expected of type
289289
(* CR layouts v5: copy test from datatypes_alpha when non-values can go in
290290
general datatype declarations. *)
291291

292+
(*****************************************************************************)
293+
(* Test 9: Looking through polytypes in mutually recursive type declarations *)
294+
295+
type 'a t9_1 = unit
296+
and t9_2 = { x : string t9_1 }
297+
and t9_3 = { x : 'a. 'a t9_1 }
298+
299+
[%%expect {|
300+
type 'a t9_1 = unit
301+
and t9_2 = { x : string t9_1; }
302+
and t9_3 = { x : 'a. 'a t9_1; }
303+
|}]
304+
305+
type 'a floaty = float#
306+
and t9_4 = { x : float#; y : string floaty }
307+
and t9_5 = { x : float#; y : 'a. 'a floaty }
308+
309+
[%%expect {|
310+
type 'a floaty = float#
311+
and t9_4 = { x : float#; y : string floaty; }
312+
and t9_5 = { x : float#; y : 'a. 'a floaty; }
313+
|}]

typing/ctype.ml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,11 @@ let expand_head_opt env ty =
19071907

19081908

19091909
type unbox_result =
1910-
| Unboxed of type_expr
1911-
| Not_unboxed of type_expr
1910+
(* unboxing process made a step: either an unboxing or removal of a [Tpoly] *)
1911+
| Stepped of type_expr
1912+
(* no step to make; we're all done here *)
1913+
| Final_result of type_expr
1914+
(* definition not in environment: missing cmi *)
19121915
| Missing of Path.t
19131916

19141917
(* We use expand_head_opt version of expand_head to get access
@@ -1921,23 +1924,24 @@ let unbox_once env ty =
19211924
| exception Not_found -> Missing p
19221925
| decl ->
19231926
begin match find_unboxed_type decl with
1924-
| None -> Not_unboxed ty
1927+
| None -> Final_result ty
19251928
| Some ty2 ->
19261929
let ty2 = match get_desc ty2 with Tpoly (t, _) -> t | _ -> ty2 in
1927-
Unboxed (apply env decl.type_params ty2 args)
1930+
Stepped (apply env decl.type_params ty2 args)
19281931
end
19291932
end
1930-
| _ -> Not_unboxed ty
1933+
| Tpoly (ty, _) -> Stepped ty
1934+
| _ -> Final_result ty
19311935

19321936
(* We use ty_prev to track the last type for which we found a definition,
19331937
allowing us to return a type for which a definition was found even if
19341938
we eventually bottom out at a missing cmi file, or otherwise. *)
19351939
let rec get_unboxed_type_representation env ty_prev ty fuel =
19361940
if fuel < 0 then Error ty else
19371941
match unbox_once env ty with
1938-
| Unboxed ty2 ->
1942+
| Stepped ty2 ->
19391943
get_unboxed_type_representation env ty ty2 (fuel - 1)
1940-
| Not_unboxed ty2 -> Ok ty2
1944+
| Final_result ty2 -> Ok ty2
19411945
| Missing _ -> Ok ty_prev
19421946

19431947
let get_unboxed_type_representation env ty =
@@ -2053,8 +2057,8 @@ let rec constrain_type_jkind ~fixed env ty jkind fuel =
20532057
| Error _ as err when fuel < 0 -> err
20542058
| Error violation ->
20552059
begin match unbox_once env ty with
2056-
| Not_unboxed ty -> constrain_unboxed ty
2057-
| Unboxed ty ->
2060+
| Final_result ty -> constrain_unboxed ty
2061+
| Stepped ty ->
20582062
constrain_type_jkind ~fixed env ty jkind (fuel - 1)
20592063
| Missing missing_cmi_for ->
20602064
Error (Jkind.Violation.record_missing_cmi ~missing_cmi_for violation)

0 commit comments

Comments
 (0)