Skip to content

Commit dc7ea7f

Browse files
authored
Fix missing End_region primitives on switch arms (#898)
1 parent d53bd35 commit dc7ea7f

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

middle_end/flambda2/from_lambda/lambda_to_flambda.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,23 @@ let restore_continuation_context acc env ccenv cont ~close_early body =
805805
Continuation.print cont;
806806
body acc ccenv continuation_closing_region
807807

808+
let restore_continuation_context_for_switch_arm env cont =
809+
match Env.pop_regions_up_to_context env cont with
810+
| None -> cont
811+
| Some region ->
812+
let ({ continuation_closing_region; continuation_after_closing_region }
813+
: Env.region_closure_continuation) =
814+
Env.region_closure_continuation env region
815+
in
816+
if not (Continuation.equal cont continuation_after_closing_region)
817+
then
818+
Misc.fatal_errorf
819+
"The continuation %a following the region closure should be the \
820+
current continuation %a"
821+
Continuation.print continuation_after_closing_region Continuation.print
822+
cont;
823+
continuation_closing_region
824+
808825
let apply_cont_with_extra_args acc env ccenv ~dbg cont traps args =
809826
let extra_args =
810827
List.map
@@ -1576,6 +1593,7 @@ and cps_switch acc env ccenv (switch : L.lambda_switch) ~condition_dbg
15761593
(fun arg : IR.simple -> Var arg)
15771594
(Env.extra_args_for_continuation env k)
15781595
in
1596+
let k = restore_continuation_context_for_switch_arm env k in
15791597
let consts_rev =
15801598
(arm, k, None, IR.Var var :: extra_args) :: consts_rev
15811599
in
@@ -1586,6 +1604,7 @@ and cps_switch acc env ccenv (switch : L.lambda_switch) ~condition_dbg
15861604
(fun arg : IR.simple -> Var arg)
15871605
(Env.extra_args_for_continuation env k)
15881606
in
1607+
let k = restore_continuation_context_for_switch_arm env k in
15891608
let consts_rev =
15901609
(arm, k, None, IR.Const cst :: extra_args) :: consts_rev
15911610
in

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ let () = check_empty "toplevel rec binding"
114114
());;
115115
let () = check_empty "toplevel eval"
116116

117+
let () =
118+
let f x b g =
119+
let local_ p = x, x in
120+
if b then () else (g p; ())
121+
in f 0 true (fun _ -> ())
122+
let () = check_empty "constant switch arm"
123+
117124
module type T = sig val x : int end
118125
let _ =
119126
let module M : T =

ocaml/testsuite/tests/typing-local/regions.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
toplevel binding: OK
1414
toplevel rec binding: OK
1515
toplevel eval: OK
16+
constant switch arm: OK
1617
first class mod: OK
1718
class d definition: OK
1819
class definitions: OK

0 commit comments

Comments
 (0)