mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3824 from ethereum/baseArgumentsEmptyParenthesis
Error when using empty parentheses for base class constructors that r…
This commit is contained in:
commit
037eba20fc
@ -11,6 +11,7 @@ Features:
|
|||||||
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
|
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
|
||||||
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).
|
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).
|
||||||
* General: Introduce new constructor syntax using the ``constructor`` keyword as experimental 0.5.0 feature.
|
* General: Introduce new constructor syntax using the ``constructor`` keyword as experimental 0.5.0 feature.
|
||||||
|
* Inheritance: Error when using empty parenthesis for base class constructors that require arguments as experimental 0.5.0 feature.
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
* Code Generator: Allow ``block.blockhash`` without being called.
|
* Code Generator: Allow ``block.blockhash`` without being called.
|
||||||
|
@ -320,7 +320,7 @@ void TypeChecker::checkContractAbstractConstructors(ContractDefinition const& _c
|
|||||||
{
|
{
|
||||||
auto baseContract = dynamic_cast<ContractDefinition const*>(&dereference(base->name()));
|
auto baseContract = dynamic_cast<ContractDefinition const*>(&dereference(base->name()));
|
||||||
solAssert(baseContract, "");
|
solAssert(baseContract, "");
|
||||||
if (!base->arguments().empty())
|
if (base->arguments() && !base->arguments()->empty())
|
||||||
argumentsNeeded.erase(baseContract);
|
argumentsNeeded.erase(baseContract);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -506,30 +506,46 @@ void TypeChecker::endVisit(InheritanceSpecifier const& _inheritance)
|
|||||||
// Interfaces do not have constructors, so there are zero parameters.
|
// Interfaces do not have constructors, so there are zero parameters.
|
||||||
parameterTypes = ContractType(*base).newExpressionType()->parameterTypes();
|
parameterTypes = ContractType(*base).newExpressionType()->parameterTypes();
|
||||||
|
|
||||||
if (!arguments.empty() && parameterTypes.size() != arguments.size())
|
if (arguments)
|
||||||
{
|
{
|
||||||
m_errorReporter.typeError(
|
bool v050 = m_scope->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
|
||||||
_inheritance.location(),
|
|
||||||
"Wrong argument count for constructor call: " +
|
|
||||||
toString(arguments.size()) +
|
|
||||||
" arguments given but expected " +
|
|
||||||
toString(parameterTypes.size()) +
|
|
||||||
"."
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < arguments.size(); ++i)
|
if (parameterTypes.size() != arguments->size())
|
||||||
if (!type(*arguments[i])->isImplicitlyConvertibleTo(*parameterTypes[i]))
|
{
|
||||||
m_errorReporter.typeError(
|
if (arguments->size() == 0 && !v050)
|
||||||
arguments[i]->location(),
|
m_errorReporter.warning(
|
||||||
"Invalid type for argument in constructor call. "
|
_inheritance.location(),
|
||||||
"Invalid implicit conversion from " +
|
"Wrong argument count for constructor call: " +
|
||||||
type(*arguments[i])->toString() +
|
toString(arguments->size()) +
|
||||||
" to " +
|
" arguments given but expected " +
|
||||||
parameterTypes[i]->toString() +
|
toString(parameterTypes.size()) +
|
||||||
" requested."
|
"."
|
||||||
);
|
);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_errorReporter.typeError(
|
||||||
|
_inheritance.location(),
|
||||||
|
"Wrong argument count for constructor call: " +
|
||||||
|
toString(arguments->size()) +
|
||||||
|
" arguments given but expected " +
|
||||||
|
toString(parameterTypes.size()) +
|
||||||
|
"."
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < arguments->size(); ++i)
|
||||||
|
if (!type(*(*arguments)[i])->isImplicitlyConvertibleTo(*parameterTypes[i]))
|
||||||
|
m_errorReporter.typeError(
|
||||||
|
(*arguments)[i]->location(),
|
||||||
|
"Invalid type for argument in constructor call. "
|
||||||
|
"Invalid implicit conversion from " +
|
||||||
|
type(*(*arguments)[i])->toString() +
|
||||||
|
" to " +
|
||||||
|
parameterTypes[i]->toString() +
|
||||||
|
" requested."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeChecker::endVisit(UsingForDirective const& _usingFor)
|
void TypeChecker::endVisit(UsingForDirective const& _usingFor)
|
||||||
|
@ -425,19 +425,22 @@ public:
|
|||||||
InheritanceSpecifier(
|
InheritanceSpecifier(
|
||||||
SourceLocation const& _location,
|
SourceLocation const& _location,
|
||||||
ASTPointer<UserDefinedTypeName> const& _baseName,
|
ASTPointer<UserDefinedTypeName> const& _baseName,
|
||||||
std::vector<ASTPointer<Expression>> _arguments
|
std::unique_ptr<std::vector<ASTPointer<Expression>>> _arguments
|
||||||
):
|
):
|
||||||
ASTNode(_location), m_baseName(_baseName), m_arguments(_arguments) {}
|
ASTNode(_location), m_baseName(_baseName), m_arguments(std::move(_arguments)) {}
|
||||||
|
|
||||||
virtual void accept(ASTVisitor& _visitor) override;
|
virtual void accept(ASTVisitor& _visitor) override;
|
||||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||||
|
|
||||||
UserDefinedTypeName const& name() const { return *m_baseName; }
|
UserDefinedTypeName const& name() const { return *m_baseName; }
|
||||||
std::vector<ASTPointer<Expression>> const& arguments() const { return m_arguments; }
|
// Returns nullptr if no argument list was given (``C``).
|
||||||
|
// If an argument list is given (``C(...)``), the arguments are returned
|
||||||
|
// as a vector of expressions. Note that this vector can be empty (``C()``).
|
||||||
|
std::vector<ASTPointer<Expression>> const* arguments() const { return m_arguments.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ASTPointer<UserDefinedTypeName> m_baseName;
|
ASTPointer<UserDefinedTypeName> m_baseName;
|
||||||
std::vector<ASTPointer<Expression>> m_arguments;
|
std::unique_ptr<std::vector<ASTPointer<Expression>>> m_arguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -268,7 +268,7 @@ bool ASTJsonConverter::visit(InheritanceSpecifier const& _node)
|
|||||||
{
|
{
|
||||||
setJsonNode(_node, "InheritanceSpecifier", {
|
setJsonNode(_node, "InheritanceSpecifier", {
|
||||||
make_pair("baseName", toJson(_node.name())),
|
make_pair("baseName", toJson(_node.name())),
|
||||||
make_pair("arguments", toJson(_node.arguments()))
|
make_pair("arguments", _node.arguments() ? toJson(*_node.arguments()) : Json::Value(Json::arrayValue))
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,8 @@ void InheritanceSpecifier::accept(ASTVisitor& _visitor)
|
|||||||
if (_visitor.visit(*this))
|
if (_visitor.visit(*this))
|
||||||
{
|
{
|
||||||
m_baseName->accept(_visitor);
|
m_baseName->accept(_visitor);
|
||||||
listAccept(m_arguments, _visitor);
|
if (m_arguments)
|
||||||
|
listAccept(*m_arguments, _visitor);
|
||||||
}
|
}
|
||||||
_visitor.endVisit(*this);
|
_visitor.endVisit(*this);
|
||||||
}
|
}
|
||||||
@ -104,7 +105,8 @@ void InheritanceSpecifier::accept(ASTConstVisitor& _visitor) const
|
|||||||
if (_visitor.visit(*this))
|
if (_visitor.visit(*this))
|
||||||
{
|
{
|
||||||
m_baseName->accept(_visitor);
|
m_baseName->accept(_visitor);
|
||||||
listAccept(m_arguments, _visitor);
|
if (m_arguments)
|
||||||
|
listAccept(*m_arguments, _visitor);
|
||||||
}
|
}
|
||||||
_visitor.endVisit(*this);
|
_visitor.endVisit(*this);
|
||||||
}
|
}
|
||||||
|
@ -157,8 +157,8 @@ void ContractCompiler::appendInitAndConstructorCode(ContractDefinition const& _c
|
|||||||
);
|
);
|
||||||
solAssert(baseContract, "");
|
solAssert(baseContract, "");
|
||||||
|
|
||||||
if (!m_baseArguments.count(baseContract->constructor()) && !base->arguments().empty())
|
if (!m_baseArguments.count(baseContract->constructor()) && base->arguments() && !base->arguments()->empty())
|
||||||
m_baseArguments[baseContract->constructor()] = &base->arguments();
|
m_baseArguments[baseContract->constructor()] = base->arguments();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Initialization of state variables in base-to-derived order.
|
// Initialization of state variables in base-to-derived order.
|
||||||
|
@ -286,17 +286,17 @@ ASTPointer<InheritanceSpecifier> Parser::parseInheritanceSpecifier()
|
|||||||
RecursionGuard recursionGuard(*this);
|
RecursionGuard recursionGuard(*this);
|
||||||
ASTNodeFactory nodeFactory(*this);
|
ASTNodeFactory nodeFactory(*this);
|
||||||
ASTPointer<UserDefinedTypeName> name(parseUserDefinedTypeName());
|
ASTPointer<UserDefinedTypeName> name(parseUserDefinedTypeName());
|
||||||
vector<ASTPointer<Expression>> arguments;
|
unique_ptr<vector<ASTPointer<Expression>>> arguments;
|
||||||
if (m_scanner->currentToken() == Token::LParen)
|
if (m_scanner->currentToken() == Token::LParen)
|
||||||
{
|
{
|
||||||
m_scanner->next();
|
m_scanner->next();
|
||||||
arguments = parseFunctionCallListArguments();
|
arguments.reset(new vector<ASTPointer<Expression>>(parseFunctionCallListArguments()));
|
||||||
nodeFactory.markEndPosition();
|
nodeFactory.markEndPosition();
|
||||||
expectToken(Token::RParen);
|
expectToken(Token::RParen);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
nodeFactory.setEndPositionFromNode(name);
|
nodeFactory.setEndPositionFromNode(name);
|
||||||
return nodeFactory.createNode<InheritanceSpecifier>(name, arguments);
|
return nodeFactory.createNode<InheritanceSpecifier>(name, std::move(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
Declaration::Visibility Parser::parseVisibilitySpecifier(Token::Value _token)
|
Declaration::Visibility Parser::parseVisibilitySpecifier(Token::Value _token)
|
||||||
|
@ -3,4 +3,5 @@ contract Base {
|
|||||||
}
|
}
|
||||||
contract Derived is Base(2) { }
|
contract Derived is Base(2) { }
|
||||||
contract Derived2 is Base(), Derived() { }
|
contract Derived2 is Base(), Derived() { }
|
||||||
contract Derived3 is Base, Derived {}
|
// ----
|
||||||
|
// Warning: Wrong argument count for constructor call: 0 arguments given but expected 1.
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
pragma experimental "v0.5.0";
|
||||||
|
|
||||||
|
contract Base {
|
||||||
|
constructor(uint) public {}
|
||||||
|
}
|
||||||
|
contract Derived is Base(2) { }
|
||||||
|
contract Derived2 is Base(), Derived() { }
|
||||||
|
// ----
|
||||||
|
// TypeError: Wrong argument count for constructor call: 0 arguments given but expected 1.
|
@ -0,0 +1,5 @@
|
|||||||
|
contract Base {
|
||||||
|
constructor(uint) public {}
|
||||||
|
}
|
||||||
|
contract Derived is Base(2) { }
|
||||||
|
contract Derived2 is Base, Derived {}
|
Loading…
Reference in New Issue
Block a user