solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Allow reverting with signature `error Panic(uint)`

Open hrkrshnn opened this issue 4 years ago • 5 comments

Currently, solidity doesn't allow defining errors with the following names Panic and Error (note: name, not signature).

There are use cases where reverting with this error would be useful.

One example is in library fixed point implementation. When adding two fixed point numbers overflows, then it's useful to revert with Panic(0x11) to be consistent with how the compiler would revert in such cases. (List of error codes, or Docs)


Possible solutions

  1. @chriseth suggested allowing something like import Panic from std.errors.
  2. Allow defining errors with signature Panic(uint) and Error(string).
  3. Use inline assembly to mimic the error: no need for the compiler to do anything
  4. Use a collision hack. Find <name>(uint) with the same hash as Panic(uint) and similarly for Error(string).

hrkrshnn avatar Aug 12 '21 12:08 hrkrshnn

Pinging @paulrberg since this could be useful for https://github.com/hifi-finance/prb-math/blob/main/contracts/PRBMath.sol

hrkrshnn avatar Aug 12 '21 12:08 hrkrshnn

Yes, this would be helpful. This is the specific place where I am reverting with a custom error that mimics Panic:

if (rValue < x.value) {
    revert PRBMathUD60x18__AddOverflow(x.value, y.value);
}

If I could, I would switch to the Panic code instead of using my custom defined error.

PaulRBerg avatar Aug 13 '21 10:08 PaulRBerg

Decision: do 1 and 2 at the same time.

hrkrshnn avatar Aug 18 '21 12:08 hrkrshnn

Any news on that feature?

Amxx avatar Jun 15 '22 14:06 Amxx

Not yet but this is one of the features we have in the priority backlog so it should be one of the first to pick up once we have some more free cycles.

(1) Looks like it depends on stdlib though and that's not ready yet.

cameel avatar Jun 15 '22 20:06 cameel

Again, any news on that feature?

Amxx avatar Jun 16 '23 09:06 Amxx