Browse Source

Bugfixes concerning variable declarations.

Fixes #1637
cl-refactor
chriseth 10 years ago
parent
commit
3bc78361d6
  1. 16
      libsolidity/AST.cpp
  2. 23
      libsolidity/Parser.cpp
  3. 12
      test/SolidityNameAndTypeResolution.cpp
  4. 9
      test/SolidityParser.cpp

16
libsolidity/AST.cpp

@ -378,20 +378,16 @@ void VariableDeclaration::checkTypeRequirements()
if ((m_type && !m_type->isValueType()) || !m_value) if ((m_type && !m_type->isValueType()) || !m_value)
BOOST_THROW_EXCEPTION(createTypeError("Unitialized \"constant\" variable.")); BOOST_THROW_EXCEPTION(createTypeError("Unitialized \"constant\" variable."));
} }
if (!m_value)
return;
if (m_type) if (m_type)
{ {
m_value->expectType(*m_type); if (m_value)
if (m_isStateVariable && !m_type->externalType() && getVisibility() >= Visibility::Public) m_value->expectType(*m_type);
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for state variables."));
if (!FunctionType(*this).externalType())
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for public state variables."));
} }
else else
{ {
// no type declared and no previous assignment, infer the type if (!m_value)
// This feature might be extended in the future.
BOOST_THROW_EXCEPTION(createTypeError("Assignment necessary for type detection."));
m_value->checkTypeRequirements(); m_value->checkTypeRequirements();
TypePointer type = m_value->getType(); TypePointer type = m_value->getType();
if (type->getCategory() == Type::Category::IntegerConstant) if (type->getCategory() == Type::Category::IntegerConstant)
@ -405,6 +401,8 @@ void VariableDeclaration::checkTypeRequirements()
BOOST_THROW_EXCEPTION(createTypeError("Variable cannot have void type.")); BOOST_THROW_EXCEPTION(createTypeError("Variable cannot have void type."));
m_type = type; m_type = type;
} }
if (m_isStateVariable && getVisibility() >= Visibility::Public && !FunctionType(*this).externalType())
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for public state variables."));
} }
bool VariableDeclaration::isExternalFunctionParameter() const bool VariableDeclaration::isExternalFunctionParameter() const

23
libsolidity/Parser.cpp

@ -472,17 +472,18 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
else else
BOOST_THROW_EXCEPTION(createParserError("Expected type name")); BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
// Parse "[...]" postfixes for arrays. if (type)
while (m_scanner->getCurrentToken() == Token::LBrack) // Parse "[...]" postfixes for arrays.
{ while (m_scanner->getCurrentToken() == Token::LBrack)
m_scanner->next(); {
ASTPointer<Expression> length; m_scanner->next();
if (m_scanner->getCurrentToken() != Token::RBrack) ASTPointer<Expression> length;
length = parseExpression(); if (m_scanner->getCurrentToken() != Token::RBrack)
nodeFactory.markEndPosition(); length = parseExpression();
expectToken(Token::RBrack); nodeFactory.markEndPosition();
type = nodeFactory.createNode<ArrayTypeName>(type, length); expectToken(Token::RBrack);
} type = nodeFactory.createNode<ArrayTypeName>(type, length);
}
return type; return type;
} }

12
test/SolidityNameAndTypeResolution.cpp

@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
{ {
char const* text = "contract test {\n" char const* text = "contract test {\n"
" uint256 stateVariable1;\n" " uint256 stateVariable1;\n"
" function fun(uint256 arg1) { var x; uint256 y; }" " function fun(uint256 arg1) { uint256 y; }"
"}\n"; "}\n";
ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed"); ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
} }
@ -1623,6 +1623,16 @@ BOOST_AUTO_TEST_CASE(bytes0_array)
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
} }
BOOST_AUTO_TEST_CASE(uninitialized_var)
{
char const* sourceCode = R"(
contract C {
function f() returns (uint) { var x; return 2; }
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

9
test/SolidityParser.cpp

@ -841,6 +841,15 @@ BOOST_AUTO_TEST_CASE(constant_is_keyword)
BOOST_CHECK_THROW(parseText(text), ParserError); BOOST_CHECK_THROW(parseText(text), ParserError);
} }
BOOST_AUTO_TEST_CASE(var_array)
{
char const* text = R"(
contract Foo {
function f() { var[] a; }
})";
BOOST_CHECK_THROW(parseText(text), ParserError);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

Loading…
Cancel
Save