Allow ABI encoding for array slices without explicit casts.

This commit is contained in:
Daniel Kirchner
2020-05-13 18:20:07 +02:00
parent e9446475bb
commit 97296d8622
9 changed files with 214 additions and 24 deletions
@@ -0,0 +1,62 @@
contract C {
function enc_packed_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(data[start:end]);
}
function enc_packed_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(bytes(data[start:end]));
}
function enc_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(data[start:end]);
}
function enc_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(bytes(data[start:end]));
}
function enc_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_packed_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function enc_packed_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function compare(bytes memory x, bytes memory y) internal {
assert(x.length == y.length);
for (uint i = 0; i < x.length; ++i)
assert(x[i] == y[i]);
}
function test_bytes() public {
bytes memory test = new bytes(3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_bytes(test, i, j), this.enc_packed_bytes_reference(test, i, j));
compare(this.enc_bytes(test, i, j), this.enc_bytes_reference(test, i, j));
}
}
function test_uint256() public {
uint256[] memory test = new uint256[](3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_uint256(test, i, j), this.enc_packed_uint256_reference(test, i, j));
compare(this.enc_uint256(test, i, j), this.enc_uint256_reference(test, i, j));
}
}
}
// ====
// EVMVersion: >homestead
// ----
// test_bytes() ->
// test_uint256() ->
@@ -0,0 +1,63 @@
pragma experimental ABIEncoderV2;
contract C {
function enc_packed_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(data[start:end]);
}
function enc_packed_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(bytes(data[start:end]));
}
function enc_bytes(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(data[start:end]);
}
function enc_bytes_reference(bytes calldata data, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(bytes(data[start:end]));
}
function enc_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encode(x[start:end]);
}
function enc_packed_uint256(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function enc_packed_uint256_reference(uint256[] calldata x, uint256 start, uint256 end) external returns (bytes memory) {
return abi.encodePacked(x[start:end]);
}
function compare(bytes memory x, bytes memory y) internal {
assert(x.length == y.length);
for (uint i = 0; i < x.length; ++i)
assert(x[i] == y[i]);
}
function test_bytes() public {
bytes memory test = new bytes(3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_bytes(test, i, j), this.enc_packed_bytes_reference(test, i, j));
compare(this.enc_bytes(test, i, j), this.enc_bytes_reference(test, i, j));
}
}
function test_uint256() public {
uint256[] memory test = new uint256[](3);
test[0] = 0x41; test[1] = 0x42; test[2] = 0x42;
for (uint i = 0; i < test.length; i++)
for (uint j = i; j <= test.length; j++)
{
compare(this.enc_packed_uint256(test, i, j), this.enc_packed_uint256_reference(test, i, j));
compare(this.enc_uint256(test, i, j), this.enc_uint256_reference(test, i, j));
}
}
}
// ====
// EVMVersion: >homestead
// ----
// test_bytes() ->
// test_uint256() ->
@@ -0,0 +1,8 @@
contract c {
bytes public b;
function f() public {
b = msg.data[:];
}
}
// ----
// TypeError: (63-74): Type bytes calldata slice is not implicitly convertible to expected type bytes storage ref.
@@ -4,4 +4,3 @@ contract C {
}
}
// ----
// TypeError: (85-91): This type cannot be encoded.