Skip to content

Commit f367f80

Browse files
gretay-jsccasin
andauthored
flambda-backend: Zero alloc: "assert all" and signatures (#2687)
* Zero alloc inclusion check takes into account [@@@zero_alloc all] * Add a test * Move fabrication to typedtree to get accurate arity --------- Co-authored-by: Chris Casinghino <ccasinghino@janestreet.com>
1 parent b5db10f commit f367f80

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

typing/typedtree.ml

+24-7
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,12 @@ let as_computation_pattern (p : pattern) : computation general_pattern =
844844
pat_attributes = [];
845845
}
846846

847+
let function_arity params body =
848+
List.length params +
849+
match body with
850+
| Tfunction_body _ -> 0
851+
| Tfunction_cases _ -> 1
852+
847853
let rec classify_pattern_desc : type k . k pattern_desc -> k pattern_category =
848854
function
849855
| Tpat_alias _ -> Value
@@ -1062,7 +1068,24 @@ let let_bound_idents_with_modes_sorts_and_checks bindings =
10621068
iter_pattern_full ~both_sides_of_or:true f vb.vb_sort vb.vb_pat;
10631069
match vb.vb_pat.pat_desc, vb.vb_expr.exp_desc with
10641070
| Tpat_var (id, _, _, _), Texp_function fn ->
1065-
Ident.Map.add id fn.zero_alloc checks
1071+
let zero_alloc : Builtin_attributes.zero_alloc_attribute =
1072+
match fn.zero_alloc with
1073+
| Ignore_assert_all | Check _ | Assume _ -> fn.zero_alloc
1074+
| Default_zero_alloc when !Clflags.zero_alloc_check_assert_all ->
1075+
(* We fabricate a "Check" attribute if a top-level annotation
1076+
specifies that all functions should be checked for zero
1077+
alloc. *)
1078+
let arity = function_arity fn.params fn.body in
1079+
if arity > 0 then
1080+
Check { strict = false;
1081+
arity;
1082+
loc = Location.none;
1083+
opt = false }
1084+
else
1085+
Default_zero_alloc
1086+
| Default_zero_alloc -> Default_zero_alloc
1087+
in
1088+
Ident.Map.add id zero_alloc checks
10661089
| _ -> checks
10671090
) Ident.Map.empty bindings
10681091
in
@@ -1150,9 +1173,3 @@ let rec exp_is_nominal exp =
11501173
| Texp_field (parent, _, _, _) | Texp_send (parent, _, _) ->
11511174
exp_is_nominal parent
11521175
| _ -> false
1153-
1154-
let function_arity params body =
1155-
List.length params +
1156-
match body with
1157-
| Tfunction_body _ -> 0
1158-
| Tfunction_cases _ -> 1

0 commit comments

Comments
 (0)