cadence icon indicating copy to clipboard operation
cadence copied to clipboard

Re-Destroy optional resource

Open KSlashh opened this issue 3 years ago • 2 comments

Bug Desciption

Resource is supposed to be destroy only once, so is the optional resource. But, if an interface destroy an optional resource in the pre condition, and an inherited function also destroy the optional resource, it will not cause any error. In fact, if you haven't destroy the optional resource in the inherited function, you will get error: lose of resource when deploy. Not sure it's a bug or a feature.

Example

KSlashh avatar Jul 06 '22 02:07 KSlashh

Thanks for reporting this. Yes, there is room for improvement here. At the moment resource invalidation happens at two stages:

  1. Statically - when the code is deployed
  2. Dynamically - when the program runs

The scenario you mentioned is currently caught only when the program is running (i.e: dynamic check). As you correctly pointed out, it could be improved to also give a static error during the type checking of the program (i.e: when deploying the code).

SupunS avatar Jul 06 '22 15:07 SupunS

@dsainati1 I think this will be resolved once conditions become view contexts?

turbolent avatar Sep 23 '22 23:09 turbolent

This will be resolved with the Stable Cadence release: FLIP 1056 will require conditions to only call view functions, and moving a resource is considered impure.

Testing the linked program above will fail statically:

error: Impure operation performed in view context
  --> test.cdc:19:16
   |
19 |                 ResourceTest.checkTicket(<-ticket): "invalid ticket"
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

turbolent avatar Oct 21 '22 19:10 turbolent