Merge pull request #4176 from sifmelcara/add/calldata-keyword

Add a new keyword, "calldata", to allow explicitly specify data location in external function's argument list
This commit is contained in:
chriseth
2018-05-30 14:42:50 +02:00
committed by GitHub
25 changed files with 155 additions and 25 deletions
@@ -6280,7 +6280,7 @@ BOOST_AUTO_TEST_CASE(unspecified_storage_v050)
}
}
)";
CHECK_ERROR(text, TypeError, "Storage location must be specified as either \"memory\" or \"storage\".");
CHECK_ERROR(text, TypeError, "Data location must be specified as either \"memory\" or \"storage\".");
}
BOOST_AUTO_TEST_CASE(storage_location_non_array_or_struct_disallowed)
@@ -6290,7 +6290,7 @@ BOOST_AUTO_TEST_CASE(storage_location_non_array_or_struct_disallowed)
function f(uint storage a) public { }
}
)";
CHECK_ERROR(text, TypeError, "Storage location can only be given for array or struct types.");
CHECK_ERROR(text, TypeError, "Data location can only be given for array or struct types.");
}
BOOST_AUTO_TEST_CASE(storage_location_non_array_or_struct_disallowed_is_not_fatal)
@@ -6302,7 +6302,7 @@ BOOST_AUTO_TEST_CASE(storage_location_non_array_or_struct_disallowed_is_not_fata
}
}
)";
CHECK_ERROR_ALLOW_MULTI(text, TypeError, (std::vector<std::string>{"Storage location can only be given for array or struct types."}));
CHECK_ERROR_ALLOW_MULTI(text, TypeError, (std::vector<std::string>{"Data location can only be given for array or struct types."}));
}
BOOST_AUTO_TEST_CASE(implicit_conversion_disallowed)
+5
View File
@@ -151,7 +151,12 @@ BOOST_AUTO_TEST_CASE(type_identifiers)
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("bool")->identifier(), "t_bool");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("bytes")->identifier(), "t_bytes_storage_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("bytes memory")->identifier(), "t_bytes_memory_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("bytes storage")->identifier(), "t_bytes_storage_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("bytes calldata")->identifier(), "t_bytes_calldata_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("string")->identifier(), "t_string_storage_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("string memory")->identifier(), "t_string_memory_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("string storage")->identifier(), "t_string_storage_ptr");
BOOST_CHECK_EQUAL(Type::fromElementaryTypeName("string calldata")->identifier(), "t_string_calldata_ptr");
ArrayType largeintArray(DataLocation::Memory, Type::fromElementaryTypeName("int128"), u256("2535301200456458802993406410752"));
BOOST_CHECK_EQUAL(largeintArray.identifier(), "t_array$_t_int128_$2535301200456458802993406410752_memory_ptr");
TypePointer stringArray = make_shared<ArrayType>(DataLocation::Storage, Type::fromElementaryTypeName("string"), u256("20"));
@@ -0,0 +1,4 @@
contract test {
function f(bytes calldata) external;
}
// ----
@@ -0,0 +1,5 @@
contract test {
function f(bytes memory) external;
}
// ----
// TypeError: (31-36): Location has to be calldata for external functions (remove the "memory" or "storage" keyword).
@@ -0,0 +1,5 @@
contract test {
function f(bytes storage) external;
}
// ----
// TypeError: (31-36): Location has to be calldata for external functions (remove the "memory" or "storage" keyword).
@@ -0,0 +1,5 @@
library test {
function f(bytes calldata) public;
}
// ----
// TypeError: (30-35): Location cannot be calldata for non-external functions (remove the "calldata" keyword).
@@ -0,0 +1,5 @@
contract test {
function f(bytes4 memory) public;
}
// ----
// TypeError: (31-37): Data location can only be given for array or struct types.
@@ -0,0 +1,5 @@
contract test {
function f(bytes calldata) internal;
}
// ----
// TypeError: (31-36): Variable cannot be declared as "calldata" (remove the "calldata" keyword).
@@ -0,0 +1,4 @@
contract test {
function f(bytes memory) internal;
}
// ----
@@ -0,0 +1,4 @@
contract test {
function f(bytes storage) internal;
}
// ----
@@ -0,0 +1,5 @@
contract test {
function f(bytes calldata) public;
}
// ----
// TypeError: (31-36): Location has to be memory for publicly visible functions (remove the "storage" or "calldata" keyword).
@@ -0,0 +1,4 @@
contract test {
function f(bytes memory) public;
}
// ----
@@ -0,0 +1,5 @@
contract test {
function f(bytes storage) public;
}
// ----
// TypeError: (31-36): Location has to be memory for publicly visible functions (remove the "storage" or "calldata" keyword).
@@ -0,0 +1,13 @@
contract test {
function f() {
uint storage a1;
bytes16 storage b1;
uint memory a2;
bytes16 memory b2;
}
}
// ----
// TypeError: (41-56): Data location can only be given for array or struct types.
// TypeError: (64-82): Data location can only be given for array or struct types.
// TypeError: (90-104): Data location can only be given for array or struct types.
// TypeError: (112-129): Data location can only be given for array or struct types.
@@ -0,0 +1,14 @@
contract test {
uint[] a;
uint[] b;
function f() public {
uint[] storage s1 = a;
uint[] memory s2 = new uint[](42);
uint[] s3 = b;
s1.push(42);
s2[3] = 12;
s3.push(42);
}
}
// ----
// Warning: (147-156): Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
@@ -2,4 +2,4 @@ contract Foo {
function f(uint[] storage constant x, uint[] memory y) internal { }
}
// ----
// TypeError: (30-55): Storage location has to be "memory" (or unspecified) for constants.
// TypeError: (30-55): Data location has to be "memory" (or unspecified) for constants.