Generates a syntax error when declaring a variable declaration's LHS has no named components

This commit is contained in:
Christian Parpart 2018-07-05 12:31:46 +02:00 committed by chriseth
parent fa8102880f
commit cc585138bc
5 changed files with 37 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include <libsolidity/analysis/SemVerHandler.h>
#include <libsolidity/interface/ErrorReporter.h>
#include <libsolidity/interface/Version.h>
#include <boost/algorithm/cxx11/all_of.hpp>
using namespace std;
using namespace dev;
@ -254,6 +255,18 @@ bool SyntaxChecker::visit(FunctionTypeName const& _node)
return true;
}
bool SyntaxChecker::visit(VariableDeclarationStatement const& _statement)
{
// Report if none of the variable components in the tuple have a name (only possible via deprecated "var")
if (boost::algorithm::all_of_equal(_statement.declarations(), nullptr))
m_errorReporter.syntaxError(
_statement.location(),
"The use of the \"var\" keyword is disallowed. The declaration part of the statement can be removed, since it is empty."
);
return true;
}
bool SyntaxChecker::visit(StructDefinition const& _struct)
{
if (_struct.members().empty())

View File

@ -69,6 +69,8 @@ private:
virtual bool visit(FunctionDefinition const& _function) override;
virtual bool visit(FunctionTypeName const& _node) override;
virtual bool visit(VariableDeclarationStatement const& _statement) override;
virtual bool visit(StructDefinition const& _struct) override;
ErrorReporter& m_errorReporter;

View File

@ -13,6 +13,8 @@ contract C {
}
}
// ----
// SyntaxError: (307-325): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// SyntaxError: (335-350): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// Warning: (179-198): Different number of components on the left hand side (2) than on the right hand side (3).
// Warning: (208-233): Different number of components on the left hand side (3) than on the right hand side (2).
// Warning: (243-262): Different number of components on the left hand side (2) than on the right hand side (3).

View File

@ -3,4 +3,5 @@ contract C {
function f() public { var (,) = one(); }
}
// ----
// SyntaxError: (81-96): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// TypeError: (81-96): Wildcard both at beginning and end of variable declaration list is only allowed if the number of components is equal.

View File

@ -0,0 +1,19 @@
pragma solidity ^0.4.20;
contract C {
function f() internal pure {}
function g() internal pure returns (uint) { return 1; }
function h() internal pure returns (uint, uint) { return (1, 2); }
function test() internal pure {
var () = f();
var () = g();
var (,) = h();
}
}
// ----
// SyntaxError: (249-261): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// SyntaxError: (271-283): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// SyntaxError: (293-306): The use of the "var" keyword is disallowed. The declaration part of the statement can be removed, since it is empty.
// TypeError: (271-283): Too many components (1) in value for variable assignment (0) needed