Skip to content

Internal error: tried to substitute for mutated var

Someone on #ligo provided this example:

$ ligo run test bug.ligo
An internal error ocurred. Please, contact the developers.
internal error, please report this as a bug: tried to substitute for mutated var File "src/passes/15-self_mini_c/subst.ml", line 312, characters 105-112.
type internal_storage is [@layout:comb] record [
  value : int;
]

type storage is [@layout:comb] record [
  internal_storage : internal_storage; 
]

type parameter is
  Reset

type return is list (operation) * storage

function reset (var s : storage) : storage is {
  s.internal_storage.value := 0;
} with (s);

function main (const action : parameter; const s : storage) : return is
 ((nil : list (operation)),    // No operations
  case action of [
  | Reset         -> reset(s)
  ]);

const test_reproducing = {
  const initial_storage: storage = record [
    internal_storage = record [
      value = 0;
    ];
  ];
  const (_typed_address, _, _) = Test.originate(main, initial_storage, 0tez);
} with ("OK")

It appears the problem is that somehow the mutable function parameter (for reset) winds up being compiled to a normal function parameter which is incorrectly mutated:

            (let reset = 
               fun s#3371 ->                                                         
               (let ()#3372 =
                  s#3371 := { !s#3371 with (0) = { (!s#3371).(0) with (0) = 
                  L(0) } } in                                                        
                !s#3371) in