argotorg / argotorg/solidity

Function selectors accessed via contract variables are not considered constant

Open
#16,338 4 comments 0 reactions 0 assignees View on GitHub
bug :bug: low effort low impact must have eventually
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
21

Description

## 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

```solidity
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.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example with compiler version 0.8.31, then trace the compile-time constant validation for member access through `.selector`. The issue is done when the non-constant contract variable case `s3` is accepted consistently with `s2` and `s4`, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, solidity
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.