cadence
cadence copied to clipboard
Re-Destroy optional resource
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
Thanks for reporting this. Yes, there is room for improvement here. At the moment resource invalidation happens at two stages:
- Statically - when the code is deployed
- 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).
@dsainati1 I think this will be resolved once conditions become view contexts?
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"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^