Skip to content

Inconsistent treatment of constants by static analysis and constant evaluation #15709

Open
@blishko

Description

@blishko

Description

The compiler exhibits an inconsistent treatment of constant expressions evaluation.

When applying bitwise negation on uint256 max value (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), the compiler reports error for some expressions, but does not report an error in others.

When the expression appears as part of division, an error is reported.

contract BitwiseSolver {
    uint256 constant largeConstant = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
    function test() public returns (uint256) {
        return 1/(~largeConstant);
    }
}

Error reported:

Error: Arithmetic error when computing constant value.
 --> test2.sol:5:19:
  |
5 |         return 1/(~largeConstant);
  |                   ^^^^^^^^^^^^^^

However, when the expression stands on its own, no error is reported.

contract BitwiseSolver {
    uint256 constant largeConstant = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
    function test() public returns (uint256) {
        return ~largeConstant;
    }
}

Note that if the definition is inlined and the bitwise negation is applied directly to a literal a conversion error is reported:

contract BitwiseSolver {
    function test() public returns (uint256) {
        return ~0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
    }
}
Error: Return argument type int_const -115...(71 digits omitted)...9936 is not implicitly convertible to expected type (type of first return variable) uint256. Cannot implicitly convert signed literal to unsigned type.
 --> test2.sol:3:16:
  |
3 |         return ~0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛low impactChanges are not very noticeable or potential benefits are limited.medium effortDefault level of effortmust have eventuallySomething we consider essential but not enough to prevent us from releasing Solidity 1.0 without it.should compile without errorError is reported even though it shouldn't. Source is fine.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions