ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
23.13k stars 5.74k forks source link

View getter functions referencing immutable value types cannot be defined as pure #15096

Open Ashraile opened 4 months ago

Ashraile commented 4 months ago

Description

Immutable variables replace their values at deployment with compile-time constants (as inlined bytecode). This necessarily means that any getter functions for immutable value types do not view contract state and so can be defined as pure instead of view, however the compiler does not allow this.

Environment

Steps to Reproduce


contract ImmutableGetter {
    uint256 private immutable _releaseDate;
    constructor() {
        _releaseDate = block.timestamp;
    }
    // pure throws with ("potentially reads from environment or state"). This is lazy
    function releaseDate() public view /* pure */ returns (uint256) {
        return _releaseDate;
    }
}
nikola-matic commented 4 months ago

Heya @Ashraile and thanks for the issue. Now, just to be clear, we don't consider this to be a bug per-se, but the topic itself has been quite contentious and we still don't have a definitive decision on what to about the pure decorator. Some people are for the opposite of what you suggest - i.e. strengthening pure even further, but some are again the opposite of that. You can see some of the discussion in https://github.com/ethereum/solidity/issues/8153. There's some further reading in the following issues as well: https://github.com/ethereum/solidity/issues/12829, https://github.com/ethereum/solidity/issues/9554. The reason we don't consider it a bug is because it's technically reading a state variable (which of course most would assume to mean reading from storage). I'll triage this and keep it open, since it's definitely something that could be improved upon, but as you can see from the multitude of similar issues, it's not easy to come to an agreement, so it will take some time.

github-actions[bot] commented 1 month ago

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.