Moves length check to reference resolver.

This commit is contained in:
Erik Kundt 2018-09-22 00:18:22 +02:00
parent ff5be17990
commit d821cbdff5
7 changed files with 36 additions and 13 deletions

View File

@ -231,6 +231,8 @@ void ReferencesResolver::endVisit(Mapping const& _typeName)
void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
{
TypePointer baseType = _typeName.baseType().annotation().type;
auto arrayType = dynamic_cast<ArrayType const*>(baseType.get());
bool isFixedSizeArray = (arrayType && arrayType->isByteArray()) || !baseType->isDynamicallySized();
if (!baseType)
{
solAssert(!m_errorReporter.errors().empty(), "");
@ -246,6 +248,8 @@ void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
RationalNumberType const* lengthType = dynamic_cast<RationalNumberType const*>(lengthTypeGeneric.get());
if (!lengthType || !lengthType->mobileType())
fatalTypeError(length->location(), "Invalid array length, expected integer literal or constant expression.");
else if (lengthType->isZero() && isFixedSizeArray)
fatalTypeError(length->location(), "Array with zero length specified.");
else if (lengthType->isFractional())
fatalTypeError(length->location(), "Array with fractional length specified.");
else if (lengthType->isNegative())

View File

@ -822,17 +822,12 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
{
case Type::Category::Array:
if (auto arrayType = dynamic_cast<ArrayType const*>(varType.get()))
{
if (
((arrayType->location() == DataLocation::Memory) ||
(arrayType->location() == DataLocation::CallData)) &&
!arrayType->validForCalldata()
)
m_errorReporter.typeError(_variable.location(), "Array is too large to be encoded.");
if (auto baseType = dynamic_cast<ArrayType const*>(arrayType->baseType().get()))
if (!baseType->isDynamicallySized() && baseType->length() == 0)
m_errorReporter.typeError(_variable.location(), "Fixed-size multidimensional arrays are not allowed to have zero length.");
}
break;
case Type::Category::Mapping:
if (auto mappingType = dynamic_cast<MappingType const*>(varType.get()))

View File

@ -0,0 +1,15 @@
contract C {
function a() public pure returns(int[0][500] memory) {}
function b() public pure returns(uint[0][500] memory) {}
function c() public pure returns(byte[0][500] memory) {}
function d() public pure returns(bytes32[0][500] memory) {}
function e() public pure returns(bytes[0][500] memory) {}
function e() public pure returns(string[0][500] memory) {}
}
// ----
// TypeError: (52-53): Array with zero length specified.
// TypeError: (111-112): Array with zero length specified.
// TypeError: (170-171): Array with zero length specified.
// TypeError: (232-233): Array with zero length specified.
// TypeError: (292-293): Array with zero length specified.
// TypeError: (353-354): Array with zero length specified.

View File

@ -0,0 +1,15 @@
contract C {
int[0] a;
uint[0] b;
byte[0] c;
bytes32[0] d;
bytes[0] e;
string[0] f;
}
// ----
// TypeError: (19-20): Array with zero length specified.
// TypeError: (32-33): Array with zero length specified.
// TypeError: (45-46): Array with zero length specified.
// TypeError: (61-62): Array with zero length specified.
// TypeError: (75-76): Array with zero length specified.
// TypeError: (90-91): Array with zero length specified.

View File

@ -21,5 +21,5 @@ contract C {
uint[((2) + 1) + 1] a12;
uint[(2 + 1) + ((1))] a13;
uint[(((2) + 1)) + (((1)))] a14;
uint[((((2) + 1)) + (((1))))%1] a15;
uint[((((3) + 1)) + (((1))))%2] a15;
}

View File

@ -1,6 +0,0 @@
contract C {
bytes[0] a;
function f() public pure returns(bytes32[0][500] memory) {}
}
// ----
// TypeError: (62-84): Fixed-size multidimensional arrays are not allowed to have zero length.

View File

@ -1,6 +1,6 @@
contract c {
uint[10] a;
uint[] a2;
struct x { uint[2**20] b; y[0] c; }
struct x { uint[2**20] b; y[1] c; }
struct y { uint d; mapping(uint=>x)[] e; }
}