Skip to content

Commit 0049c4e

Browse files
authored
Rework jkind default (#2158)
* extend policy to control ty var jkind init * annotation on type vars should be upper bounds * proper jkind reason * sort init rhs of constraints for backward compatibility * add comment&rename to jkind_initialization_choice * refactor policy * comment about transl_simple_type_delayed * use Any more in typeclass * add new_var_jkind param and thread it through * use Any for with constraint * add note * more tests * more tests
1 parent de94147 commit 0049c4e

File tree

11 files changed

+246
-89
lines changed

11 files changed

+246
-89
lines changed

ocaml/testsuite/tests/typing-layouts/basics.ml

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,103 @@ module type S1 = sig val f : t_any -> int end
4949
module type S1 = sig
5050
type t : any
5151

52-
type ('a : any) s = ('a : any) -> int constraint ('a : any) = t
52+
type ('a : any) s = 'a -> int constraint 'a = t
53+
54+
type q = t s
55+
end;;
56+
[%%expect{|
57+
module type S1 =
58+
sig type t : any type 'a s = 'a -> int constraint 'a = t type q = t s end
59+
|}]
60+
61+
module type S1 = sig
62+
type t : any
63+
64+
type ('a : any) s = int -> 'a constraint 'a = t
65+
66+
type q = t s
67+
end;;
68+
[%%expect{|
69+
module type S1 =
70+
sig type t : any type 'a s = int -> 'a constraint 'a = t type q = t s end
71+
|}]
72+
73+
module type S1 = sig
74+
type t : any
75+
76+
type ('a : any) s = { a: 'a -> 'a }
77+
78+
type q = t s
79+
end;;
80+
[%%expect{|
81+
module type S1 =
82+
sig type t : any type ('a : any) s = { a : 'a -> 'a; } type q = t s end
83+
|}]
84+
85+
module type S1 = sig
86+
type t : any
87+
88+
type ('a : any) s = A of ('a -> 'a)
89+
90+
type q = t s
5391
end;;
5492
[%%expect{|
55-
module type S1 = sig type t : any type 'a s = 'a -> int constraint 'a = t end
93+
module type S1 =
94+
sig type t : any type ('a : any) s = A of ('a -> 'a) type q = t s end
5695
|}]
5796

5897
module type S1 = sig
5998
type t : any
6099

61-
type ('a : any) s = int -> ('a : any) constraint ('a : any) = t
100+
type ('a : any) s = A of { a: 'a -> 'a }
101+
102+
type q = t s
62103
end;;
63104
[%%expect{|
64-
module type S1 = sig type t : any type 'a s = int -> 'a constraint 'a = t end
105+
module type S1 =
106+
sig
107+
type t : any
108+
type ('a : any) s = A of { a : 'a -> 'a; }
109+
type q = t s
110+
end
111+
|}]
112+
113+
module S1 = struct
114+
type t : any
115+
116+
type ('a : any) s = A : { a: 'a -> 'b -> 'a } -> 'a s
117+
118+
type q = t s
119+
120+
let f () = A {a = (fun x y -> x)}
121+
end;;
122+
[%%expect{|
123+
module S1 :
124+
sig
125+
type t : any
126+
type ('a : any) s = A : { a : 'a -> 'b -> 'a; } -> 'a s
127+
type q = t s
128+
val f : unit -> 'a s
129+
end
130+
|}]
131+
132+
module S1 = struct
133+
type t : any
134+
135+
type ('a : any) s = A : ('a -> 'b -> 'a) -> 'a s
136+
137+
type q = t s
138+
139+
let f () = A (fun x y -> x)
140+
end
141+
[%%expect{|
142+
module S1 :
143+
sig
144+
type t : any
145+
type ('a : any) s = A : ('a -> 'b -> 'a) -> 'a s
146+
type q = t s
147+
val f : unit -> 'a s
148+
end
65149
|}]
66150

67151
module type S1 = sig
@@ -289,7 +373,7 @@ Error:
289373
|}]
290374
(* CR layouts v2.9: improve error, which requires layout histories *)
291375

292-
type ('a : any) t4 = ('a : any)
376+
type ('a : any) t4 = 'a
293377
and s4 = string t4;;
294378
[%%expect{|
295379
type ('a : any) t4 = 'a
@@ -1100,7 +1184,7 @@ val f : ('a : float64). unit -> 'a t22f t22f = <fun>
11001184

11011185
(* CR layouts v5: bring void version here from layouts_alpha *)
11021186

1103-
type (_ : any, _ : any) eq = Refl : ('a : any). ('a, 'a) eq
1187+
type (_ : any, _ : any) eq = Refl : ('a : any). ('a, 'a) eq
11041188

11051189
module Mf : sig
11061190
type t_float64 : float64
@@ -1207,7 +1291,7 @@ let q () =
12071291
()
12081292

12091293
[%%expect{|
1210-
val ( let* ) : 'a -> (t_float64 -> 'b) -> unit = <fun>
1294+
val ( let* ) : ('b : any) 'a. 'a -> (t_float64 -> 'b) -> unit = <fun>
12111295
val q : unit -> unit = <fun>
12121296
|}]
12131297

@@ -1219,7 +1303,7 @@ let q () =
12191303
assert false
12201304

12211305
[%%expect{|
1222-
val ( let* ) : 'a -> ('b -> t_float64) -> unit = <fun>
1306+
val ( let* ) : ('b : any) 'a. 'a -> ('b -> t_float64) -> unit = <fun>
12231307
val q : unit -> unit = <fun>
12241308
|}]
12251309

ocaml/testsuite/tests/typing-layouts/basics_alpha.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module type S1 = sig val f : t_any -> int end
4545
module type S1 = sig
4646
type t : any
4747

48-
type ('a : any) s = ('a : any) -> int constraint ('a : any) = t
48+
type ('a : any) s = 'a -> int constraint 'a = t
4949
end;;
5050
[%%expect{|
5151
module type S1 = sig type t : any type 'a s = 'a -> int constraint 'a = t end
@@ -54,7 +54,7 @@ module type S1 = sig type t : any type 'a s = 'a -> int constraint 'a = t end
5454
module type S1 = sig
5555
type t : any
5656

57-
type ('a : any) s = int -> ('a : any) constraint ('a : any) = t
57+
type ('a : any) s = int -> 'a constraint 'a = t
5858
end;;
5959
[%%expect{|
6060
module type S1 = sig type t : any type 'a s = int -> 'a constraint 'a = t end
@@ -301,7 +301,7 @@ Error:
301301
|}]
302302
(* CR layouts v2.9: improve error, which will require jkind histories *)
303303

304-
type ('a : any) t4 = ('a : any)
304+
type ('a : any) t4 = 'a
305305
and s4 = string t4;;
306306
[%%expect{|
307307
type ('a : any) t4 = 'a
@@ -1321,7 +1321,7 @@ let q () =
13211321
()
13221322

13231323
[%%expect{|
1324-
val ( let* ) : 'a -> (t_float64 -> 'b) -> unit = <fun>
1324+
val ( let* ) : ('b : any) 'a. 'a -> (t_float64 -> 'b) -> unit = <fun>
13251325
val q : unit -> unit = <fun>
13261326
|}]
13271327

@@ -1347,7 +1347,7 @@ let q () =
13471347
assert false
13481348

13491349
[%%expect{|
1350-
val ( let* ) : 'a -> ('b -> t_float64) -> unit = <fun>
1350+
val ( let* ) : ('b : any) 'a. 'a -> ('b -> t_float64) -> unit = <fun>
13511351
val q : unit -> unit = <fun>
13521352
|}]
13531353

ocaml/testsuite/tests/typing-local/local.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ val catch : (unit -> local_ string) -> string * string = <fun>
10231023
(* same, but this time the function is allowed to return its argument *)
10241024
let use_locally (f : local_ 'a -> local_ 'a) : local_ 'a -> local_ 'a = f
10251025
[%%expect{|
1026-
val use_locally : (local_ 'a -> local_ 'a) -> local_ 'a -> local_ 'a = <fun>
1026+
val use_locally :
1027+
('a : any). (local_ 'a -> local_ 'a) -> local_ 'a -> local_ 'a = <fun>
10271028
|}]
10281029

10291030
let loc = ((fun x -> local_ x) : local_ int -> local_ int)

ocaml/testsuite/tests/typing-poly/poly.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,10 @@ external reraise : exn -> 'a = "%reraise"
17951795
module M :
17961796
functor () ->
17971797
sig
1798-
val f : 'a -> 'a
1799-
val g : 'a -> 'a
1800-
val h : 'a -> 'a
1801-
val i : 'a -> 'a
1798+
val f : ('a : any). 'a -> 'a
1799+
val g : ('a : any). 'a -> 'a
1800+
val h : ('a : any). 'a -> 'a
1801+
val i : ('a : any). 'a -> 'a
18021802
end
18031803
|}]
18041804

ocaml/typing/jkind.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ type any_creation_reason =
629629
| Dummy_jkind
630630
| Type_expression_call
631631
| Inside_of_Tarrow
632+
| Wildcard
633+
| Unification_var
632634

633635
type float64_creation_reason = Primitive of Ident.t
634636

@@ -988,6 +990,8 @@ end = struct
988990
| Type_expression_call ->
989991
fprintf ppf "a call to [type_expression] via the ocaml API"
990992
| Inside_of_Tarrow -> fprintf ppf "argument or result of a Tarrow"
993+
| Wildcard -> fprintf ppf "a _ in a type"
994+
| Unification_var -> fprintf ppf "a fresh unification variable"
991995

992996
let format_immediate_creation_reason ppf : immediate_creation_reason -> _ =
993997
function
@@ -1359,6 +1363,8 @@ module Debug_printers = struct
13591363
| Dummy_jkind -> fprintf ppf "Dummy_jkind"
13601364
| Type_expression_call -> fprintf ppf "Type_expression_call"
13611365
| Inside_of_Tarrow -> fprintf ppf "Inside_of_Tarrow"
1366+
| Wildcard -> fprintf ppf "Wildcard"
1367+
| Unification_var -> fprintf ppf "Unification_var"
13621368

13631369
let immediate_creation_reason ppf : immediate_creation_reason -> _ = function
13641370
| Empty_record -> fprintf ppf "Empty_record"

ocaml/typing/jkind.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ type any_creation_reason =
256256
unified to correct levels *)
257257
| Type_expression_call
258258
| Inside_of_Tarrow
259+
| Wildcard
260+
| Unification_var
259261

260262
type float64_creation_reason = Primitive of Ident.t
261263

ocaml/typing/typeclass.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ let unify_delayed_method_type loc env label ty expected_ty=
261261
raise(Error(loc, env, Field_type_mismatch ("method", label, trace)))
262262

263263
let type_constraint val_env sty sty' loc =
264-
let cty = transl_simple_type val_env ~closed:false Alloc.Const.legacy sty in
264+
let cty = transl_simple_type ~new_var_jkind:Any val_env ~closed:false Alloc.Const.legacy sty in
265265
let ty = cty.ctyp_type in
266-
let cty' = transl_simple_type val_env ~closed:false Alloc.Const.legacy sty' in
266+
let cty' = transl_simple_type ~new_var_jkind:Sort val_env ~closed:false Alloc.Const.legacy sty' in
267267
let ty' = cty'.ctyp_type in
268268
begin
269269
try Ctype.unify val_env ty ty' with Ctype.Unify err ->
@@ -308,7 +308,7 @@ let rec class_type_field env sign self_scope ctf =
308308
| Pctf_val ({txt=lab}, mut, virt, sty) ->
309309
mkctf_with_attrs
310310
(fun () ->
311-
let cty = transl_simple_type env ~closed:false Alloc.Const.legacy sty in
311+
let cty = transl_simple_type ~new_var_jkind:Sort env ~closed:false Alloc.Const.legacy sty in
312312
let ty = cty.ctyp_type in
313313
begin match
314314
Ctype.constrain_type_jkind
@@ -343,7 +343,7 @@ let rec class_type_field env sign self_scope ctf =
343343
) :: !delayed_meth_specs;
344344
Tctf_method (lab, priv, virt, returned_cty)
345345
| _ ->
346-
let cty = transl_simple_type env ~closed:false Alloc.Const.legacy sty in
346+
let cty = transl_simple_type ~new_var_jkind:Any env ~closed:false Alloc.Const.legacy sty in
347347
let ty = cty.ctyp_type in
348348
add_method loc env lab priv virt ty sign;
349349
Tctf_method (lab, priv, virt, cty))
@@ -367,7 +367,7 @@ and class_signature virt env pcsig self_scope loc =
367367
(* Introduce a dummy method preventing self type from being closed. *)
368368
Ctype.add_dummy_method env ~scope:self_scope sign;
369369

370-
let self_cty = transl_simple_type env ~closed:false Alloc.Const.legacy sty in
370+
let self_cty = transl_simple_type ~new_var_jkind:Any env ~closed:false Alloc.Const.legacy sty in
371371
let self_type = self_cty.ctyp_type in
372372
begin try
373373
Ctype.unify env self_type sign.csig_self
@@ -417,7 +417,7 @@ and class_type_aux env virt self_scope scty =
417417
List.length styl)));
418418
let ctys = List.map2
419419
(fun sty ty ->
420-
let cty' = transl_simple_type env ~closed:false Alloc.Const.legacy sty in
420+
let cty' = transl_simple_type ~new_var_jkind:Any env ~closed:false Alloc.Const.legacy sty in
421421
let ty' = cty'.ctyp_type in
422422
begin
423423
try Ctype.unify env ty' ty with Ctype.Unify err ->
@@ -437,7 +437,7 @@ and class_type_aux env virt self_scope scty =
437437
cltyp (Tcty_signature clsig) typ
438438

439439
| Pcty_arrow (l, sty, scty) ->
440-
let cty = transl_simple_type env ~closed:false Alloc.Const.legacy sty in
440+
let cty = transl_simple_type ~new_var_jkind:Any env ~closed:false Alloc.Const.legacy sty in
441441
let ty = cty.ctyp_type in
442442
let ty =
443443
if Btype.is_optional l
@@ -670,7 +670,7 @@ let rec class_field_first_pass self_loc cl_num sign self_scope acc cf =
670670
(fun () ->
671671
let cty =
672672
Ctype.with_local_level_if_principal
673-
(fun () -> Typetexp.transl_simple_type val_env
673+
(fun () -> Typetexp.transl_simple_type ~new_var_jkind:Any val_env
674674
~closed:false Alloc.Const.legacy styp)
675675
~post:(fun cty -> Ctype.generalize_structure cty.ctyp_type)
676676
in
@@ -760,7 +760,7 @@ let rec class_field_first_pass self_loc cl_num sign self_scope acc cf =
760760
with_attrs
761761
(fun () ->
762762
let sty = Ast_helper.Typ.force_poly sty in
763-
let cty = transl_simple_type val_env ~closed:false Alloc.Const.legacy sty in
763+
let cty = transl_simple_type ~new_var_jkind:Any val_env ~closed:false Alloc.Const.legacy sty in
764764
let ty = cty.ctyp_type in
765765
add_method loc val_env label.txt priv Virtual ty sign;
766766
let field =
@@ -800,7 +800,7 @@ let rec class_field_first_pass self_loc cl_num sign self_scope acc cf =
800800
| Some sty ->
801801
let sty = Ast_helper.Typ.force_poly sty in
802802
let cty' =
803-
Typetexp.transl_simple_type val_env ~closed:false Alloc.Const.legacy sty
803+
Typetexp.transl_simple_type ~new_var_jkind:Any val_env ~closed:false Alloc.Const.legacy sty
804804
in
805805
cty'.ctyp_type
806806
in
@@ -1118,7 +1118,7 @@ and class_expr_aux cl_num val_env met_env virt self_scope scl =
11181118
if Path.same decl.cty_path unbound_class then
11191119
raise(Error(scl.pcl_loc, val_env, Unbound_class_2 lid.txt));
11201120
let tyl = List.map
1121-
(fun sty -> transl_simple_type val_env ~closed:false Alloc.Const.legacy sty)
1121+
(fun sty -> transl_simple_type ~new_var_jkind:Any val_env ~closed:false Alloc.Const.legacy sty)
11221122
styl
11231123
in
11241124
let (params, clty) =

ocaml/typing/typecore.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4006,7 +4006,7 @@ let rec approx_type env sty =
40064006
(* Polymorphic types will only unify with types that match all of their
40074007
polymorphic parts, so we need to fully translate the type here
40084008
unlike in the monomorphic case *)
4009-
Typetexp.transl_simple_type env ~closed:false arg_mode arg_sty
4009+
Typetexp.transl_simple_type ~new_var_jkind:Any env ~closed:false arg_mode arg_sty
40104010
in
40114011
let ret = approx_type env sty in
40124012
let marg = Alloc.of_const arg_mode in
@@ -4061,7 +4061,7 @@ let type_pattern_approx env spat ty_expected =
40614061
mode_annots_or_default mode_annots ~default:Alloc.Const.legacy
40624062
in
40634063
let ty_pat =
4064-
Typetexp.transl_simple_type env ~closed:false arg_type_mode sty
4064+
Typetexp.transl_simple_type ~new_var_jkind:Any env ~closed:false arg_type_mode sty
40654065
in
40664066
begin try unify env ty_pat.ctyp_type ty_expected with Unify trace ->
40674067
raise(Error(spat.ppat_loc, env, Pattern_type_clash(trace, None)))
@@ -5678,7 +5678,7 @@ and type_expect_
56785678
let type_mode =
56795679
mode_annots_or_default mode_annots ~default:Alloc.Const.legacy
56805680
in
5681-
Typetexp.transl_simple_type env ~closed:false type_mode sty
5681+
Typetexp.transl_simple_type ~new_var_jkind:Any env ~closed:false type_mode sty
56825682
end
56835683
~post:(fun cty -> generalize_structure cty.ctyp_type)
56845684
in
@@ -6013,7 +6013,7 @@ and type_expect_
60136013
| Some sty ->
60146014
let sty = Ast_helper.Typ.force_poly sty in
60156015
let cty =
6016-
Typetexp.transl_simple_type env ~closed:false
6016+
Typetexp.transl_simple_type ~new_var_jkind:Any env ~closed:false
60176017
Alloc.Const.legacy sty
60186018
in
60196019
cty.ctyp_type, Some cty

0 commit comments

Comments
 (0)