Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent treatment of constant expressions evaluation. #15709

Open
blishko opened this issue Jan 13, 2025 · 1 comment
Open

Inconsistent treatment of constant expressions evaluation. #15709

blishko opened this issue Jan 13, 2025 · 1 comment

Comments

@blishko
Copy link
Contributor

blishko commented Jan 13, 2025

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;
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@blishko
Copy link
Contributor Author

blishko commented Jan 13, 2025

This has been distilled out of #15600 where the current behaviour of constant evaluation causes SMTChecker to crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant