Warn if modifiers are applied to functions without implementation.

This commit is contained in:
chriseth 2018-03-16 12:39:30 +01:00
parent d64e933327
commit 2ad1acaf72
5 changed files with 35 additions and 4 deletions

View File

@ -14,6 +14,7 @@ Features:
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
* Static Analyzer: Error on duplicated super constructor calls as experimental 0.5.0 feature.
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).
* Syntax Checker: Warn about modifiers on functions without implementation (this will turn into an error with version 0.5.0).
* Syntax Tests: Add source locations to syntax test expectations.
* General: Introduce new constructor syntax using the ``constructor`` keyword as experimental 0.5.0 feature.
* Inheritance: Error when using empty parentheses for base class constructors that require arguments as experimental 0.5.0 feature.

View File

@ -232,6 +232,13 @@ bool SyntaxChecker::visit(FunctionDefinition const& _function)
"Use \"constructor(...) { ... }\" instead."
);
}
if (!_function.isImplemented() && !_function.modifiers().empty())
{
if (v050)
m_errorReporter.syntaxError(_function.location(), "Functions without implementation cannot have modifiers.");
else
m_errorReporter.warning( _function.location(), "Modifiers of functions without implementation are ignored." );
}
return true;
}

View File

@ -365,12 +365,12 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(
Token::Value token = m_scanner->currentToken();
if (_allowModifiers && token == Token::Identifier)
{
// This can either be a modifier (function declaration) or the name of the
// variable (function type name plus variable).
if (
// If the name is empty, then this can either be a modifier (fallback function declaration)
// or the name of the state variable (function type name plus variable).
if (result.name->empty() && (
m_scanner->peekNextToken() == Token::Semicolon ||
m_scanner->peekNextToken() == Token::Assign
)
))
// Variable declaration, break here.
break;
else

View File

@ -0,0 +1,10 @@
pragma experimental "v0.5.0";
contract C
{
modifier only_owner() { _; }
function foo() only_owner public;
function bar() public only_owner;
}
// ----
// SyntaxError: (80-113): Functions without implementation cannot have modifiers.
// SyntaxError: (118-151): Functions without implementation cannot have modifiers.

View File

@ -0,0 +1,13 @@
// Previous versions of Solidity turned this
// into a parser error (they wrongly recognized
// these functions as state variables of
// function type).
contract C
{
modifier only_owner() { _; }
function foo() only_owner public;
function bar() public only_owner;
}
// ----
// Warning: (203-236): Modifiers of functions without implementation are ignored.
// Warning: (241-274): Modifiers of functions without implementation are ignored.