Skip to content

Commit 0d7cc30

Browse files
flambda-backend: Move layout restriction from -> to fun (#2107)
* wip * change Wildcard and Unification_var to sort vars * test changes * more tests * fix test * test changes * 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 * rebase over type var print order change * Add a bunch of tests * check instead of constrain * jkind check in unify_univar * update test outputs * change unify_univar to take jkinds * Testing representability of function argument * More tests * Yet more tests * Accept test output * Propagate new test to _alpha * More tests, this time about inference * add tests to _alpha * small test changes * relax approx_type * reword history message * add cr for bad error message * fix test * rename layout to jkind * add comments about check_type_jkind_exn * add comment about unify_univar invariant --------- Co-authored-by: Richard Eisenberg <[email protected]>
1 parent 6c02a61 commit 0d7cc30

20 files changed

+1213
-228
lines changed

testsuite/tests/typing-layouts/annots.ml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,15 @@ val f : 'a -> 'a = <fun>
242242
let f : ('a : any). 'a -> 'a = fun x -> x
243243
;;
244244
[%%expect {|
245-
Line 1, characters 8-28:
245+
Line 1, characters 31-41:
246246
1 | let f : ('a : any). 'a -> 'a = fun x -> x
247-
^^^^^^^^^^^^^^^^^^^^
248-
Error: The universal type variable 'a was declared to have
249-
layout any, but was inferred to have a representable layout.
247+
^^^^^^^^^^
248+
Error: This definition has type 'b -> 'b which is less general than
249+
('a : any). 'a -> 'a
250+
'a has layout any, which is not representable.
250251
|}]
251-
(* CR layouts v2.5: This error message should change to complain
252-
about the [fun x], not the arrow type. *)
252+
(* CR layouts v2.9: This error message is not great. Check later if layout history
253+
is able to improve it. *)
253254

254255
let f : ('a : float64). 'a -> 'a = fun x -> x
255256
;;
@@ -401,14 +402,12 @@ val f : ('a : float64). 'a -> 'a = <fun>
401402
let f : type (a : any). a -> a = fun x -> x
402403
;;
403404
[%%expect {|
404-
Line 1, characters 24-30:
405+
Line 1, characters 33-43:
405406
1 | let f : type (a : any). a -> a = fun x -> x
406-
^^^^^^
407-
Error: The universal type variable 'a was declared to have
408-
layout any, but was inferred to have a representable layout.
407+
^^^^^^^^^^
408+
Error: Function arguments and returns must be representable.
409+
a has layout any, which is not representable.
409410
|}]
410-
(* CR layouts v2.5: This error message will change to complain
411-
about the fun x, not the arrow type. *)
412411

413412
(**************************************************)
414413
(* Test 7: Defaulting universal variable to value *)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
(* TEST
2+
flags = "-extension layouts"
3+
* native
4+
* bytecode
5+
*)
6+
7+
(* See also Test 10 in modules.ml, which tests for type-checking failures in
8+
code that is similar to this. *)
9+
10+
let unbox = Stdlib__Float_u.of_float
11+
12+
module type S = sig
13+
type t : any
14+
15+
val add : t -> t -> t
16+
val one : unit -> t
17+
val print : (unit -> t) -> unit
18+
end
19+
20+
(* type substitution *)
21+
module M1 : S with type t = float# = struct
22+
type t = float#
23+
24+
let add x y = Stdlib__Float_u.add x y
25+
let one () = unbox 1.
26+
let print f = Printf.printf "Printing a float#: %f\n" (Stdlib__Float_u.to_float (f ()))
27+
end
28+
29+
module M2 : S with type t = int = struct
30+
type t = int
31+
32+
let add x y = x + y
33+
let one () = 1
34+
let print f = Printf.printf "Printing a int: %d\n" (f ())
35+
end
36+
37+
let () = Printf.printf "%f\n" (Stdlib__Float_u.to_float (M1.add (unbox 10.) (unbox 10.)))
38+
let () = Printf.printf "%d\n" (M2.add 10 10)
39+
40+
(* destructive substitution *)
41+
module M3 : S with type t := float# = struct
42+
let add x y = Stdlib__Float_u.add x y
43+
let one () = unbox 1.
44+
let print f = Printf.printf "Printing a float#: %f\n" (Stdlib__Float_u.to_float (f ()))
45+
end
46+
47+
module M4 : S with type t := int = struct
48+
let add x y = x + y
49+
let one () = 1
50+
let print f = Printf.printf "Printing a int: %d\n" (f ())
51+
end
52+
53+
let () = Printf.printf "%f\n" (Stdlib__Float_u.to_float (M3.add (unbox 10.) (unbox 10.)))
54+
let () = Printf.printf "%d\n" (M4.add 10 10)
55+
56+
(* functor *)
57+
module type Q = sig
58+
include S
59+
60+
val print_one : unit -> unit
61+
end
62+
63+
module Make (M : S) : Q = struct
64+
include M
65+
66+
let print_one () = M.print M.one
67+
end
68+
69+
module M1' = Make (M1)
70+
module M2' = Make (M2)
71+
72+
let () = M1'.print_one ()
73+
let () = M2'.print_one ()
74+
75+
(* edge cases and order of evaluation *)
76+
let g : type (a : any). unit -> a -> a = fun () -> assert false
77+
78+
let () =
79+
try
80+
let r =
81+
g
82+
()
83+
(Printf.printf "a\n";
84+
unbox 10.)
85+
in
86+
Printf.printf "%f\n" (Stdlib__Float_u.to_float r)
87+
with
88+
| _ -> Printf.printf "b\n"
89+
;;
90+
91+
let () =
92+
try
93+
let r =
94+
g
95+
()
96+
(Printf.printf "c\n";
97+
10)
98+
in
99+
Printf.printf "%d\n" r
100+
with
101+
| _ -> Printf.printf "d\n"
102+
;;
103+
104+
(* This should type check *)
105+
let rec f : type (a : any). unit -> a -> a = fun () -> f ()
106+
107+
let f' () =
108+
let _ = f () 10 in
109+
f () (unbox 10.)
110+
;;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
20.000000
2+
20
3+
20.000000
4+
20
5+
Printing a float#: 1.000000
6+
Printing a int: 1
7+
a
8+
b
9+
c
10+
d

0 commit comments

Comments
 (0)