Skip to content

Commit 8612f54

Browse files
committed
Finish getting rid of type_sort_exn
1 parent 1d789d2 commit 8612f54

File tree

6 files changed

+111
-49
lines changed

6 files changed

+111
-49
lines changed

ocaml/testsuite/tests/typing-layouts-missing-cmi/function_arg.ml

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,75 @@ script = "rm -f function_a.cmi"
2020
let f0 (g : Function_b.fun_t) = g ~arg1:(assert false)
2121

2222
[%%expect{|
23-
blah
23+
Line 1, characters 40-54:
24+
1 | let f0 (g : Function_b.fun_t) = g ~arg1:(assert false)
25+
^^^^^^^^^^^^^^
26+
Error: Function arguments and returns must be representable.
27+
Function_a.t has an unknown layout, which might not be representable.
28+
No .cmi file found containing Function_a.t.
29+
Hint: Adding "function_a" to your dependencies might help.
2430
|}]
2531

2632
let f1 (g : Function_b.fun_t) = g ()
2733

2834
[%%expect{|
29-
blah
35+
Line 1, characters 34-36:
36+
1 | let f1 (g : Function_b.fun_t) = g ()
37+
^^
38+
Error: Function arguments and returns must be representable.
39+
Function_a.t has an unknown layout, which might not be representable.
40+
No .cmi file found containing Function_a.t.
41+
Hint: Adding "function_a" to your dependencies might help.
3042
|}]
3143

3244
let f2 : Function_b.fun_t = fun ~arg1:_ ~arg2 () -> arg2
3345

3446
[%%expect{|
35-
blah
47+
Line 1, characters 28-56:
48+
1 | let f2 : Function_b.fun_t = fun ~arg1:_ ~arg2 () -> arg2
49+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
Error: Function arguments and returns must be representable.
51+
Function_a.t has an unknown layout, which might not be representable.
52+
No .cmi file found containing Function_a.t.
53+
Hint: Adding "function_a" to your dependencies might help.
54+
|}]
55+
56+
let f3 : Function_b.return_t = fun () -> assert false
57+
58+
[%%expect{|
59+
Line 1, characters 31-53:
60+
1 | let f3 : Function_b.return_t = fun () -> assert false
61+
^^^^^^^^^^^^^^^^^^^^^^
62+
Error: Function arguments and returns must be representable.
63+
Function_a.t has an unknown layout, which might not be representable.
64+
No .cmi file found containing Function_a.t.
65+
Hint: Adding "function_a" to your dependencies might help.
66+
|}]
67+
68+
let f4 (_ : Function_b.take_t) = ()
69+
let x1 = f4 Function_b.f_opt
70+
71+
[%%expect{|
72+
val f4 : Function_b.take_t -> unit = <fun>
73+
Line 2, characters 12-28:
74+
2 | let x1 = f4 Function_b.f_opt
75+
^^^^^^^^^^^^^^^^
76+
Error: Function arguments and returns must be representable.
77+
Function_a.t has an unknown layout, which might not be representable.
78+
No .cmi file found containing Function_a.t.
79+
Hint: Adding "function_a" to your dependencies might help.
80+
|}]
81+
82+
let f5 (_ : Function_b.return_t) = ()
83+
let x2 = f5 Function_b.f_opt_2
84+
85+
[%%expect{|
86+
val f5 : Function_b.return_t -> unit = <fun>
87+
Line 2, characters 12-30:
88+
2 | let x2 = f5 Function_b.f_opt_2
89+
^^^^^^^^^^^^^^^^^^
90+
Error: Function arguments and returns must be representable.
91+
Function_a.t has an unknown layout, which might not be representable.
92+
No .cmi file found containing Function_a.t.
93+
Hint: Adding "function_a" to your dependencies might help.
3694
|}]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11

22
type fun_t = arg1:Function_a.t -> arg2:Function_a.t -> unit -> Function_a.t
33

4+
type take_t = Function_a.t -> unit
5+
type return_t = unit -> Function_a.t
6+
7+
let f_opt : ?opt:int -> Function_a.t -> unit = fun ?opt _ -> ()
8+
let f_opt_2 : ?opt:int -> unit -> Function_a.t = fun ?opt _ -> assert false
9+

ocaml/typing/ctype.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,11 +2075,6 @@ let type_sort ~why env ty =
20752075
| Ok _ -> Ok sort
20762076
| Error _ as e -> e
20772077

2078-
let type_sort_exn ~why env ty =
2079-
match type_sort ~why env ty with
2080-
| Ok s -> s
2081-
| Error _ -> Misc.fatal_error "Ctype.type_sort_exn on non-sort"
2082-
20832078
(* Note: Because [estimate_type_layout] actually returns an upper bound, this
20842079
function computes an inaccurate intersection in some cases.
20852080
@@ -3764,6 +3759,7 @@ type filter_arrow_failure =
37643759
; expected_type : type_expr
37653760
}
37663761
| Not_a_function
3762+
| Layout_error of type_expr * Layout.Violation.t
37673763

37683764
exception Filter_arrow_failed of filter_arrow_failure
37693765

@@ -3842,8 +3838,13 @@ let filter_arrow env t l ~force_tpoly =
38423838
entirely by storing sorts on [TArrow], but that seems incompatible
38433839
with the future plan to shift the layout requirements from the types
38443840
to the terms. *)
3845-
let arg_sort = type_sort_exn ~why:Function_argument env ty_arg in
3846-
let ret_sort = type_sort_exn ~why:Function_argument env ty_ret in
3841+
let type_sort ~why ty =
3842+
match type_sort ~why env ty with
3843+
| Ok sort -> sort
3844+
| Error err -> raise (Filter_arrow_failed (Layout_error (ty, err)))
3845+
in
3846+
let arg_sort = type_sort ~why:Function_argument ty_arg in
3847+
let ret_sort = type_sort ~why:Function_result ty_ret in
38473848
{ ty_arg; arg_mode; arg_sort; ty_ret; ret_mode; ret_sort }
38483849
else raise (Filter_arrow_failed
38493850
(Label_mismatch

ocaml/typing/ctype.mli

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ type filter_arrow_failure =
301301
; expected_type : type_expr
302302
}
303303
| Not_a_function
304+
| Layout_error of type_expr * Layout.Violation.t
304305

305306
exception Filter_arrow_failed of filter_arrow_failure
306307

@@ -494,12 +495,6 @@ val type_sort :
494495
why:Layouts.Layout.concrete_layout_reason ->
495496
Env.t -> type_expr -> (sort, Layout.Violation.t) result
496497

497-
(* Same as [type_sort], but only safe to call on types known to be a sort.
498-
For example, if the type is used as an argument in a function type that
499-
has already been translated. *)
500-
val type_sort_exn :
501-
why:Layouts.Layout.concrete_layout_reason -> Env.t -> type_expr -> sort
502-
503498
(* Layout checking. [constrain_type_layout] will update the layout of type
504499
variables to make the check true, if possible. [check_decl_layout] and
505500
[check_type_layout] won't, but will still instantiate sort variables.

ocaml/typing/typecore.ml

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,24 @@ type error =
191191
| Layout_not_enabled of Layout.const
192192
| Unboxed_int_literals_not_supported
193193
| Unboxed_float_literals_not_supported
194-
| Function_arg_not_rep of type_expr * Layout.Violation.t
194+
| Function_type_not_rep of type_expr * Layout.Violation.t
195195

196196
exception Error of Location.t * Env.t * error
197197
exception Error_forward of Location.error
198198

199+
let error_of_filter_arrow_failure ~explanation in_function ty_fun
200+
: filter_arrow_failure -> _ = function
201+
| Unification_error unif_err ->
202+
Expr_type_clash(unif_err, explanation, None)
203+
| Label_mismatch { got; expected; expected_type} ->
204+
Abstract_wrong_label { got; expected; expected_type; explanation }
205+
| Not_a_function -> begin
206+
match in_function with
207+
| Some _ -> Too_many_arguments(ty_fun, explanation)
208+
| None -> Not_a_function(ty_fun, explanation)
209+
end
210+
| Layout_error (ty, err) -> Function_type_not_rep (ty, err)
211+
199212
(* Forward declaration, to be filled in by Typemod.type_module *)
200213

201214
let type_module =
@@ -3119,7 +3132,7 @@ let collect_unknown_apply_args env funct ty_fun mode_fun rev_args sargs ret_tvar
31193132
match type_sort ~why:Function_argument env ty_arg with
31203133
| Ok sort -> sort
31213134
| Error err -> raise(Error(funct.exp_loc, env,
3122-
Function_arg_not_rep (ty_arg,err)))
3135+
Function_type_not_rep (ty_arg,err)))
31233136
in
31243137
(sort_arg, mode_arg, tpoly_get_mono ty_arg, mode_ret, ty_res)
31253138
| td ->
@@ -3164,7 +3177,7 @@ let collect_apply_args env funct ignore_labels ty_fun ty_fun0 mode_fun sargs ret
31643177
let sort_arg = match type_sort ~why:Function_argument env ty_arg with
31653178
| Ok sort -> sort
31663179
| Error err -> raise(Error(sarg1.pexp_loc, env,
3167-
Function_arg_not_rep(ty_arg, err)))
3180+
Function_type_not_rep(ty_arg, err)))
31683181
in
31693182
let name = label_name l
31703183
and optional = is_optional l in
@@ -3623,17 +3636,8 @@ let rec type_function_approx env loc label spato sexp in_function ty_expected =
36233636
let { ty_arg; arg_mode; ty_ret; _ } =
36243637
try filter_arrow env ty_expected label ~force_tpoly:(not has_poly)
36253638
with Filter_arrow_failed err ->
3626-
let explanation = None in
3627-
let err = match err with
3628-
| Unification_error unif_err ->
3629-
Expr_type_clash(unif_err, explanation, None)
3630-
| Label_mismatch { got; expected; expected_type} ->
3631-
Abstract_wrong_label { got; expected; expected_type; explanation }
3632-
| Not_a_function -> begin
3633-
match in_function with
3634-
| Some _ -> Too_many_arguments(ty_fun, explanation)
3635-
| None -> Not_a_function(ty_fun, explanation)
3636-
end
3639+
let err =
3640+
error_of_filter_arrow_failure ~explanation:None in_function ty_fun err
36373641
in
36383642
raise (Error(loc_fun, env, err))
36393643
in
@@ -5762,16 +5766,8 @@ and type_function ?in_function loc attrs env (expected_mode : expected_mode)
57625766
in
57635767
try filter_arrow env ty_expected' arg_label ~force_tpoly
57645768
with Filter_arrow_failed err ->
5765-
let err = match err with
5766-
| Unification_error unif_err ->
5767-
Expr_type_clash(unif_err, explanation, None)
5768-
| Label_mismatch { got; expected; expected_type} ->
5769-
Abstract_wrong_label { got; expected; expected_type; explanation }
5770-
| Not_a_function -> begin
5771-
match in_function with
5772-
| Some _ -> Too_many_arguments(ty_fun, explanation)
5773-
| None -> Not_a_function(ty_fun, explanation)
5774-
end
5769+
let err =
5770+
error_of_filter_arrow_failure ~explanation in_function ty_fun err
57755771
in
57765772
raise (Error(loc_fun, env, err))
57775773
in
@@ -6372,10 +6368,17 @@ and type_argument ?explanation ?recarg env (mode : expected_mode) sarg
63726368
let eta_mode = Value_mode.local_to_regional (Value_mode.of_alloc marg) in
63736369
let eta_pat, eta_var = var_pair ~mode:eta_mode "eta" ty_arg in
63746370
(* CR layouts v10: When we add abstract layouts, the eta expansion here
6375-
becomes impossible in some cases - we'll need good errors and test
6376-
cases instead of `type_sort_exn`. *)
6377-
let arg_sort = type_sort_exn env ~why:Function_argument ty_arg in
6378-
let ret_sort = type_sort_exn env ~why:Function_argument ty_res in
6371+
becomes impossible in some cases - we'll need better errors. For test
6372+
cases, look toward the end of
6373+
typing-layouts-missing-cmi/function_arg.ml *)
6374+
let type_sort ~why ty =
6375+
match type_sort ~why env ty with
6376+
| Ok sort -> sort
6377+
| Error err ->
6378+
raise(Error(sarg.pexp_loc, env, Function_type_not_rep (ty, err)))
6379+
in
6380+
let arg_sort = type_sort ~why:Function_argument ty_arg in
6381+
let ret_sort = type_sort ~why:Function_result ty_res in
63796382
let func texp =
63806383
let ret_mode = Value_mode.of_alloc mret in
63816384
let e =
@@ -8365,12 +8368,11 @@ let report_error ~loc env = function
83658368
| Unboxed_float_literals_not_supported ->
83668369
Location.errorf ~loc
83678370
"@[Unboxed float literals aren't supported yet.@]"
8368-
| Function_arg_not_rep (ty_arg,violation) ->
8371+
| Function_type_not_rep (ty,violation) ->
83698372
Location.errorf ~loc
8370-
"@[Function argument of type %a@ is not representable.@]@ %a"
8371-
Printtyp.type_expr ty_arg
8373+
"@[Function arguments and returns must be representable.@]@ %a"
83728374
(Layout.Violation.report_with_offender
8373-
~offender:(fun ppf -> Printtyp.type_expr ppf ty_arg)) violation
8375+
~offender:(fun ppf -> Printtyp.type_expr ppf ty)) violation
83748376
83758377
let report_error ~loc env err =
83768378
Printtyp.wrap_printing_env ~error:true env

ocaml/typing/typecore.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ type error =
268268
| Layout_not_enabled of Layout.const
269269
| Unboxed_int_literals_not_supported
270270
| Unboxed_float_literals_not_supported
271-
| Function_arg_not_rep of type_expr * Layout.Violation.t
271+
| Function_type_not_rep of type_expr * Layout.Violation.t
272272

273273
exception Error of Location.t * Env.t * error
274274
exception Error_forward of Location.error

0 commit comments

Comments
 (0)