Inconsistent treatment of constants by static analysis and constant evaluation
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
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.
```solidity
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.
```solidity
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:
```solidity
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;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Contributor guide
Research direction
Start by reproducing the three Solidity examples in the issue and compare constant evaluation with static analysis for the named uint256 value and bitwise negation. The work is done when these equivalent forms receive consistent, correct diagnostics, including the division, standalone expression, and inlined literal cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- solidity
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100