Initial introduction of array slices with partial implementation for dynamic calldata arrays.

This commit is contained in:
Daniel Kirchner
2019-09-13 10:57:53 +02:00
parent 50ce3b0ac8
commit 4782c800ec
50 changed files with 659 additions and 105 deletions
@@ -0,0 +1,5 @@
contract C {
function f(bytes calldata x) external pure {
x[1:2];
}
}
@@ -0,0 +1,7 @@
contract C {
function f(bytes memory x) public pure {
x[1:2];
}
}
// ----
// TypeError: (66-72): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,8 @@
contract C {
bytes x;
function f() public view {
x[1:2];
}
}
// ----
// TypeError: (65-71): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,5 @@
contract C {
function f(uint256[] calldata x) external pure {
x[1:2];
}
}
@@ -0,0 +1,9 @@
contract C {
function f(uint256[] calldata x) external pure {
x[1:2][0];
x[1:][0];
x[1:][1:2][0];
x[1:2][1:][0];
}
}
// ----
@@ -0,0 +1,7 @@
contract C {
function f(bytes calldata x) external {
bytes memory y = x[1:2];
}
}
// ----
// TypeError: (65-88): Type bytes calldata slice is not implicitly convertible to expected type bytes memory.
@@ -0,0 +1,7 @@
contract C {
function f(uint256[] calldata x) external pure {
abi.encode(x[1:2]);
}
}
// ----
// TypeError: (85-91): This type cannot be encoded.
@@ -0,0 +1,7 @@
contract C {
function f(bytes calldata x) external {
return this.f(x[1:2]);
}
}
// ----
// TypeError: (79-85): Invalid type for argument in function call. Invalid implicit conversion from bytes calldata slice to bytes memory requested.
@@ -0,0 +1,7 @@
contract C {
function f(uint256[42] calldata x) external pure {
x[1:2];
}
}
// ----
// TypeError: (76-82): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,7 @@
contract C {
function f(uint256[] memory x) public pure {
x[1:2];
}
}
// ----
// TypeError: (70-76): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,7 @@
contract C {
function f(uint256[42] memory x) public pure {
x[1:2];
}
}
// ----
// TypeError: (72-78): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,7 @@
contract C {
function f() public pure {
1[1:];
}
}
// ----
// TypeError: (52-57): Index range access is only possible for arrays and array slices.
@@ -0,0 +1,8 @@
contract C {
function f() public pure {
bytes memory y;
y[1:2];
}
}
// ----
// TypeError: (76-82): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,8 @@
contract C {
function f() public pure {
string memory y;
y[1:2];
}
}
// ----
// TypeError: (77-83): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,7 @@
contract C {
function f() public pure {
""[1:];
}
}
// ----
// TypeError: (52-58): Index range access is only possible for arrays and array slices.
@@ -0,0 +1,8 @@
contract C {
uint256[] x;
function f() public view {
x[1:2];
}
}
// ----
// TypeError: (69-75): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,8 @@
contract C {
uint256[42] x;
function f() public view {
x[1:2];
}
}
// ----
// TypeError: (71-77): Index range access is only supported for dynamic calldata arrays.
@@ -0,0 +1,8 @@
contract C {
function f(bool cond, bytes calldata x) external pure {
bytes1 a = x[cond ? 1 : 2]; a;
abi.decode(x[cond ? 1 : 2 : ], (uint256));
abi.decode(x[cond ? 1 : 2 : cond ? 3 : 4], (uint256));
}
}
// ----
@@ -0,0 +1,14 @@
contract C {
function f() public pure {
uint[] memory x;
uint[1:](x);
uint[1:2](x);
uint[][1:](x);
}
}
// ----
// TypeError: (77-85): Types cannot be sliced.
// TypeError: (77-88): Explicit type conversion not allowed from "uint256[] memory" to "uint256".
// TypeError: (98-107): Types cannot be sliced.
// TypeError: (98-110): Explicit type conversion not allowed from "uint256[] memory" to "uint256".
// TypeError: (120-130): Types cannot be sliced.
@@ -0,0 +1,12 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint256[][] calldata x) external pure {
x[0][1:2];
x[1:2][1:2];
uint256 a = x[1:2][1:2][1:][3:][0][2];
uint256 b = x[1:][3:4][1][1:][2:3][0];
a; b;
}
}
// ----
// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
@@ -0,0 +1,6 @@
contract C {
function f(uint256[] calldata x) external pure {
x[:][:10];
}
}
// ----
@@ -0,0 +1,14 @@
contract C {
function f() public pure {
uint[][1:] memory x;
uint[][1:2] memory x;
uint[1:] memory x;
uint[1:2] memory x;
}
}
// ----
// ParserError: (52-62): Expected array length expression.
// ParserError: (81-92): Expected array length expression.
// ParserError: (111-119): Expected array length expression.
// ParserError: (138-147): Expected array length expression.
@@ -4,5 +4,5 @@ contract C {
}
}
// ----
// TypeError: (57-61): Invalid type for argument in function call. Invalid implicit conversion from type(uint256) to bytes memory requested.
// TypeError: (57-61): The first argument to "abi.decode" must be implicitly convertible to bytes memory or bytes calldata, but is of type type(uint256).
// TypeError: (63-67): The second argument to "abi.decode" has to be a tuple of types.