From 4e8275df2cf1b9874b70b7bff2bf2b821dfcf00b Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Fri, 18 Sep 2020 13:45:11 +0200 Subject: [PATCH] Enabling triggered tests and adding one new for deleting struct --- .../structs/recursive_struct_2.sol | 24 ++++++++++ .../structs/recursive_structs.sol | 2 + .../structs/struct_delete_storage.sol | 23 +++++++++ .../struct_delete_storage_with_array.sol | 47 +++++++++++++++++++ .../struct_delete_struct_in_mapping.sol | 2 + 5 files changed, 98 insertions(+) create mode 100644 test/libsolidity/semanticTests/structs/recursive_struct_2.sol create mode 100644 test/libsolidity/semanticTests/structs/struct_delete_storage.sol create mode 100644 test/libsolidity/semanticTests/structs/struct_delete_storage_with_array.sol diff --git a/test/libsolidity/semanticTests/structs/recursive_struct_2.sol b/test/libsolidity/semanticTests/structs/recursive_struct_2.sol new file mode 100644 index 000000000..65cfcf209 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/recursive_struct_2.sol @@ -0,0 +1,24 @@ +contract C { + struct S { + uint16 v; + S[] x; + } + uint8[77] padding; + S s; + constructor() { + s.v = 21; + s.x.push(); s.x.push(); s.x.push(); + s.x[0].v = 101; s.x[1].v = 102; s.x[2].v = 103; + } + function f() public returns (uint256 a, uint256 b, uint256 c, uint256 d) { + S storage sptr1 = s.x[0]; + S storage sptr2 = s.x[1]; + S storage sptr3 = s.x[2]; + uint256 slot1; uint256 slot2; uint256 slot3; + assembly { slot1 := sptr1.slot slot2 := sptr2.slot slot3 := sptr3.slot } + delete s; + assembly { a := sload(s.slot) b := sload(slot1) c := sload(slot2) d := sload(slot3) } + } +} +// ---- +// f() -> 0, 0, 0, 0 diff --git a/test/libsolidity/semanticTests/structs/recursive_structs.sol b/test/libsolidity/semanticTests/structs/recursive_structs.sol index da568a255..ce9ffcdaf 100644 --- a/test/libsolidity/semanticTests/structs/recursive_structs.sol +++ b/test/libsolidity/semanticTests/structs/recursive_structs.sol @@ -15,5 +15,7 @@ contract C { } } +// ==== +// compileViaYul: also // ---- // f() -> 1 diff --git a/test/libsolidity/semanticTests/structs/struct_delete_storage.sol b/test/libsolidity/semanticTests/structs/struct_delete_storage.sol new file mode 100644 index 000000000..a35a74258 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/struct_delete_storage.sol @@ -0,0 +1,23 @@ +contract C { + struct S { + uint256 x; + uint128 y; + uint32 z; + } + uint8 b = 23; + S s; + uint8 a = 17; + function f() public { + s.x = 42; s.y = 42; s.y = 42; + delete s; + assert(s.x == 0); + assert(s.y == 0); + assert(s.z == 0); + assert(b == 23); + assert(a == 17); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> diff --git a/test/libsolidity/semanticTests/structs/struct_delete_storage_with_array.sol b/test/libsolidity/semanticTests/structs/struct_delete_storage_with_array.sol new file mode 100644 index 000000000..b2ccfe2da --- /dev/null +++ b/test/libsolidity/semanticTests/structs/struct_delete_storage_with_array.sol @@ -0,0 +1,47 @@ +pragma experimental ABIEncoderV2; + +contract C { + struct S { + uint128 a; + uint256[] x; + uint240 b; + } + uint8 b = 23; + S s; + uint8 a = 17; + function f() public { + delete s; + s.x.push(42); s.x.push(42); s.x.push(42); + delete s; + assert(s.x.length == 0); + uint256[] storage x = s.x; + assembly { sstore(x.slot, 3) } + assert(s.x[0] == 0); + assert(s.x[1] == 0); + assert(s.x[2] == 0); + assert(b == 23); + assert(a == 17); + } + + function g() public { + delete s; + s.x.push(42); s.x.push(42); s.x.push(42); + s.a = 1; s.b = 2; + delete s.x; + assert(s.x.length == 0); + uint256[] storage x = s.x; + assembly { sstore(x.slot, 3) } + assert(s.x[0] == 0); + assert(s.x[1] == 0); + assert(s.x[2] == 0); + assert(b == 23); + assert(a == 17); + assert(s.a == 1); + assert(s.b == 2); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> +// g() -> diff --git a/test/libsolidity/semanticTests/structs/struct_delete_struct_in_mapping.sol b/test/libsolidity/semanticTests/structs/struct_delete_struct_in_mapping.sol index b5c8a2cfa..85d2c0556 100644 --- a/test/libsolidity/semanticTests/structs/struct_delete_struct_in_mapping.sol +++ b/test/libsolidity/semanticTests/structs/struct_delete_struct_in_mapping.sol @@ -14,5 +14,7 @@ contract test { } } +// ==== +// compileViaYul: also // ---- // deleteIt() -> 0