From a9f31da41146221c674356d5678030616110d471 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 10 Aug 2018 15:56:29 +0200 Subject: [PATCH] Allow mapping arguments and return values in all internal functions. --- Changelog.md | 2 +- libsolidity/analysis/TypeChecker.cpp | 1 - test/libsolidity/SolidityEndToEndTest.cpp | 56 +++++++++++++++++++ .../types/mapping/argument_internal.sol | 2 - .../types/mapping/argument_private.sol | 2 - .../types/mapping/mapping_return_internal.sol | 5 -- 6 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Changelog.md b/Changelog.md index 22a751f69..a61a73002 100644 --- a/Changelog.md +++ b/Changelog.md @@ -71,7 +71,7 @@ Language Features: * General: Support ``pop()`` for storage arrays. * General: Scoping rules now follow the C99-style. * General: Allow ``enum``s in interfaces. - * General: Allow ``mapping`` arguments and return values in internal library functions. + * General: Allow ``mapping`` storage pointers as arguments and return values in all internal functions. Compiler Features: * C API (``libsolc``): Export the ``solidity_license``, ``solidity_version`` and ``solidity_compile`` methods. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 62216272b..f2f5e6269 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -632,7 +632,6 @@ bool TypeChecker::visit(FunctionDefinition const& _function) if ( !type(*var)->canLiveOutsideStorage() && !( - isLibraryFunction && (_function.visibility() <= FunctionDefinition::Visibility::Internal) && type(*var)->category() == Type::Category::Mapping ) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index cc89a307d..09e940281 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1543,6 +1543,62 @@ BOOST_AUTO_TEST_CASE(mapping_local_compound_assignment) ABI_CHECK(callContractFunction("f()"), encodeArgs(byte(42), byte(0), byte(0), byte(21))); } +BOOST_AUTO_TEST_CASE(mapping_internal_argument) +{ + char const* sourceCode = R"( + contract test { + mapping(uint8 => uint8) a; + mapping(uint8 => uint8) b; + function set_internal(mapping(uint8 => uint8) storage m, uint8 key, uint8 value) internal returns (uint8) { + uint8 oldValue = m[key]; + m[key] = value; + return oldValue; + } + function set(uint8 key, uint8 value_a, uint8 value_b) public returns (uint8 old_a, uint8 old_b) { + old_a = set_internal(a, key, value_a); + old_b = set_internal(b, key, value_b); + } + function get(uint8 key) public returns (uint8, uint8) { + return (a[key], b[key]); + } + } + )"; + compileAndRun(sourceCode); + + ABI_CHECK(callContractFunction("set(uint8,uint8,uint8)", byte(1), byte(21), byte(42)), encodeArgs(byte(0), byte(0))); + ABI_CHECK(callContractFunction("get(uint8)", byte(1)), encodeArgs(byte(21), byte(42))); + ABI_CHECK(callContractFunction("set(uint8,uint8,uint8)", byte(1), byte(10), byte(11)), encodeArgs(byte(21), byte(42))); + ABI_CHECK(callContractFunction("get(uint8)", byte(1)), encodeArgs(byte(10), byte(11))); +} + +BOOST_AUTO_TEST_CASE(mapping_internal_return) +{ + char const* sourceCode = R"( + contract test { + mapping(uint8 => uint8) a; + mapping(uint8 => uint8) b; + function f() internal returns (mapping(uint8 => uint8) storage r) { + r = a; + r[1] = 42; + r = b; + r[1] = 84; + } + function g() public returns (uint8, uint8, uint8, uint8, uint8, uint8) { + f()[2] = 21; + return (a[0], a[1], a[2], b[0], b[1], b[2]); + } + function h() public returns (uint8, uint8, uint8, uint8, uint8, uint8) { + mapping(uint8 => uint8) storage m = f(); + m[2] = 17; + return (a[0], a[1], a[2], b[0], b[1], b[2]); + } + } + )"; + compileAndRun(sourceCode); + + ABI_CHECK(callContractFunction("g()"), encodeArgs(byte(0), byte(42), byte(0), byte(0), byte(84), byte (21))); + ABI_CHECK(callContractFunction("h()"), encodeArgs(byte(0), byte(42), byte(0), byte(0), byte(84), byte (17))); +} BOOST_AUTO_TEST_CASE(structs) { diff --git a/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol b/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol index 395f58088..3c021515c 100644 --- a/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol +++ b/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol @@ -1,7 +1,5 @@ -// This is expected to fail now, but may work in the future. contract C { function f(mapping(uint => uint) storage) internal pure { } } // ---- -// TypeError: (89-110): Type is required to live outside storage. diff --git a/test/libsolidity/syntaxTests/types/mapping/argument_private.sol b/test/libsolidity/syntaxTests/types/mapping/argument_private.sol index 0360514ea..63733d711 100644 --- a/test/libsolidity/syntaxTests/types/mapping/argument_private.sol +++ b/test/libsolidity/syntaxTests/types/mapping/argument_private.sol @@ -1,7 +1,5 @@ -// This is expected to fail now, but may work in the future. contract C { function f(mapping(uint => uint) storage) private pure { } } // ---- -// TypeError: (89-110): Type is required to live outside storage. diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol index a46003f8b..4912836ef 100644 --- a/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol @@ -1,4 +1,3 @@ -// This should be allowed in a future release. contract C { mapping(uint=>uint) m; function f() internal view returns (mapping(uint=>uint) storage) { @@ -15,7 +14,3 @@ contract C { } } // ---- -// TypeError: (127-146): Type is required to live outside storage. -// TypeError: (221-240): Type is required to live outside storage. -// TypeError: (316-345): Type is required to live outside storage. -// TypeError: (409-438): Type is required to live outside storage.