Skip to content

Commit

Permalink
Allow constant integer variables as array lengths.
Browse files Browse the repository at this point in the history
  • Loading branch information
Balajiganapathi authored and axic committed Nov 22, 2017
1 parent 5c9e273 commit 165857b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.4.19 (unreleased)

Features:
* Allow constant variables to be used as array length
* Syntax Checker: Turn the usage of ``callcode`` into an error as experimental 0.5.0 feature.
* Type Checker: Improve address checksum warning.
* Type Checker: More detailed errors for invalid array lengths (such as division by zero).
Expand Down
18 changes: 18 additions & 0 deletions libsolidity/analysis/ConstantEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ void ConstantEvaluator::endVisit(Literal const& _literal)
if (!_literal.annotation().type)
m_errorReporter.fatalTypeError(_literal.location(), "Invalid literal value.");
}

void ConstantEvaluator::endVisit(Identifier const& _identifier)
{
VariableDeclaration const *variableDeclaration = dynamic_cast<VariableDeclaration const *>(_identifier.annotation().referencedDeclaration);
if (!variableDeclaration)
return;
if (!variableDeclaration->isConstant())
m_errorReporter.fatalTypeError(_identifier.location(), "Identifier must be declared constant.");

ASTPointer<Expression> value = variableDeclaration->value();
if (value)
{
if (!value->annotation().type)
ConstantEvaluator e(*value, m_errorReporter);

_identifier.annotation().type = value->annotation().type;
}
}
1 change: 1 addition & 0 deletions libsolidity/analysis/ConstantEvaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ConstantEvaluator: private ASTConstVisitor
virtual void endVisit(BinaryOperation const& _operation);
virtual void endVisit(UnaryOperation const& _operation);
virtual void endVisit(Literal const& _literal);
virtual void endVisit(Identifier const& _identifier);

ErrorReporter& m_errorReporter;
};
Expand Down
18 changes: 18 additions & 0 deletions test/libsolidity/SolidityEndToEndTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,24 @@ BOOST_AUTO_TEST_CASE(constructor_static_array_argument)
ABI_CHECK(callContractFunction("b(uint256)", u256(2)), encodeArgs(u256(4)));
}

BOOST_AUTO_TEST_CASE(constant_var_as_array_length)
{
char const* sourceCode = R"(
contract C {
uint constant LEN = 3;
uint[LEN] public a;
function C(uint[LEN] _a) {
a = _a;
}
}
)";
compileAndRun(sourceCode, 0, "C", encodeArgs(u256(1), u256(2), u256(3)));
ABI_CHECK(callContractFunction("a(uint256)", u256(0)), encodeArgs(u256(1)));
ABI_CHECK(callContractFunction("a(uint256)", u256(1)), encodeArgs(u256(2)));
ABI_CHECK(callContractFunction("a(uint256)", u256(2)), encodeArgs(u256(3)));
}

BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
{
char const* sourceCode = R"(
Expand Down
24 changes: 23 additions & 1 deletion test/libsolidity/SolidityNameAndTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2107,7 +2107,7 @@ BOOST_AUTO_TEST_CASE(array_with_nonconstant_length)
function f(uint a) public { uint8[a] x; }
}
)";
CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal.");
CHECK_ERROR(text, TypeError, "Identifier must be declared constant");
}

BOOST_AUTO_TEST_CASE(array_with_negative_length)
Expand Down Expand Up @@ -7263,6 +7263,28 @@ BOOST_AUTO_TEST_CASE(array_length_not_convertible_to_integer)
CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal.");
}

BOOST_AUTO_TEST_CASE(array_length_constant_var)
{
char const* text = R"(
contract C {
uint constant LEN = 10;
uint[LEN] ids;
}
)";
CHECK_SUCCESS(text);
}

BOOST_AUTO_TEST_CASE(array_length_non_integer_constant_var)
{
char const* text = R"(
contract C {
bool constant LEN = true;
uint[LEN] ids;
}
)";
CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal.");
}

BOOST_AUTO_TEST_CASE(array_length_invalid_expression)
{
char const* text = R"(
Expand Down

0 comments on commit 165857b

Please sign in to comment.