02 - SimpleRevert

Goal: Trigger a revert without any message from Yul.

// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {SimpleRevert} from "../src/SimpleRevert.sol";

contract SimpleRevertTest is Test {
    SimpleRevert public c;

    function setUp() public {
        c = new SimpleRevert();
    }

    function test_SimpleRevert() public {
        vm.expectRevert();
        c.main();
    }
}

To revert execution (e.g., indicate an error), Yul provides the revert(p, s) opcode, which stops execution and returns an error code to the caller. A simple revert with no message can be done by specifying a memory pointer and length of zero (revert(0, 0)). This is equivalent to a Solidity revert() with no reason string.

// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

contract SimpleRevert {
    function main() external pure {
        assembly {
            // Revert execution with no data (equivalent to revert())
            revert(0, 0)
        }
    }
}

Explanation:

We do not need to store any data in memory for an empty revert. By convention, the pointer can be 0

and length 0 to signify no return data. This will roll back all state changes and signal an error.

Run test:

forge test --mp test/SimpleRevert.t.sol -vvvv

Last updated