Allow mapping arguments and return values in all internal functions.

This commit is contained in:
Daniel Kirchner 2018-08-10 15:56:29 +02:00
parent 57ada1d69e
commit a9f31da411
6 changed files with 57 additions and 11 deletions

View File

@ -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.

View File

@ -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
)

View File

@ -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)
{

View File

@ -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.

View File

@ -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.

View File

@ -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.