Skip to content

Mutable record field updates get lost when in partial application #10032

Description

@IagoAbal

I observed a regression when upgrading from OCaml 4.04.2 to a more recent version, after some debugging I came to the conclusion that this is related to mutable record fields and partial application, and the behavior changed from 4.05.0 to 4.06.0. Here you have a minimal example (simply ocamlopt main.ml && ./main):

type norm_env =
  { mutable k : int; }

let push_local ({k;} as env) _ =
  env.k <- k + 1;
  Printf.eprintf "push_local %d --> %d\n" k env.k

let pop_local ({k;} as env) _ =
  env.k <- k - 1;
  Printf.eprintf "pop_local %d --> %d\n" k env.k

let bug() =
  let env = { k=0; } in
  Printf.eprintf "BUG?\n";
  List.iter (push_local env) [1; 2];        (* BUG? *)
  List.iter (pop_local env) [1; 2]          (* BUG? *)

let works() =
  let env = { k=0; } in
  Printf.eprintf "WORKS\n";
  List.iter (fun x -> push_local env x) [1; 2];   (* WORKS *)
  List.iter (fun x -> pop_local env x) [1; 2]     (* WORKS *)

let _ =
  bug();
  works()

(* OCaml 4.05.0
BUG?
push_local 0 --> 1
push_local 1 --> 2
pop_local 2 --> 1
pop_local 1 --> 0
WORKS
push_local 0 --> 1
push_local 1 --> 2
pop_local 2 --> 1
pop_local 1 --> 0
 *)

(* OCaml 4.06.0
BUG?
push_local 0 --> 1
push_local 0 --> 1
pop_local 1 --> 0
pop_local 1 --> 0
WORKS
push_local 0 --> 1
push_local 1 --> 2
pop_local 2 --> 1
pop_local 1 --> 0
 *)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions