Skip to content

Commit 30ce67d

Browse files
authored
Improve inclusion error messages for [@local_opt] (#10)
1 parent f925a62 commit 30ce67d

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

testsuite/tests/typing-local/local.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,35 @@ Error: Signature mismatch:
16711671
is not included in
16721672
val add : local_ int32 -> local_ int32 -> int32
16731673
|}]
1674+
module Opt32 : sig external add : (int32[@local_opt]) -> (int32[@local_opt]) -> (int32[@local_opt]) = "%int32_add" end = Int32
1675+
module Bad32_2 : sig val add : local_ int32 -> local_ int32 -> int32 end =
1676+
Opt32
1677+
[%%expect{|
1678+
module Opt32 :
1679+
sig
1680+
external add :
1681+
(int32 [@local_opt]) -> (int32 [@local_opt]) -> (int32 [@local_opt])
1682+
= "%int32_add"
1683+
end
1684+
Line 3, characters 2-7:
1685+
3 | Opt32
1686+
^^^^^
1687+
Error: Signature mismatch:
1688+
Modules do not match:
1689+
sig
1690+
external add :
1691+
(int32 [@local_opt]) ->
1692+
(int32 [@local_opt]) -> (int32 [@local_opt]) = "%int32_add"
1693+
end
1694+
is not included in
1695+
sig val add : local_ int32 -> local_ int32 -> int32 end
1696+
Values do not match:
1697+
external add :
1698+
(int32 [@local_opt]) ->
1699+
(int32 [@local_opt]) -> (int32 [@local_opt]) = "%int32_add"
1700+
is not included in
1701+
val add : local_ int32 -> local_ int32 -> int32
1702+
|}]
16741703
(* Return modes *)
16751704
let zx : int ref -> (int -> unit) = (:=)
16761705
let zz : local_ (int ref) -> int -> unit = (:=)

typing/primitive.ml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,18 @@ let parse_declaration valdecl ~native_repr_args ~native_repr_res =
194194

195195
open Outcometree
196196

197+
let add_attribute_list ty attrs =
198+
List.fold_left (fun ty attr -> Otyp_attribute(ty, attr)) ty attrs
199+
197200
let rec add_native_repr_attributes ty attrs =
198201
match ty, attrs with
199-
| Otyp_arrow (label, am, a, rm, r), attr_opt :: rest ->
202+
| Otyp_arrow (label, am, a, rm, r), attr_l :: rest ->
200203
let r = add_native_repr_attributes r rest in
201-
let a =
202-
match attr_opt with
203-
| None -> a
204-
| Some attr -> Otyp_attribute (a, attr)
205-
in
204+
let a = add_attribute_list a attr_l in
206205
Otyp_arrow (label, am, a, rm, r)
207-
| _, [Some attr] -> Otyp_attribute (ty, attr)
206+
| _, [attr_l] -> add_attribute_list ty attr_l
208207
| _ ->
209-
assert (List.for_all (fun x -> x = None) attrs);
208+
assert (List.for_all (fun x -> x = []) attrs);
210209
ty
211210

212211
let oattr_unboxed = { oattr_name = "unboxed" }
@@ -216,6 +215,7 @@ let oattr_builtin = { oattr_name = "builtin" }
216215
let oattr_no_effects = { oattr_name = "no_effects" }
217216
let oattr_only_generative_effects = { oattr_name = "only_generative_effects" }
218217
let oattr_no_coeffects = { oattr_name = "no_coeffects" }
218+
let oattr_local_opt = { oattr_name = "local_opt" }
219219

220220
let print p osig_val_decl =
221221
let prims =
@@ -248,15 +248,20 @@ let print p osig_val_decl =
248248
else
249249
attrs
250250
in
251-
let attr_of_native_repr = function
252-
| _, Same_as_ocaml_repr -> None
253-
| _, Unboxed_float
254-
| _, Unboxed_integer _ -> if all_unboxed then None else Some oattr_unboxed
255-
| _, Untagged_int -> if all_untagged then None else Some oattr_untagged
251+
let attrs_of_mode_and_repr (m, repr) =
252+
(match m with
253+
| Prim_local | Prim_global -> []
254+
| Prim_poly -> [oattr_local_opt])
255+
@
256+
(match repr with
257+
| Same_as_ocaml_repr -> []
258+
| Unboxed_float
259+
| Unboxed_integer _ -> if all_unboxed then [] else [oattr_unboxed]
260+
| Untagged_int -> if all_untagged then [] else [oattr_untagged])
256261
in
257262
let type_attrs =
258-
List.map attr_of_native_repr p.prim_native_repr_args @
259-
[attr_of_native_repr p.prim_native_repr_res]
263+
List.map attrs_of_mode_and_repr p.prim_native_repr_args @
264+
[attrs_of_mode_and_repr p.prim_native_repr_res]
260265
in
261266
{ osig_val_decl with
262267
oval_prims = prims;

0 commit comments

Comments
 (0)