diff --git a/test/libsolidity/semanticTests/array/calldata_array_as_argument_internal_function.sol b/test/libsolidity/semanticTests/array/calldata_array_as_argument_internal_function.sol new file mode 100644 index 000000000..21085a7d0 --- /dev/null +++ b/test/libsolidity/semanticTests/array/calldata_array_as_argument_internal_function.sol @@ -0,0 +1,19 @@ +pragma abicoder v2; +contract Test { + function f(uint256[] calldata c) internal returns (uint a, uint b) { + return (c.length, c[0]); + } + + function g(uint256[] calldata c) external returns (uint a, uint b) { + return f(c); + } + + function h(uint256[] calldata c, uint start, uint end) external returns (uint a, uint b) { + return f(c[start: end]); + } +} +// ==== +// compileViaYul: also +// ---- +// g(uint256[]): 0x20, 4, 1, 2, 3, 4 -> 4, 1 +// h(uint256[], uint256, uint256): 0x60, 1, 3, 4, 1, 2, 3, 4 -> 2, 2 diff --git a/test/libsolidity/semanticTests/array/slices/array_slice_calldata_as_argument_of_external_calls.sol b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_as_argument_of_external_calls.sol new file mode 100644 index 000000000..1f5e929d4 --- /dev/null +++ b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_as_argument_of_external_calls.sol @@ -0,0 +1,39 @@ +contract C { + function f1(bytes calldata c1, uint256 s, uint256 e, bytes calldata c2) public returns (bool) { + return keccak256(c1[s:e]) == keccak256(c2); + } + + function f2(bytes calldata c, uint256 s) public returns (uint256, bytes memory) { + return abi.decode(c[s:], (uint256, bytes)); + } + + function f3(bytes calldata c1, uint256 s, uint256 e, bytes calldata c2) public returns (bool) { + bytes memory a = abi.encode(c1[s:e]); + bytes memory b = abi.encode(c2); + if (a.length != b.length) { return false; } + for (uint256 i = 0; i < a.length; i++) { + if (a[i] != b[i]) { return false; } + } + return true; + } + + function f4(bytes calldata c1, uint256 s, uint256 e, bytes calldata c2) public returns (bool) { + bytes memory a = abi.encodePacked(c1[s:e]); + bytes memory b = abi.encodePacked(c2); + if (a.length != b.length) { return false; } + for (uint256 i = 0; i < a.length; i++) { + if (a[i] != b[i]) { return false; } + } + return true; + } +} +// ==== +// compileViaYul: also +// ---- +// f1(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcde" -> true +// f1(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcdf" -> false +// f2(bytes,uint256): 0x40, 0, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg" +// f3(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcde" -> true +// f3(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcdf" -> false +// f4(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcde" -> true +// f4(bytes,uint256,uint256,bytes): 0x80, 1, 5, 0xC0, 8, "abcdefgh", 4, "bcdf" -> false diff --git a/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_memory.sol b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_memory.sol new file mode 100644 index 000000000..6ec8c7900 --- /dev/null +++ b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_memory.sol @@ -0,0 +1,29 @@ +contract C { + function f(int[] calldata b, uint256 start, uint256 end) public returns (int) { + int[] memory m = b[start:end]; + uint len = end - start; + assert(len == m.length); + for (uint i = 0; i < len; i++) { + assert(b[start:end][i] == m[i]); + } + return [b[start:end]][0][0]; + } + + function g(int[] calldata b, uint256 start, uint256 end) public returns (int[] memory) { + return b[start:end]; + } + + function h1(int[] memory b) internal returns (int[] memory) { + return b; + } + + function h(int[] calldata b, uint256 start, uint256 end) public returns (int[] memory) { + return h1(b[start:end]); + } +} +// ==== +// compileViaYul: also +// ---- +// f(int256[], uint256, uint256): 0x60, 1, 3, 4, 1, 2, 3, 4 -> 2 +// g(int256[], uint256, uint256): 0x60, 1, 3, 4, 1, 2, 3, 4 -> 0x20, 2, 2, 3 +// h(int256[], uint256, uint256): 0x60, 1, 3, 4, 1, 2, 3, 4 -> 0x20, 2, 2, 3 diff --git a/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_storage.sol b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_storage.sol new file mode 100644 index 000000000..88ad84604 --- /dev/null +++ b/test/libsolidity/semanticTests/array/slices/array_slice_calldata_to_storage.sol @@ -0,0 +1,16 @@ +contract C { + int[] s; + function f(int[] calldata b, uint256 start, uint256 end) public returns (int) { + s = b[start:end]; + uint len = end - start; + assert(len == s.length); + for (uint i = 0; i < len; i++) { + assert(b[start:end][i] == s[i]); + } + return s[0]; + } +} +// ==== +// compileViaYul: also +// ---- +// f(int256[], uint256, uint256): 0x60, 1, 3, 4, 1, 2, 3, 4 -> 2 diff --git a/test/libsolidity/syntaxTests/array/slice/assign_to_storage.sol b/test/libsolidity/syntaxTests/array/slice/assign_to_storage.sol index 12fd82710..df259b294 100644 --- a/test/libsolidity/syntaxTests/array/slice/assign_to_storage.sol +++ b/test/libsolidity/syntaxTests/array/slice/assign_to_storage.sol @@ -5,4 +5,3 @@ contract c { } } // ---- -// TypeError 7407: (63-74): Type bytes calldata slice is not implicitly convertible to expected type bytes storage ref. diff --git a/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_convert_to_memory.sol b/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_convert_to_memory.sol index 76a360754..62cc33c65 100644 --- a/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_convert_to_memory.sol +++ b/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_convert_to_memory.sol @@ -1,7 +1,5 @@ contract C { - function f(bytes calldata x) external { - bytes memory y = x[1:2]; + function f(bytes calldata x) external pure returns (bytes memory) { + return x[1:2]; } -} -// ---- -// TypeError 9574: (65-88): Type bytes calldata slice is not implicitly convertible to expected type bytes memory. +} \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_forward.sol b/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_forward.sol index ccb70bbbd..75c42cd4e 100644 --- a/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_forward.sol +++ b/test/libsolidity/syntaxTests/array/slice/calldata_dynamic_forward.sol @@ -4,4 +4,3 @@ contract C { } } // ---- -// TypeError 9553: (79-85): Invalid type for argument in function call. Invalid implicit conversion from bytes calldata slice to bytes memory requested.