solidity
solidity copied to clipboard
Function selectors accessed via contract variables are not considered constant
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.