Merge pull request #1005 from ethereum/modifierbody

Require ";" after "_"
This commit is contained in:
Yoichi Hirai 2016-09-05 18:28:32 +02:00 committed by GitHub
commit e2b787cdd0
13 changed files with 69 additions and 59 deletions

View File

@ -17,6 +17,7 @@ Breaking Changes:
* Function call throws if target contract does not have code * Function call throws if target contract does not have code
* Modifiers are required to contain ``_`` (use ``if (false) _`` as a workaround if needed). * Modifiers are required to contain ``_`` (use ``if (false) _`` as a workaround if needed).
* Modifiers: return does not skip part in modifier after ``_`` * Modifiers: return does not skip part in modifier after ``_``
* Placeholder statement `_` in modifier now requires explicit `;`.
* ``ecrecover`` now returns zero if the input is malformed (it previously returned garbage) * ``ecrecover`` now returns zero if the input is malformed (it previously returned garbage)
* Removed ``--interface`` (Solidity interface) output option * Removed ``--interface`` (Solidity interface) output option
* JSON AST: General cleanup, renamed many nodes to match their C++ names. * JSON AST: General cleanup, renamed many nodes to match their C++ names.

View File

@ -148,10 +148,10 @@ restrictions highly readable.
{ {
if (msg.sender != _account) if (msg.sender != _account)
throw; throw;
// Do not forget the "_"! It will // Do not forget the "_;"! It will
// be replaced by the actual function // be replaced by the actual function
// body when the modifier is invoked. // body when the modifier is invoked.
_ _;
} }
/// Make `_newOwner` the new owner of this /// Make `_newOwner` the new owner of this
@ -164,7 +164,7 @@ restrictions highly readable.
modifier onlyAfter(uint _time) { modifier onlyAfter(uint _time) {
if (now < _time) throw; if (now < _time) throw;
_ _;
} }
/// Erase ownership information. /// Erase ownership information.
@ -187,7 +187,7 @@ restrictions highly readable.
modifier costs(uint _amount) { modifier costs(uint _amount) {
if (msg.value < _amount) if (msg.value < _amount)
throw; throw;
_ _;
if (msg.value > _amount) if (msg.value > _amount)
msg.sender.send(msg.value - _amount); msg.sender.send(msg.value - _amount);
} }
@ -286,7 +286,7 @@ function finishes.
modifier atStage(Stages _stage) { modifier atStage(Stages _stage) {
if (stage != _stage) throw; if (stage != _stage) throw;
_ _;
} }
function nextStage() internal { function nextStage() internal {
@ -304,7 +304,7 @@ function finishes.
now >= creationTime + 12 days) now >= creationTime + 12 days)
nextStage(); nextStage();
// The other stages transition by transaction // The other stages transition by transaction
_ _;
} }
// Order of the modifiers matters here! // Order of the modifiers matters here!
@ -328,7 +328,7 @@ function finishes.
// automatically. // automatically.
modifier transitionNext() modifier transitionNext()
{ {
_ _;
nextStage(); nextStage();
} }

View File

@ -321,7 +321,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
modifier onlyOwner { modifier onlyOwner {
if (msg.sender != owner) if (msg.sender != owner)
throw; throw;
_ _;
} }
} }
@ -341,7 +341,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
// Modifiers can receive arguments: // Modifiers can receive arguments:
modifier costs(uint price) { modifier costs(uint price) {
if (msg.value >= price) { if (msg.value >= price) {
_ _;
} }
} }
} }
@ -367,7 +367,7 @@ inheritable properties of contracts and may be overridden by derived contracts.
modifier noReentrancy() { modifier noReentrancy() {
if (locked) throw; if (locked) throw;
locked = true; locked = true;
_ _;
locked = false; locked = false;
} }

View File

@ -403,8 +403,8 @@ high or low invalid bids.
/// functions. `onlyBefore` is applied to `bid` below: /// functions. `onlyBefore` is applied to `bid` below:
/// The new function body is the modifier's body where /// The new function body is the modifier's body where
/// `_` is replaced by the old function body. /// `_` is replaced by the old function body.
modifier onlyBefore(uint _time) { if (now >= _time) throw; _ } modifier onlyBefore(uint _time) { if (now >= _time) throw; _; }
modifier onlyAfter(uint _time) { if (now <= _time) throw; _ } modifier onlyAfter(uint _time) { if (now <= _time) throw; _; }
function BlindAuction( function BlindAuction(
uint _biddingTime, uint _biddingTime,

View File

@ -41,8 +41,9 @@ ArrayTypeName = TypeName StorageLocation? '[' Expression? ']'
StorageLocation = 'memory' | 'storage' StorageLocation = 'memory' | 'storage'
Block = '{' Statement* '}' Block = '{' Statement* '}'
Statement = IfStatement | WhileStatement | ForStatement | Block | PlaceholderStatement | Statement = IfStatement | WhileStatement | ForStatement | Block |
( Continue | Break | Return | Throw | SimpleStatement | ExpressionStatement ) ';' ( PlaceholderStatement | Continue | Break | Return |
Throw | SimpleStatement | ExpressionStatement ) ';'
ExpressionStatement = Expression | VariableDefinition ExpressionStatement = Expression | VariableDefinition
IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )? IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?

View File

@ -753,7 +753,7 @@ ASTPointer<Statement> Parser::parseStatement()
{ {
statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString); statement = ASTNodeFactory(*this).createNode<PlaceholderStatement>(docString);
m_scanner->next(); m_scanner->next();
return statement; break;
} }
// fall-through // fall-through
default: default:

View File

@ -158,7 +158,7 @@ contract GlobalRegistrar is Registrar, AuctionSystem {
return bytes(_name).length < c_freeBytes; return bytes(_name).length < c_freeBytes;
} }
modifier onlyrecordowner(string _name) { if (m_toRecord[_name].owner == msg.sender) _ } modifier onlyrecordowner(string _name) { if (m_toRecord[_name].owner == msg.sender) _; }
function transfer(string _name, address _newOwner) onlyrecordowner(_name) { function transfer(string _name, address _newOwner) onlyrecordowner(_name) {
m_toRecord[_name].owner = _newOwner; m_toRecord[_name].owner = _newOwner;

View File

@ -71,7 +71,7 @@ contract FixedFeeRegistrar is Registrar {
address owner; address owner;
} }
modifier onlyrecordowner(string _name) { if (m_record(_name).owner == msg.sender) _ } modifier onlyrecordowner(string _name) { if (m_record(_name).owner == msg.sender) _; }
function reserve(string _name) { function reserve(string _name) {
Record rec = m_record(_name); Record rec = m_record(_name);

View File

@ -86,14 +86,14 @@ contract multiowned {
// simple single-sig function modifier. // simple single-sig function modifier.
modifier onlyowner { modifier onlyowner {
if (isOwner(msg.sender)) if (isOwner(msg.sender))
_ _;
} }
// multi-sig function modifier: the operation must have an intrinsic hash in order // multi-sig function modifier: the operation must have an intrinsic hash in order
// that later attempts can be realised as the same underlying operation and // that later attempts can be realised as the same underlying operation and
// thus count as confirmations. // thus count as confirmations.
modifier onlymanyowners(bytes32 _operation) { modifier onlymanyowners(bytes32 _operation) {
if (confirmAndCheck(_operation)) if (confirmAndCheck(_operation))
_ _;
} }
// METHODS // METHODS
@ -281,7 +281,7 @@ contract daylimit is multiowned {
// simple modifier for daily limit. // simple modifier for daily limit.
modifier limitedDaily(uint _value) { modifier limitedDaily(uint _value) {
if (underLimit(_value)) if (underLimit(_value))
_ _;
} }
// METHODS // METHODS

View File

@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(enum_value)
BOOST_AUTO_TEST_CASE(modifier_definition) BOOST_AUTO_TEST_CASE(modifier_definition)
{ {
CompilerStack c; CompilerStack c;
c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }"); c.addSource("a", "contract C { modifier M(uint i) { _; } function F() M(1) {} }");
c.parse(); c.parse();
map<string, unsigned> sourceIndices; map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1; sourceIndices["a"] = 1;
@ -136,20 +136,20 @@ BOOST_AUTO_TEST_CASE(modifier_definition)
Json::Value modifier = astJson["children"][0]["children"][0]; Json::Value modifier = astJson["children"][0]["children"][0];
BOOST_CHECK_EQUAL(modifier["name"], "ModifierDefinition"); BOOST_CHECK_EQUAL(modifier["name"], "ModifierDefinition");
BOOST_CHECK_EQUAL(modifier["attributes"]["name"], "M"); BOOST_CHECK_EQUAL(modifier["attributes"]["name"], "M");
BOOST_CHECK_EQUAL(modifier["src"], "13:24:1"); BOOST_CHECK_EQUAL(modifier["src"], "13:25:1");
} }
BOOST_AUTO_TEST_CASE(modifier_invocation) BOOST_AUTO_TEST_CASE(modifier_invocation)
{ {
CompilerStack c; CompilerStack c;
c.addSource("a", "contract C { modifier M(uint i) { _ } function F() M(1) {} }"); c.addSource("a", "contract C { modifier M(uint i) { _; } function F() M(1) {} }");
c.parse(); c.parse();
map<string, unsigned> sourceIndices; map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1; sourceIndices["a"] = 1;
Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json(); Json::Value astJson = ASTJsonConverter(c.ast("a"), sourceIndices).json();
Json::Value modifier = astJson["children"][0]["children"][1]["children"][2]; Json::Value modifier = astJson["children"][0]["children"][1]["children"][2];
BOOST_CHECK_EQUAL(modifier["name"], "ModifierInvocation"); BOOST_CHECK_EQUAL(modifier["name"], "ModifierInvocation");
BOOST_CHECK_EQUAL(modifier["src"], "51:4:1"); BOOST_CHECK_EQUAL(modifier["src"], "52:4:1");
BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["type"], "modifier (uint256)"); BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["type"], "modifier (uint256)");
BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["value"], "M"); BOOST_CHECK_EQUAL(modifier["children"][0]["attributes"]["value"], "M");
BOOST_CHECK_EQUAL(modifier["children"][1]["attributes"]["value"], "1"); BOOST_CHECK_EQUAL(modifier["children"][1]["attributes"]["value"], "1");
@ -185,7 +185,7 @@ BOOST_AUTO_TEST_CASE(array_type_name)
BOOST_AUTO_TEST_CASE(placeholder_statement) BOOST_AUTO_TEST_CASE(placeholder_statement)
{ {
CompilerStack c; CompilerStack c;
c.addSource("a", "contract C { modifier M { _ } }"); c.addSource("a", "contract C { modifier M { _; } }");
c.parse(); c.parse();
map<string, unsigned> sourceIndices; map<string, unsigned> sourceIndices;
sourceIndices["a"] = 1; sourceIndices["a"] = 1;

View File

@ -2353,7 +2353,7 @@ BOOST_AUTO_TEST_CASE(function_modifier)
char const* sourceCode = R"( char const* sourceCode = R"(
contract C { contract C {
function getOne() nonFree returns (uint r) { return 1; } function getOne() nonFree returns (uint r) { return 1; }
modifier nonFree { if (msg.value > 0) _ } modifier nonFree { if (msg.value > 0) _; }
} }
)"; )";
compileAndRun(sourceCode); compileAndRun(sourceCode);
@ -2365,8 +2365,8 @@ BOOST_AUTO_TEST_CASE(function_modifier_local_variables)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract C { contract C {
modifier mod1 { var a = 1; var b = 2; _ } modifier mod1 { var a = 1; var b = 2; _; }
modifier mod2(bool a) { if (a) return; else _ } modifier mod2(bool a) { if (a) return; else _; }
function f(bool a) mod1 mod2(a) returns (uint r) { return 3; } function f(bool a) mod1 mod2(a) returns (uint r) { return 3; }
} }
)"; )";
@ -2379,7 +2379,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_loop)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract C { contract C {
modifier repeat(uint count) { for (var i = 0; i < count; ++i) _ } modifier repeat(uint count) { for (var i = 0; i < count; ++i) _; }
function f() repeat(10) returns (uint r) { r += 1; } function f() repeat(10) returns (uint r) { r += 1; }
} }
)"; )";
@ -2391,7 +2391,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract C { contract C {
modifier repeat(bool twice) { if (twice) _ _ } modifier repeat(bool twice) { if (twice) _; _; }
function f(bool twice) repeat(twice) returns (uint r) { r += 1; } function f(bool twice) repeat(twice) returns (uint r) { r += 1; }
} }
)"; )";
@ -2406,7 +2406,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return)
// modifier code block. // modifier code block.
char const* sourceCode = R"( char const* sourceCode = R"(
contract C { contract C {
modifier repeat(bool twice) { if (twice) _ _ } modifier repeat(bool twice) { if (twice) _; _; }
function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; } function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; }
} }
)"; )";
@ -2420,10 +2420,10 @@ BOOST_AUTO_TEST_CASE(function_modifier_overriding)
char const* sourceCode = R"( char const* sourceCode = R"(
contract A { contract A {
function f() mod returns (bool r) { return true; } function f() mod returns (bool r) { return true; }
modifier mod { _ } modifier mod { _; }
} }
contract C is A { contract C is A {
modifier mod { if (false) _ } modifier mod { if (false) _; }
} }
)"; )";
compileAndRun(sourceCode); compileAndRun(sourceCode);
@ -2439,12 +2439,12 @@ BOOST_AUTO_TEST_CASE(function_modifier_calling_functions_in_creation_context)
function f1() mod2 { data |= 0x1; } function f1() mod2 { data |= 0x1; }
function f2() { data |= 0x20; } function f2() { data |= 0x20; }
function f3() { } function f3() { }
modifier mod1 { f2(); _ } modifier mod1 { f2(); _; }
modifier mod2 { f3(); if (false) _ } modifier mod2 { f3(); if (false) _; }
function getData() returns (uint r) { return data; } function getData() returns (uint r) { return data; }
} }
contract C is A { contract C is A {
modifier mod1 { f4(); _ } modifier mod1 { f4(); _; }
function f3() { data |= 0x300; } function f3() { data |= 0x300; }
function f4() { data |= 0x4000; } function f4() { data |= 0x4000; }
} }
@ -2459,11 +2459,11 @@ BOOST_AUTO_TEST_CASE(function_modifier_for_constructor)
contract A { contract A {
uint data; uint data;
function A() mod1 { data |= 2; } function A() mod1 { data |= 2; }
modifier mod1 { data |= 1; _ } modifier mod1 { data |= 1; _; }
function getData() returns (uint r) { return data; } function getData() returns (uint r) { return data; }
} }
contract C is A { contract C is A {
modifier mod1 { data |= 4; _ } modifier mod1 { data |= 4; _; }
} }
)"; )";
compileAndRun(sourceCode); compileAndRun(sourceCode);
@ -6920,7 +6920,7 @@ BOOST_AUTO_TEST_CASE(return_does_not_skip_modifier)
contract C { contract C {
uint public x; uint public x;
modifier setsx { modifier setsx {
_ _;
x = 9; x = 9;
} }
function f() setsx returns (uint) { function f() setsx returns (uint) {
@ -6941,7 +6941,7 @@ BOOST_AUTO_TEST_CASE(break_in_modifier)
uint public x; uint public x;
modifier run() { modifier run() {
for (uint i = 0; i < 10; i++) { for (uint i = 0; i < 10; i++) {
_ _;
break; break;
} }
} }
@ -6963,7 +6963,7 @@ BOOST_AUTO_TEST_CASE(stacked_return_with_modifiers)
uint public x; uint public x;
modifier run() { modifier run() {
for (uint i = 0; i < 10; i++) { for (uint i = 0; i < 10; i++) {
_ _;
break; break;
} }
} }
@ -6986,7 +6986,7 @@ BOOST_AUTO_TEST_CASE(mutex)
modifier protected { modifier protected {
if (locked) throw; if (locked) throw;
locked = true; locked = true;
_ _;
locked = false; locked = false;
} }
} }

View File

@ -859,8 +859,8 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation)
char const* text = R"( char const* text = R"(
contract B { contract B {
function f() mod1(2, true) mod2("0123456") { } function f() mod1(2, true) mod2("0123456") { }
modifier mod1(uint a, bool b) { if (b) _ } modifier mod1(uint a, bool b) { if (b) _; }
modifier mod2(bytes7 a) { while (a == "1234567") _ } modifier mod2(bytes7 a) { while (a == "1234567") _; }
} }
)"; )";
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
@ -871,7 +871,7 @@ BOOST_AUTO_TEST_CASE(invalid_function_modifier_type)
char const* text = R"( char const* text = R"(
contract B { contract B {
function f() mod1(true) { } function f() mod1(true) { }
modifier mod1(uint a) { if (a > 0) _ } modifier mod1(uint a) { if (a > 0) _; }
} }
)"; )";
BOOST_CHECK(expectError(text) == Error::Type::TypeError); BOOST_CHECK(expectError(text) == Error::Type::TypeError);
@ -882,8 +882,8 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation_parameters)
char const* text = R"( char const* text = R"(
contract B { contract B {
function f(uint8 a) mod1(a, true) mod2(r) returns (bytes7 r) { } function f(uint8 a) mod1(a, true) mod2(r) returns (bytes7 r) { }
modifier mod1(uint a, bool b) { if (b) _ } modifier mod1(uint a, bool b) { if (b) _; }
modifier mod2(bytes7 a) { while (a == "1234567") _ } modifier mod2(bytes7 a) { while (a == "1234567") _; }
} }
)"; )";
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
@ -894,7 +894,7 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables)
char const* text = R"( char const* text = R"(
contract B { contract B {
function f() mod(x) { uint x = 7; } function f() mod(x) { uint x = 7; }
modifier mod(uint a) { if (a > 0) _ } modifier mod(uint a) { if (a > 0) _; }
} }
)"; )";
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
@ -903,8 +903,8 @@ BOOST_AUTO_TEST_CASE(function_modifier_invocation_local_variables)
BOOST_AUTO_TEST_CASE(legal_modifier_override) BOOST_AUTO_TEST_CASE(legal_modifier_override)
{ {
char const* text = R"( char const* text = R"(
contract A { modifier mod(uint a) { _ } } contract A { modifier mod(uint a) { _; } }
contract B is A { modifier mod(uint a) { _ } } contract B is A { modifier mod(uint a) { _; } }
)"; )";
BOOST_CHECK(success(text)); BOOST_CHECK(success(text));
} }
@ -912,8 +912,8 @@ BOOST_AUTO_TEST_CASE(legal_modifier_override)
BOOST_AUTO_TEST_CASE(illegal_modifier_override) BOOST_AUTO_TEST_CASE(illegal_modifier_override)
{ {
char const* text = R"( char const* text = R"(
contract A { modifier mod(uint a) { _ } } contract A { modifier mod(uint a) { _; } }
contract B is A { modifier mod(uint8 a) { _ } } contract B is A { modifier mod(uint8 a) { _; } }
)"; )";
BOOST_CHECK(expectError(text) == Error::Type::TypeError); BOOST_CHECK(expectError(text) == Error::Type::TypeError);
} }
@ -921,7 +921,7 @@ BOOST_AUTO_TEST_CASE(illegal_modifier_override)
BOOST_AUTO_TEST_CASE(modifier_overrides_function) BOOST_AUTO_TEST_CASE(modifier_overrides_function)
{ {
char const* text = R"( char const* text = R"(
contract A { modifier mod(uint a) { _ } } contract A { modifier mod(uint a) { _; } }
contract B is A { function mod(uint a) { } } contract B is A { function mod(uint a) { } }
)"; )";
BOOST_CHECK(expectError(text) == Error::Type::TypeError); BOOST_CHECK(expectError(text) == Error::Type::TypeError);
@ -931,7 +931,7 @@ BOOST_AUTO_TEST_CASE(function_overrides_modifier)
{ {
char const* text = R"( char const* text = R"(
contract A { function mod(uint a) { } } contract A { function mod(uint a) { } }
contract B is A { modifier mod(uint a) { _ } } contract B is A { modifier mod(uint a) { _; } }
)"; )";
BOOST_CHECK(expectError(text) == Error::Type::TypeError); BOOST_CHECK(expectError(text) == Error::Type::TypeError);
} }
@ -941,7 +941,7 @@ BOOST_AUTO_TEST_CASE(modifier_returns_value)
char const* text = R"( char const* text = R"(
contract A { contract A {
function f(uint a) mod(2) returns (uint r) { } function f(uint a) mod(2) returns (uint r) { }
modifier mod(uint a) { _ return 7; } modifier mod(uint a) { _; return 7; }
} }
)"; )";
BOOST_CHECK(expectError(text) == Error::Type::TypeError); BOOST_CHECK(expectError(text) == Error::Type::TypeError);

View File

@ -681,15 +681,23 @@ BOOST_AUTO_TEST_CASE(placeholder_in_function_context)
BOOST_AUTO_TEST_CASE(modifier) BOOST_AUTO_TEST_CASE(modifier)
{ {
char const* text = "contract c {\n" char const* text = "contract c {\n"
" modifier mod { if (msg.sender == 0) _ }\n" " modifier mod { if (msg.sender == 0) _; }\n"
"}\n"; "}\n";
BOOST_CHECK(successParse(text)); BOOST_CHECK(successParse(text));
} }
BOOST_AUTO_TEST_CASE(modifier_without_semicolon)
{
char const* text = "contract c {\n"
" modifier mod { if (msg.sender == 0) _ }\n"
"}\n";
BOOST_CHECK(!successParse(text));
}
BOOST_AUTO_TEST_CASE(modifier_arguments) BOOST_AUTO_TEST_CASE(modifier_arguments)
{ {
char const* text = "contract c {\n" char const* text = "contract c {\n"
" modifier mod(uint a) { if (msg.sender == a) _ }\n" " modifier mod(uint a) { if (msg.sender == a) _; }\n"
"}\n"; "}\n";
BOOST_CHECK(successParse(text)); BOOST_CHECK(successParse(text));
} }
@ -697,8 +705,8 @@ BOOST_AUTO_TEST_CASE(modifier_arguments)
BOOST_AUTO_TEST_CASE(modifier_invocation) BOOST_AUTO_TEST_CASE(modifier_invocation)
{ {
char const* text = "contract c {\n" char const* text = "contract c {\n"
" modifier mod1(uint a) { if (msg.sender == a) _ }\n" " modifier mod1(uint a) { if (msg.sender == a) _; }\n"
" modifier mod2 { if (msg.sender == 2) _ }\n" " modifier mod2 { if (msg.sender == 2) _; }\n"
" function f() mod1(7) mod2 { }\n" " function f() mod1(7) mod2 { }\n"
"}\n"; "}\n";
BOOST_CHECK(successParse(text)); BOOST_CHECK(successParse(text));