Open
Description
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;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^