Skip to content

Event and error selectors are not constants #13137

@cameel

Description

@cameel

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛language design :rage4:Any changes to the language, e.g. new featureslow effortThere is not much implementation work to be done. The task is very easy or tiny.medium difficultymedium impactDefault level of impactmust haveSomething we consider an essential part of Solidity 1.0.

    Type

    No type

    Projects

    Status

    Consider to Promote

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions