aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Kirchner <daniel@ekpyron.org>2018-08-10 21:13:12 +0800
committerDaniel Kirchner <daniel@ekpyron.org>2018-08-13 20:31:10 +0800
commit57ada1d69e311f847b4581a0e487aebd1b0e468f (patch)
tree653920cb378e2a7ac1a97ea3b022c106c599a197
parenta2c754b3fed422b3d8027a5298624bcfed3744a5 (diff)
downloaddexon-solidity-57ada1d69e311f847b4581a0e487aebd1b0e468f.tar.gz
dexon-solidity-57ada1d69e311f847b4581a0e487aebd1b0e468f.tar.zst
dexon-solidity-57ada1d69e311f847b4581a0e487aebd1b0e468f.zip
Allow mapping arguments and return values in internal library functions.
-rw-r--r--Changelog.md1
-rw-r--r--libsolidity/analysis/TypeChecker.cpp9
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp156
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/argument_external.sol7
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/argument_internal.sol7
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/argument_private.sol7
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/argument_public.sol7
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_argument_external.sol6
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_argument_internal.sol4
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_argument_private.sol4
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_argument_public.sol6
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_return_external.sol10
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_return_internal.sol6
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_return_private.sol6
-rw-r--r--test/libsolidity/syntaxTests/types/mapping/library_return_public.sol10
15 files changed, 245 insertions, 1 deletions
diff --git a/Changelog.md b/Changelog.md
index 56e00003..22a751f6 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -71,6 +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.
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 bcc3757a..62216272 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -629,7 +629,14 @@ bool TypeChecker::visit(FunctionDefinition const& _function)
}
for (ASTPointer<VariableDeclaration> const& var: _function.parameters() + _function.returnParameters())
{
- if (!type(*var)->canLiveOutsideStorage())
+ if (
+ !type(*var)->canLiveOutsideStorage() &&
+ !(
+ isLibraryFunction &&
+ (_function.visibility() <= FunctionDefinition::Visibility::Internal) &&
+ type(*var)->category() == Type::Category::Mapping
+ )
+ )
m_errorReporter.typeError(var->location(), "Type is required to live outside storage.");
if (_function.visibility() >= FunctionDefinition::Visibility::Public && !(type(*var)->interfaceType(isLibraryFunction)))
m_errorReporter.fatalTypeError(var->location(), "Internal or recursive type is not allowed for public or external functions.");
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index e487f0ae..cc89a307 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -7961,6 +7961,162 @@ BOOST_AUTO_TEST_CASE(internal_types_in_library)
ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(4), u256(17)));
}
+BOOST_AUTO_TEST_CASE(mapping_arguments_in_library)
+{
+ char const* sourceCode = R"(
+ library Lib {
+ function set(mapping(uint => uint) storage m, uint key, uint value) internal
+ {
+ m[key] = value;
+ }
+ function get(mapping(uint => uint) storage m, uint key) internal view returns (uint)
+ {
+ return m[key];
+ }
+ }
+ contract Test {
+ mapping(uint => uint) m;
+ function set(uint256 key, uint256 value) public returns (uint)
+ {
+ uint oldValue = Lib.get(m, key);
+ Lib.set(m, key, value);
+ return oldValue;
+ }
+ function get(uint256 key) public view returns (uint) {
+ return Lib.get(m, key);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Lib");
+ compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}});
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(1), u256(42)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(2), u256(84)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(21), u256(7)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(1)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(2)), encodeArgs(u256(84)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(21)), encodeArgs(u256(7)));
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(1), u256(21)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(2), u256(42)), encodeArgs(u256(84)));
+ ABI_CHECK(callContractFunction("set(uint256,uint256)", u256(21), u256(14)), encodeArgs(u256(7)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(1)), encodeArgs(u256(21)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(2)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get(uint256)", u256(21)), encodeArgs(u256(14)));
+}
+
+BOOST_AUTO_TEST_CASE(mapping_returns_in_library)
+{
+ char const* sourceCode = R"(
+ library Lib {
+ function choose_mapping(mapping(uint => uint) storage a, mapping(uint => uint) storage b, bool c) internal pure returns(mapping(uint=>uint) storage)
+ {
+ return c ? a : b;
+ }
+ }
+ contract Test {
+ mapping(uint => uint) a;
+ mapping(uint => uint) b;
+ function set(bool choice, uint256 key, uint256 value) public returns (uint)
+ {
+ mapping(uint => uint) storage m = Lib.choose_mapping(a, b, choice);
+ uint oldValue = m[key];
+ m[key] = value;
+ return oldValue;
+ }
+ function get(bool choice, uint256 key) public view returns (uint) {
+ return Lib.choose_mapping(a, b, choice)[key];
+ }
+ function get_a(uint256 key) public view returns (uint) {
+ return a[key];
+ }
+ function get_b(uint256 key) public view returns (uint) {
+ return b[key];
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Lib");
+ compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}});
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(1), u256(42)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(2), u256(84)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(21), u256(7)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(1), u256(10)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(2), u256(11)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(21), u256(12)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(1)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(2)), encodeArgs(u256(84)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(21)), encodeArgs(u256(7)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(1)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(2)), encodeArgs(u256(84)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(21)), encodeArgs(u256(7)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(1)), encodeArgs(u256(10)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(2)), encodeArgs(u256(11)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(21)), encodeArgs(u256(12)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(1)), encodeArgs(u256(10)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(2)), encodeArgs(u256(11)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(21)), encodeArgs(u256(12)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(1), u256(21)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(2), u256(42)), encodeArgs(u256(84)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", true, u256(21), u256(14)), encodeArgs(u256(7)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(1), u256(30)), encodeArgs(u256(10)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(2), u256(31)), encodeArgs(u256(11)));
+ ABI_CHECK(callContractFunction("set(bool,uint256,uint256)", false, u256(21), u256(32)), encodeArgs(u256(12)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(1)), encodeArgs(u256(21)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(2)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get_a(uint256)", u256(21)), encodeArgs(u256(14)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(1)), encodeArgs(u256(21)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(2)), encodeArgs(u256(42)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", true, u256(21)), encodeArgs(u256(14)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(1)), encodeArgs(u256(30)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(2)), encodeArgs(u256(31)));
+ ABI_CHECK(callContractFunction("get_b(uint256)", u256(21)), encodeArgs(u256(32)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(0)), encodeArgs(u256(0)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(1)), encodeArgs(u256(30)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(2)), encodeArgs(u256(31)));
+ ABI_CHECK(callContractFunction("get(bool,uint256)", false, u256(21)), encodeArgs(u256(32)));
+}
+
+BOOST_AUTO_TEST_CASE(mapping_returns_in_library_named)
+{
+ char const* sourceCode = R"(
+ library Lib {
+ function f(mapping(uint => uint) storage a, mapping(uint => uint) storage b) internal returns(mapping(uint=>uint) storage r)
+ {
+ r = a;
+ r[1] = 42;
+ r = b;
+ r[1] = 21;
+ }
+ }
+ contract Test {
+ mapping(uint => uint) a;
+ mapping(uint => uint) b;
+ function f() public returns (uint, uint, uint, uint, uint, uint)
+ {
+ Lib.f(a, b)[2] = 84;
+ return (a[0], a[1], a[2], b[0], b[1], b[2]);
+ }
+ function g() public returns (uint, uint, uint, uint, uint, uint)
+ {
+ mapping(uint => uint) storage m = Lib.f(a, b);
+ m[2] = 17;
+ return (a[0], a[1], a[2], b[0], b[1], b[2]);
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "Lib");
+ compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}});
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(0), u256(42), u256(0), u256(0), u256(21), u256(84)));
+ ABI_CHECK(callContractFunction("g()"), encodeArgs(u256(0), u256(42), u256(0), u256(0), u256(21), u256(17)));
+}
+
BOOST_AUTO_TEST_CASE(using_library_structs)
{
char const* sourceCode = R"(
diff --git a/test/libsolidity/syntaxTests/types/mapping/argument_external.sol b/test/libsolidity/syntaxTests/types/mapping/argument_external.sol
new file mode 100644
index 00000000..0354893f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/argument_external.sol
@@ -0,0 +1,7 @@
+contract C {
+ function f(mapping(uint => uint) storage) external pure {
+ }
+}
+// ----
+// TypeError: (28-49): Type is required to live outside storage.
+// TypeError: (28-49): Internal or recursive type is not allowed for public or external functions.
diff --git a/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol b/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol
new file mode 100644
index 00000000..395f5808
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/argument_internal.sol
@@ -0,0 +1,7 @@
+// 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
new file mode 100644
index 00000000..0360514e
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/argument_private.sol
@@ -0,0 +1,7 @@
+// 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/argument_public.sol b/test/libsolidity/syntaxTests/types/mapping/argument_public.sol
new file mode 100644
index 00000000..e4121c7f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/argument_public.sol
@@ -0,0 +1,7 @@
+contract C {
+ function f(mapping(uint => uint) storage) public pure {
+ }
+}
+// ----
+// TypeError: (28-49): Type is required to live outside storage.
+// TypeError: (28-49): Internal or recursive type is not allowed for public or external functions.
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_argument_external.sol b/test/libsolidity/syntaxTests/types/mapping/library_argument_external.sol
new file mode 100644
index 00000000..e78c6930
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_argument_external.sol
@@ -0,0 +1,6 @@
+library L {
+ function f(mapping(uint => uint) storage) external pure {
+ }
+}
+// ----
+// TypeError: (27-48): Type is required to live outside storage.
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_argument_internal.sol b/test/libsolidity/syntaxTests/types/mapping/library_argument_internal.sol
new file mode 100644
index 00000000..1228b6b6
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_argument_internal.sol
@@ -0,0 +1,4 @@
+library L {
+ function f(mapping(uint => uint) storage) internal pure {
+ }
+}
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_argument_private.sol b/test/libsolidity/syntaxTests/types/mapping/library_argument_private.sol
new file mode 100644
index 00000000..5eaff16b
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_argument_private.sol
@@ -0,0 +1,4 @@
+library L {
+ function f(mapping(uint => uint) storage) private pure {
+ }
+}
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_argument_public.sol b/test/libsolidity/syntaxTests/types/mapping/library_argument_public.sol
new file mode 100644
index 00000000..56393b68
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_argument_public.sol
@@ -0,0 +1,6 @@
+library L {
+ function f(mapping(uint => uint) storage) public pure {
+ }
+}
+// ----
+// TypeError: (27-48): Type is required to live outside storage.
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_return_external.sol b/test/libsolidity/syntaxTests/types/mapping/library_return_external.sol
new file mode 100644
index 00000000..a3bb1c32
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_return_external.sol
@@ -0,0 +1,10 @@
+library L
+{
+ function f(mapping(uint => uint) storage a, mapping(uint => uint) storage b, bool c) external pure returns(mapping(uint => uint) storage) {
+ return c ? a : b;
+ }
+}
+// ----
+// TypeError: (27-58): Type is required to live outside storage.
+// TypeError: (60-91): Type is required to live outside storage.
+// TypeError: (123-144): Type is required to live outside storage.
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_return_internal.sol b/test/libsolidity/syntaxTests/types/mapping/library_return_internal.sol
new file mode 100644
index 00000000..d0fca6bf
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_return_internal.sol
@@ -0,0 +1,6 @@
+library L
+{
+ function f(mapping(uint => uint) storage a, mapping(uint => uint) storage b, bool c) internal pure returns(mapping(uint => uint) storage) {
+ return c ? a : b;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_return_private.sol b/test/libsolidity/syntaxTests/types/mapping/library_return_private.sol
new file mode 100644
index 00000000..13c2000f
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_return_private.sol
@@ -0,0 +1,6 @@
+library L
+{
+ function f(mapping(uint => uint) storage a, mapping(uint => uint) storage b, bool c) private pure returns(mapping(uint => uint) storage) {
+ return c ? a : b;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/types/mapping/library_return_public.sol b/test/libsolidity/syntaxTests/types/mapping/library_return_public.sol
new file mode 100644
index 00000000..ac52d677
--- /dev/null
+++ b/test/libsolidity/syntaxTests/types/mapping/library_return_public.sol
@@ -0,0 +1,10 @@
+library L
+{
+ function f(mapping(uint => uint) storage a, mapping(uint => uint) storage b, bool c) public pure returns(mapping(uint => uint) storage) {
+ return c ? a : b;
+ }
+}
+// ----
+// TypeError: (27-58): Type is required to live outside storage.
+// TypeError: (60-91): Type is required to live outside storage.
+// TypeError: (121-142): Type is required to live outside storage.