TVM-Solidity-Compiler icon indicating copy to clipboard operation
TVM-Solidity-Compiler copied to clipboard

false warning: Function state mutability can be restricted to pure [v0.57.3]

Open ilyar opened this issue 3 years ago • 3 comments

App.sol

pragma ton-solidity = 0.57.3;

contract App {
    uint public owner;

    modifier onlyOwner() {
        require(msg.pubkey() == owner);
        _;
    }

    modifier checkPubKey() {
        require(msg.pubkey() == tvm.pubkey());
        _;
    }

    constructor() public checkPubKey {
        tvm.accept();
        owner = msg.pubkey();
    }

    function foo(string bar) public view onlyOwner returns(string out) {
        tvm.accept();
        out = format('42{}', bar);
    }
}
npx everdev sol set --compiler 0.57.3
npx everdev sol compile --code --output-dir build App.sol
Warning: Function state mutability can be restricted to pure
  --> App.sol:21:5:
   |
21 |     function foo(string bar) public view onlyOwner returns(string out) {
   |     ^ (Relevant source part starts here and spans across multiple lines).

ilyar avatar Feb 23 '22 20:02 ilyar

Why is it wrong warning?

IgorKoval avatar Feb 28 '22 14:02 IgorKoval

https://github.com/broxus/ton-contracts/pull/4#issuecomment-1046136377 This method can't be pure because the onlyOwner modifier must have access to the contract state which is not loaded for pure methods. It is a known bug in compiler which still exists. @Rexagon

ilyar avatar Mar 01 '22 01:03 ilyar

Hi @IgorKoval ! How's it going? For me, it seems like a critical bug. onlyOwner is a well-known pattern in Solidity development. Probably every second developer will see the compiler hint and insert the pure modifier, which leads to unexpected behavior.

pavlovdog avatar Mar 31 '22 14:03 pavlovdog