Merge pull request #12885 from StrongerXi/improve-array-size-too-large-error-message

Improve error message for static array with size too large
This commit is contained in:
Kamil Śliwak 2022-04-05 18:09:59 +02:00 committed by GitHub
commit 34dd30d71b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 5 deletions

View File

@ -297,7 +297,7 @@ void DeclarationTypeChecker::endVisit(ArrayTypeName const& _typeName)
else if (optional<ConstantEvaluator::TypedRational> value = ConstantEvaluator::evaluate(m_errorReporter, *length))
lengthValue = value->value;
if (!lengthValue || lengthValue > TypeProvider::uint256()->max())
if (!lengthValue)
m_errorReporter.typeError(
5462_error,
length->location(),
@ -309,6 +309,12 @@ void DeclarationTypeChecker::endVisit(ArrayTypeName const& _typeName)
m_errorReporter.typeError(3208_error, length->location(), "Array with fractional length specified.");
else if (*lengthValue < 0)
m_errorReporter.typeError(3658_error, length->location(), "Array with negative length specified.");
else if (lengthValue > TypeProvider::uint256()->max())
m_errorReporter.typeError(
1847_error,
length->location(),
"Array length too large, maximum is 2**256 - 1."
);
_typeName.annotation().type = TypeProvider::array(
DataLocation::Storage,

View File

@ -2,4 +2,4 @@ contract C {
bytes32[8**90] ids;
}
// ----
// TypeError 5462: (25-30): Invalid array length, expected integer literal or constant expression.
// TypeError 1847: (25-30): Array length too large, maximum is 2**256 - 1.

View File

@ -2,4 +2,4 @@ contract C {
bytes32[8**90][500] ids;
}
// ----
// TypeError 5462: (25-30): Invalid array length, expected integer literal or constant expression.
// TypeError 1847: (25-30): Array length too large, maximum is 2**256 - 1.

View File

@ -0,0 +1,16 @@
contract C {
uint[uint(1)] valid_size_invalid_expr1;
uint[uint(2**256-1)] valid_size_invalid_expr2;
uint[uint(2**256)] invalid_size_invalid_expr3;
uint[int(1)] valid_size_invalid_expr4;
uint[int(2**256-1)] valid_size_invalid_expr5;
uint[int(2**256)] invalid_size_invalid_expr6;
}
// ----
// TypeError 5462: (22-29): Invalid array length, expected integer literal or constant expression.
// TypeError 5462: (66-80): Invalid array length, expected integer literal or constant expression.
// TypeError 5462: (117-129): Invalid array length, expected integer literal or constant expression.
// TypeError 5462: (169-175): Invalid array length, expected integer literal or constant expression.
// TypeError 5462: (212-225): Invalid array length, expected integer literal or constant expression.
// TypeError 5462: (262-273): Invalid array length, expected integer literal or constant expression.

View File

@ -1,5 +1,8 @@
contract C {
uint[8**90] ids;
uint[2**256-1] okay;
uint[2**256] tooLarge;
}
// ----
// TypeError 5462: (22-27): Invalid array length, expected integer literal or constant expression.
// TypeError 1847: (22-27): Array length too large, maximum is 2**256 - 1.
// TypeError 1847: (68-74): Array length too large, maximum is 2**256 - 1.

View File

@ -2,4 +2,4 @@ contract C {
uint[8**90][500] ids;
}
// ----
// TypeError 5462: (22-27): Invalid array length, expected integer literal or constant expression.
// TypeError 1847: (22-27): Array length too large, maximum is 2**256 - 1.