From c0a169ca90b780d102442aca98d1c510dac47464 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 3 Aug 2018 14:32:37 +0200 Subject: Disallow assignments to mappings within tuple assignments. --- .../nameAndTypeResolution/016_assignment_to_mapping.sol | 12 ------------ .../syntaxTests/types/mapping/assignment_local.sol | 15 +++++++++++++++ .../types/mapping/assignment_state_variable.sol | 14 ++++++++++++++ .../syntaxTests/types/mapping/assignment_struct.sol | 17 +++++++++++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) delete mode 100644 test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/assignment_local.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/assignment_state_variable.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/assignment_struct.sol (limited to 'test/libsolidity') diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol deleted file mode 100644 index 27b1ea96..00000000 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/016_assignment_to_mapping.sol +++ /dev/null @@ -1,12 +0,0 @@ -contract test { - struct str { - mapping(uint=>uint) map; - } - str data; - function fun() public { - mapping(uint=>uint) storage a = data.map; - data.map = a; - } -} -// ---- -// TypeError: (172-184): Mappings cannot be assigned to. diff --git a/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol b/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol new file mode 100644 index 00000000..ba01c44a --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol @@ -0,0 +1,15 @@ +contract test { + mapping(uint=>uint) map; + function fun() public view { + mapping(uint=>uint) storage a = map; + mapping(uint=>uint) storage b = map; + b = a; + (b) = a; + (b, b) = (a, a); + } +} +// ---- +// TypeError: (176-177): Mappings cannot be assigned to. +// TypeError: (192-193): Mappings cannot be assigned to. +// TypeError: (209-210): Mappings cannot be assigned to. +// TypeError: (212-213): Mappings cannot be assigned to. diff --git a/test/libsolidity/syntaxTests/types/mapping/assignment_state_variable.sol b/test/libsolidity/syntaxTests/types/mapping/assignment_state_variable.sol new file mode 100644 index 00000000..1323afe6 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/assignment_state_variable.sol @@ -0,0 +1,14 @@ +contract test { + mapping(uint=>uint) map; + function fun() public { + mapping(uint=>uint) storage a = map; + map = a; + (map) = a; + (map, map) = (a, a); + } +} +// ---- +// TypeError: (126-129): Mappings cannot be assigned to. +// TypeError: (144-147): Mappings cannot be assigned to. +// TypeError: (163-166): Mappings cannot be assigned to. +// TypeError: (168-171): Mappings cannot be assigned to. diff --git a/test/libsolidity/syntaxTests/types/mapping/assignment_struct.sol b/test/libsolidity/syntaxTests/types/mapping/assignment_struct.sol new file mode 100644 index 00000000..b89241ed --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/assignment_struct.sol @@ -0,0 +1,17 @@ +contract test { + struct str { + mapping(uint=>uint) map; + } + str data; + function fun() public { + mapping(uint=>uint) storage a = data.map; + data.map = a; + (data.map) = a; + (data.map, data.map) = (a, a); + } +} +// ---- +// TypeError: (172-180): Mappings cannot be assigned to. +// TypeError: (195-203): Mappings cannot be assigned to. +// TypeError: (219-227): Mappings cannot be assigned to. +// TypeError: (229-237): Mappings cannot be assigned to. -- cgit v1.2.3 From 1e4b5886d61328d951fc03dc88cfc3dd722a3a00 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 3 Aug 2018 16:22:03 +0200 Subject: Allow assignments to local variables of mapping types. --- test/libsolidity/SolidityEndToEndTest.cpp | 22 ++++++++++++++++++++++ .../syntaxTests/types/mapping/assignment_local.sol | 4 ---- .../types/mapping/mapping_return_external.sol | 7 +++++++ .../types/mapping/mapping_return_internal.sol | 21 +++++++++++++++++++++ .../types/mapping/mapping_return_public.sol | 7 +++++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_return_external.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol create mode 100644 test/libsolidity/syntaxTests/types/mapping/mapping_return_public.sol (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index e9e7c93b..2ae13963 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1478,6 +1478,28 @@ BOOST_AUTO_TEST_CASE(multi_level_mapping) testContractAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0)); } +BOOST_AUTO_TEST_CASE(mapping_local_assignment) +{ + char const* sourceCode = R"( + contract test { + mapping(uint8 => uint8) m1; + mapping(uint8 => uint8) m2; + function f() public returns (uint8, uint8, uint8, uint8) { + mapping(uint8 => uint8) storage m = m1; + m[1] = 42; + + m = m2; + m[2] = 21; + + return (m1[1], m1[2], m2[1], m2[2]); + } + } + )"; + compileAndRun(sourceCode); + + ABI_CHECK(callContractFunction("f()"), encodeArgs(byte(42), byte(0), byte(0), byte(21))); +} + BOOST_AUTO_TEST_CASE(structs) { char const* sourceCode = R"( diff --git a/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol b/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol index ba01c44a..a329c91e 100644 --- a/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol +++ b/test/libsolidity/syntaxTests/types/mapping/assignment_local.sol @@ -9,7 +9,3 @@ contract test { } } // ---- -// TypeError: (176-177): Mappings cannot be assigned to. -// TypeError: (192-193): Mappings cannot be assigned to. -// TypeError: (209-210): Mappings cannot be assigned to. -// TypeError: (212-213): Mappings cannot be assigned to. diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_return_external.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_return_external.sol new file mode 100644 index 00000000..85121241 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_return_external.sol @@ -0,0 +1,7 @@ +contract C { + function f() external pure returns (mapping(uint=>uint) storage m) { + } +} +// ---- +// TypeError: (53-82): Type is required to live outside storage. +// TypeError: (53-82): Internal or recursive type is not allowed for public or external functions. diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol new file mode 100644 index 00000000..a46003f8 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_return_internal.sol @@ -0,0 +1,21 @@ +// This should be allowed in a future release. +contract C { + mapping(uint=>uint) m; + function f() internal view returns (mapping(uint=>uint) storage) { + return m; + } + function g() private view returns (mapping(uint=>uint) storage) { + return m; + } + function h() internal view returns (mapping(uint=>uint) storage r) { + r = m; + } + function i() private view returns (mapping(uint=>uint) storage r) { + (r,r) = (m,m); + } +} +// ---- +// 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. diff --git a/test/libsolidity/syntaxTests/types/mapping/mapping_return_public.sol b/test/libsolidity/syntaxTests/types/mapping/mapping_return_public.sol new file mode 100644 index 00000000..383fa797 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/mapping/mapping_return_public.sol @@ -0,0 +1,7 @@ +contract C { + function f() public pure returns (mapping(uint=>uint) storage m) { + } +} +// ---- +// TypeError: (51-80): Type is required to live outside storage. +// TypeError: (51-80): Internal or recursive type is not allowed for public or external functions. -- cgit v1.2.3 From cbae02b514d7a9dd3845f2f731caec7defa29a66 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 3 Aug 2018 17:38:30 +0200 Subject: Additional semantics test cases. --- test/libsolidity/SolidityEndToEndTest.cpp | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 2ae13963..ccb60b32 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1500,6 +1500,51 @@ BOOST_AUTO_TEST_CASE(mapping_local_assignment) ABI_CHECK(callContractFunction("f()"), encodeArgs(byte(42), byte(0), byte(0), byte(21))); } +BOOST_AUTO_TEST_CASE(mapping_local_tuple_assignment) +{ + char const* sourceCode = R"( + contract test { + mapping(uint8 => uint8) m1; + mapping(uint8 => uint8) m2; + function f() public returns (uint8, uint8, uint8, uint8) { + mapping(uint8 => uint8) storage m = m1; + m[1] = 42; + + uint8 v; + (m, v) = (m2, 21); + m[2] = v; + + return (m1[1], m1[2], m2[1], m2[2]); + } + } + )"; + compileAndRun(sourceCode); + + ABI_CHECK(callContractFunction("f()"), encodeArgs(byte(42), byte(0), byte(0), byte(21))); +} + +BOOST_AUTO_TEST_CASE(mapping_local_compound_assignment) +{ + char const* sourceCode = R"( + contract test { + mapping(uint8 => uint8) m1; + mapping(uint8 => uint8) m2; + function f() public returns (uint8, uint8, uint8, uint8) { + mapping(uint8 => uint8) storage m = m1; + m[1] = 42; + + (m = m2)[2] = 21; + + return (m1[1], m1[2], m2[1], m2[2]); + } + } + )"; + compileAndRun(sourceCode); + + ABI_CHECK(callContractFunction("f()"), encodeArgs(byte(42), byte(0), byte(0), byte(21))); +} + + BOOST_AUTO_TEST_CASE(structs) { char const* sourceCode = R"( -- cgit v1.2.3