This bounty is no longer available
Web3 DAO | Ethereum Foundation Logo

Event and error selectors are not constants

Organization

Ethereum Foundation

Deadline

N/A

Status

ENDED


INSTRUCTIONS

Description

Function selectors can be used to initialize constants while error and event selectors (recently introduced in #12921) are rejected as non-constant. I think that all of them should work the same way in that regard.

Steps to Reproduce

contract C {
    event Ev();
    error Er();
    function f() external {}

    bytes4 constant functionSelector = this.f.selector;  // OK
    bytes4 constant errorSelector = Er.selector;         // Error: Initial value for constant variable has to be compile-time constant.
    bytes32 constant eventSelector = Ev.selector;        // Error: Initial value for constant variable has to be compile-time constant.
}
Error: Initial value for constant variable has to be compile-time constant.
 --> test.sol:7:37:
  |
7 |     bytes4 constant errorSelector = Er.selector;         // Error: Initial value for constant variable has to be compile-time constant.
  |                                     ^^^^^^^^^^^

Error: Initial value for constant variable has to be compile-time constant.
 --> test.sol:8:38:
  |
8 |     bytes32 constant eventSelector = Ev.selector;        // Error: Initial value for constant variable has to be compile-time constant.
  |                                      ^^^^^^^^^^^            

The test passes without errors if you remove constant from the state variables.

Environment

  • Compiler version: 0.8.15 (develop)