Adds syntax tests, documentation and changelog entry.

Refines comment for array utility function.
This commit is contained in:
Erik Kundt 2018-05-16 17:18:30 +02:00
parent 34b5eca1f8
commit 98d52beba3
33 changed files with 61 additions and 3 deletions

View File

@ -1,5 +1,7 @@
### 0.5.0 (unreleased) ### 0.5.0 (unreleased)
Language Features:
* General: Support ``pop()`` for storage arrays.
Breaking Changes: Breaking Changes:
* Disallow conversions between bytesX and uintY of different size. * Disallow conversions between bytesX and uintY of different size.

View File

@ -682,7 +682,7 @@ possible:
It is planned to remove this restriction in the future but currently creates It is planned to remove this restriction in the future but currently creates
some complications because of how arrays are passed in the ABI. some complications because of how arrays are passed in the ABI.
.. index:: ! array;length, length, push, !array;push .. index:: ! array;length, length, push, pop, !array;push, !array;pop
Members Members
^^^^^^^ ^^^^^^^
@ -693,6 +693,8 @@ Members
``.length`` member. This does not happen automatically when attempting to access elements outside the current length. The size of memory arrays is fixed (but dynamic, i.e. it can depend on runtime parameters) once they are created. ``.length`` member. This does not happen automatically when attempting to access elements outside the current length. The size of memory arrays is fixed (but dynamic, i.e. it can depend on runtime parameters) once they are created.
**push**: **push**:
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``push`` that can be used to append an element at the end of the array. The function returns the new length. Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``push`` that can be used to append an element at the end of the array. The function returns the new length.
**pop**:
Dynamic storage arrays and ``bytes`` (not ``string``) have a member function called ``pop`` that can be used to remove an element from the end of the array.
.. warning:: .. warning::
It is not yet possible to use arrays of arrays in external functions. It is not yet possible to use arrays of arrays in external functions.

View File

@ -840,7 +840,7 @@ void ArrayUtils::popStorageArrayElement(ArrayType const& _type) const
let length := and(div(slot_value, 2), 0x1f) let length := and(div(slot_value, 2), 0x1f)
if iszero(length) { invalid() } if iszero(length) { invalid() }
// Zero-out the suffix inlcluding the least significant byte. // Zero-out the suffix including the least significant byte.
let mask := sub(exp(0x100, sub(33, length)), 1) let mask := sub(exp(0x100, sub(33, length)), 1)
length := sub(length, 1) length := sub(length, 1)
slot_value := or(and(not(mask), slot_value), mul(length, 2)) slot_value := or(and(not(mask), slot_value), mul(length, 2))

View File

@ -73,7 +73,7 @@ public:
/// Stack pre: reference (excludes byte offset) /// Stack pre: reference (excludes byte offset)
/// Stack post: new_length /// Stack post: new_length
void incrementDynamicArraySize(ArrayType const& _type) const; void incrementDynamicArraySize(ArrayType const& _type) const;
/// Decrements the size of a dynamic array by one if length is nonzero. Returns otherwise. /// Decrements the size of a dynamic array by one if length is nonzero. Causes an invalid instruction otherwise.
/// Clears the removed data element. In case of a byte array, this might move the data. /// Clears the removed data element. In case of a byte array, this might move the data.
/// Stack pre: reference /// Stack pre: reference
/// Stack post: /// Stack post:

View File

@ -0,0 +1,7 @@
contract C {
uint[] data;
function test() public {
data.pop();
}
}
// ----

View File

@ -0,0 +1,8 @@
contract C {
uint[] data;
function test() public {
data.pop(5);
}
}
// ----
// TypeError: (65-76): Wrong argument count for function call: 1 arguments given but expected 0.

View File

@ -0,0 +1,7 @@
contract C {
bytes data;
function test() public {
data.pop();
}
}
// ----

View File

@ -0,0 +1,8 @@
contract C {
function test() public {
uint[] memory data;
data.pop();
}
}
// ----
// TypeError: (74-82): Member "pop" is not available in uint256[] memory outside of storage.

View File

@ -0,0 +1,8 @@
contract C {
uint data;
function test() public {
data.pop();
}
}
// ----
// TypeError: (63-71): Member "pop" not found or not visible after argument-dependent lookup in uint256

View File

@ -0,0 +1,8 @@
contract C {
uint[3] data;
function test() public {
data.pop();
}
}
// ----
// TypeError: (66-74): Member "pop" not found or not visible after argument-dependent lookup in uint256[3] storage ref

View File

@ -0,0 +1,8 @@
contract C {
string data;
function test() public {
data.pop();
}
}
// ----
// TypeError: (65-73): Member "pop" not found or not visible after argument-dependent lookup in string storage ref