Skip to content

Function selectors accessed via contract variables are not considered constant #16338

@cameel

Description

@cameel

Description

A function selector accessed through this is considered compile-time constant, even though this itself is not (the address won't change at runtime but is not known at compilation time). This makes sense, because functions themselves are constant and do not depend on the address. However, even though the same reasoning applies to other contract variables, we do not consider their selectors constant.

This does work for constant contract variables though, which indicates that the check is based on the constness of the variable rather than the function and is wrong.

Environment

  • Compiler version: 0.8.31

Steps to Reproduce

contract C {
    function f() external {}
}

contract D is C {
    C cvar            = C(address(0));
    C constant cconst = C(address(0));

    bytes4 constant s1 = C(address(0)).f.selector; // OK
    bytes4 constant s2 = this.f.selector;          // OK
    bytes4 constant s3 = cvar.f.selector;          // Error: Initial value for constant variable has to be compile-time constant.
    bytes4 constant s4 = cconst.f.selector;        // OK
}

There is no good reason for s3 to be invalid if both s2 and s4 are accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛low effortThere is not much implementation work to be done. The task is very easy or tiny.low impactChanges are not very noticeable or potential benefits are limited.must have eventuallySomething we consider essential but not enough to prevent us from releasing Solidity 1.0 without it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions