Skip to content

Fix Tezos.call_view with impure

E. Rivas requested to merge er433/fix/lib+call_view into dev

type:fixed

For LIGO developers

It has been reported that Tezos.call_view doesn't work after changing from direct operations to the library.

Here's a small contract showing the situation:

$ ligo compile contract call_view.mligo
Error(s) occurred while type checking the contract:
Ill typed contract:
  01: { parameter unit ;
  02:   storage unit ;
  03:   code { DROP
  04:          /* [] */ ;
  05:          PUSH address "tz1fakefakefakefakefakefakefakcphLA5"
  06:          /* [ address ] */ ;
  07:          SENDER
  08:          /* [ @sender address : address ] */ ;
  09:          PUSH string "foo"
  10:          /* [ string : @sender address : address ] */ ;
  11:          PAIR 3
  12:          /* [ pair string address address ] */ ;
  13:          UNPAIR 3
  14:          /* [ string : address : address ] */ ;
  15:          VIEW ;
  16:          IF_NONE { UNIT } {} ;
  17:          NIL operation ;
  18:          PAIR } }
At line 15 characters 9 to 13,
primitive VIEW expects 2 arguments but is given 0.
$ cat call_view.mligo 
let main ((_, _) : unit * unit) : operation list * unit =
  let u = match (Tezos.call_view "foo" (Tezos.sender) ("tz1fakefakefakefakefakefakefakcphLA5" : address) : unit option) with
    | Some x -> x
    | None -> () in
  ([] : operation list), u

We can print the mini-c and see the problem here:

$ ligo print mini-c --optimize main call_view.mligo
let (gen#140, gen#141) = gen#142 in
let gen#186 =
  (L("foo"), SENDER(), L(@"tz1fakefakefakefakefakefakefakcphLA5")) in
let gen#185 = let (s, x, a) = gen#186 in VIEW(s , x , a) in
PAIR(LIST_EMPTY() , match gen#185 with | None -> L(unit) | Some x -> x)

The variable gen#186 is not being removed as it contains the impure expression SENDER(), and VIEW will really need to have an string as first argument when doing code generation.

This MR is a quick fix reverting the case of Tezos.call_view to the previous situation (mapped in src/pass/predefined/predefined.ml), until we find a more general and permanent fix.

Changelog details:

Merge request reports