`ConstantEvaluator` computes shift and bit-not operations differently from codegen
The ConstantEvaluator does not truncate the result of shift and bit not operations, resulting in errors when the raw value is out of range of the target type or when it exceeds the hard cap of 4096-bit precision.
This happens, for example, when evaluating array lengths or storage layout specifiers.
The behavior is inconsistent with codegen, where the value is cleaned up either lazily (evmasm) or at the end of the operation (IR) to fit the type width.
The documentation section on shifts describes the correct process, stating that the result of a shift operation has the type of the left operand, overflow checks are never performed and the value is always truncated.
When the left operand is a literal and the right operand is an integer, shifts are always performed in the uint256 or int256 type, thus the truncation rule should be applied as well.
Expressions formed only by literals have RationalNumberType and are evaluated under unlimited precision arithmetic.
This means that computations do not overflow and divisions do not truncate in number literal expressions as documented in (Rational and Integer Literals)(https://docs.soliditylang.org/en/v0.8.37/types.html#rational-and-integer-literals).
The ConstantEvaluator computes operations both on integer and rational types using the same mechanism, which in practice has a hard 4096-bit precision cap.
The results of operations that exceed this cap cannot be produced and the ConstantEvaluator returns an empty result.
When the result has integer type (i.e., at least the right operand is an integer), it should be truncated as well, so this behavior is inconsistent with codegen too.
However, the result of literal expressions is always evaluated by the ConstantEvaluator under the 4096-bit precision during analysis phase.
The type of literal expressions cannot be named and they only appear as rvalues.
Any result which exceeds the 4096-bit precision also exceeds the 256-bit width and thus cannot be attributed to an lvalue, triggering an error regardless of having being computed or not.
The compiler never generates code for the execution of operations with only literals, so there is no behavior to match in this case.
The problem affects left and right shifts and bit not operations.
Note that right shifts are only impacted in the situations where the ConstantEvaluator hard cap is exceeded.
Minimal repro:
// SPDX-License-Identifier: NONE
pragma solidity ^0.8.0;
uint8 constant FIVE = 5;
uint8 constant SIXTEEN = 16;
contract C {
uint8 constant CONST_AND_LITERAL = SIXTEEN << 5;
uint8 constant CONST_ONLY = SIXTEEN << FIVE;
uint256 constant ONE = 1;
// TypeError 2643: Arithmetic error when computing constant value.
uint[CONST_AND_LITERAL] array1;
// TypeError 2643: Arithmetic error when computing constant value.
uint[CONST_ONLY] array2;
// TypeError 2643: Arithmetic error when computing constant value.
uint[SIXTEEN << 5] array3;
// TypeError 2643: Arithmetic error when computing constant value.
uint[2**255 << ONE] array4;
// Error (5462): Invalid array length, expected integer literal or constant expression.
uint[ONE << 5000] array5; // exceeds 4096-bit precision cap
function f() public pure returns (uint8) {
uint8 x = 16;
// Ok, returns 0
return x << 5;
}
}Analysis
The problem can surface from two sources: (1) The hard limit of the 4096-bit precision is exceeded or (2) the raw computed value is not truncated and is out of range of the target type.
In (1), the ConstantEvaluator cannot compute the final value and returns an empty result.
This triggers errors Error 5462: Invalid array length, expected integer literal or constant expression. in the example repro above.
On the other hand, in case 2, the result is computed and is held as a rational number with 4096-bit precision.
When ConstantEvaluator tries to convert the raw value it might not fit into the target result type, triggering in the example above TypeError 2643: Arithmetic error when computing constant value..
The computed value of the operation should be truncated to the type of the left operand as stated in the shift operator documentation section.
Note that in this case, literals are assigned either int256 or uint256.
Accordingly, codegen always performs cleanup/masking of the result so it fits properly into the left operand type.
In the evmasm pipeline, the final value is not truncated immediately. The compiler does not clean the bits if the following operation is not affected, it only performs the cleanup if the next operation uses the value.
On the other hand, the IR pipeline explicitly does the cleanup on the result (see YulUtilFunctions::typedShiftLeftFunction()).
Either way, the result is always truncated to the result type.
ConstantEvaluator needs to process shift operation results in a separate function/branch from ordinary (mostly arithmetic) operations.
I suggest that a cleanup function is added and used after shift operation is evaluated.
Source: argotorg/solidity