diff options
Diffstat (limited to 'test')
4 files changed, 178 insertions, 0 deletions
diff --git a/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_fail.sol b/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_fail.sol new file mode 100644 index 00000000..0d4fded1 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_fail.sol @@ -0,0 +1,42 @@ +contract Test +{ + function uint256_to_uint256(uint256 x) internal pure returns (uint256) { return x; } + function uint256_to_string(uint256 x) internal pure returns (string memory) { return x == 0 ? "a" : "b"; } + function uint256_to_string_storage(uint256) internal pure returns (string storage); + function string_to_uint256(string memory x) internal pure returns (uint256) { return bytes(x).length; } + function string_to_string(string memory x) internal pure returns (string memory) { return x; } + + function uint256_uint256_to_uint256(uint256 x, uint256 y) internal pure returns (uint256) { return x + y; } + function uint256_uint256_to_string(uint256 x, uint256 y) internal pure returns (string memory) { return x == y ? "a" : "b"; } + function string_uint256_to_string(string memory x, uint256 y) internal pure returns (string memory) { return y == 0 ? "a" : x; } + function string_string_to_string(string memory x, string memory y) internal pure returns (string memory) { return bytes(x).length == 0 ? y : x; } + function uint256_string_to_string(uint256 x, string memory y) internal pure returns (string memory) { return x == 0 ? "a" : y; } + + function tests() internal pure + { + function (uint256) internal pure returns (uint256) var_uint256_to_uint256 = uint256_to_string; + function (uint256) internal pure returns (string memory) var_uint256_to_string = uint256_to_string_storage; + function (string memory) internal pure returns (uint256) var_string_to_uint256 = uint256_to_string; + function (string memory) internal pure returns (string memory) var_string_to_string = var_uint256_to_string; + + function (uint256, uint256) internal pure returns (uint256) var_uint256_uint256_to_uint256 = uint256_to_uint256; + function (string memory, uint256) internal pure returns (string memory) var_string_uint256_to_string = string_to_string; + function (string memory, string memory) internal pure returns (string memory) var_string_string_to_string = string_to_string; + + var_uint256_to_uint256(1); + var_uint256_to_string(2); + var_string_to_uint256("a"); + var_string_to_string("b"); + var_uint256_uint256_to_uint256(3, 4); + var_string_uint256_to_string("c", 7); + var_string_string_to_string("d", "e"); + } +} +// ---- +// TypeError: (1218-1311): Type function (uint256) pure returns (string memory) is not implicitly convertible to expected type function (uint256) pure returns (uint256). +// TypeError: (1319-1425): Type function (uint256) pure returns (string storage pointer) is not implicitly convertible to expected type function (uint256) pure returns (string memory). +// TypeError: (1433-1531): Type function (uint256) pure returns (string memory) is not implicitly convertible to expected type function (string memory) pure returns (uint256). +// TypeError: (1539-1646): Type function (uint256) pure returns (string memory) is not implicitly convertible to expected type function (string memory) pure returns (string memory). +// TypeError: (1655-1766): Type function (uint256) pure returns (uint256) is not implicitly convertible to expected type function (uint256,uint256) pure returns (uint256). +// TypeError: (1774-1893): Type function (string memory) pure returns (string memory) is not implicitly convertible to expected type function (string memory,uint256) pure returns (string memory). +// TypeError: (1901-2025): Type function (string memory) pure returns (string memory) is not implicitly convertible to expected type function (string memory,string memory) pure returns (string memory). diff --git a/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_success.sol b/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_success.sol new file mode 100644 index 00000000..f750632e --- /dev/null +++ b/test/libsolidity/syntaxTests/types/function_types/function_parameter_return_types_success.sol @@ -0,0 +1,39 @@ +contract Test +{ + function uint256_to_uint256(uint256 x) internal pure returns (uint256) { return x; } + function uint256_to_string(uint256 x) internal pure returns (string memory) { return x == 0 ? "a" : "b"; } + function string_to_uint256(string memory x) internal pure returns (uint256) { return bytes(x).length; } + function string_to_string(string memory x) internal pure returns (string memory) { return x; } + + function uint256_uint256_to_uint256(uint256 x, uint256 y) internal pure returns (uint256) { return x + y; } + function uint256_uint256_to_string(uint256 x, uint256 y) internal pure returns (string memory) { return x == y ? "a" : "b"; } + function string_uint256_to_string(string memory x, uint256 y) internal pure returns (string memory) { return y == 0 ? "a" : x; } + function string_string_to_string(string memory x, string memory y) internal pure returns (string memory) { return bytes(x).length == 0 ? y : x; } + function uint256_string_to_string(uint256 x, string memory y) internal pure returns (string memory) { return x == 0 ? "a" : y; } + + function tests() internal pure + { + function (uint256) internal pure returns (uint256) var_uint256_to_uint256 = uint256_to_uint256; + function (uint256) internal pure returns (string memory) var_uint256_to_string = uint256_to_string; + function (string memory) internal pure returns (uint256) var_string_to_uint256 = string_to_uint256; + function (string memory) internal pure returns (string memory) var_string_to_string = string_to_string; + + function (uint256, uint256) internal pure returns (uint256) var_uint256_uint256_to_uint256 = uint256_uint256_to_uint256; + function (uint256, uint256) internal pure returns (string memory) var_uint256_uint256_to_string = uint256_uint256_to_string; + function (string memory, uint256) internal pure returns (string memory) var_string_uint256_to_string = string_uint256_to_string; + function (string memory, string memory) internal pure returns (string memory) var_string_string_to_string = string_string_to_string; + function (uint256, string memory) internal pure returns (string memory) var_uint256_string_to_string = uint256_string_to_string; + + // Avoid unused variable warnings: + var_uint256_to_uint256(1); + var_uint256_to_string(2); + var_string_to_uint256("a"); + var_string_to_string("b"); + var_uint256_uint256_to_uint256(3, 4); + var_uint256_uint256_to_string(5, 6); + var_string_uint256_to_string("c", 7); + var_string_string_to_string("d", "e"); + var_uint256_string_to_string(8, "f"); + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_fail.sol b/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_fail.sol new file mode 100644 index 00000000..818d7840 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_fail.sol @@ -0,0 +1,51 @@ +contract Test +{ + function internalPureFunc(uint256 x) internal pure returns (uint256) { return x; } + function internalViewFunc(uint256 x) internal view returns (uint256) { return x; } + function internalMutableFunc(uint256 x) internal returns (uint256) { return x; } + + function externalPureFunc(uint256 x) external pure returns (uint256) { return x; } + function externalViewFunc(uint256 x) external view returns (uint256) { return x; } + function externalPayableFunc(uint256 x) external payable returns (uint256) { return x; } + function externalMutableFunc(uint256 x) external returns (uint256) { return x; } + + function funcTakesInternalPure(function(uint256) internal pure returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesInternalView(function(uint256) internal view returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesInternalMutable(function(uint256) internal returns(uint256) a) internal returns (uint256) { return a(4); } + + function funcTakesExternalPure(function(uint256) external pure returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesExternalView(function(uint256) external view returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesExternalPayable(function(uint256) external payable returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesExternalMutable(function(uint256) external returns(uint256) a) internal returns (uint256) { return a(4); } + + function tests() internal + { + funcTakesInternalPure(internalViewFunc); // view -> pure should fail + funcTakesInternalPure(internalMutableFunc); // mutable -> pure should fail + + funcTakesInternalView(internalMutableFunc); // mutable -> view should fail + + funcTakesExternalPure(this.externalViewFunc); // view -> pure should fail + funcTakesExternalPure(this.externalPayableFunc); // payable -> pure should fail + funcTakesExternalPure(this.externalMutableFunc); // mutable -> pure should fail + + funcTakesExternalView(this.externalPayableFunc); // payable -> view should fail + funcTakesExternalView(this.externalMutableFunc); // mutable -> view should fail + + funcTakesExternalPayable(this.externalPureFunc); // pure -> payable should fail + funcTakesExternalPayable(this.externalViewFunc); // view -> payable should fail + funcTakesExternalPayable(this.externalMutableFunc); // mutable -> payable should fail + } +} +// ---- +// TypeError: (1580-1596): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) view returns (uint256) to function (uint256) pure returns (uint256) requested. +// TypeError: (1653-1672): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) returns (uint256) to function (uint256) pure returns (uint256) requested. +// TypeError: (1733-1752): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) returns (uint256) to function (uint256) view returns (uint256) requested. +// TypeError: (1813-1834): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) view external returns (uint256) to function (uint256) pure external returns (uint256) requested. +// TypeError: (1891-1915): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) payable external returns (uint256) to function (uint256) pure external returns (uint256) requested. +// TypeError: (1975-1999): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) external returns (uint256) to function (uint256) pure external returns (uint256) requested. +// TypeError: (2060-2084): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) payable external returns (uint256) to function (uint256) view external returns (uint256) requested. +// TypeError: (2144-2168): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) external returns (uint256) to function (uint256) view external returns (uint256) requested. +// TypeError: (2232-2253): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) pure external returns (uint256) to function (uint256) payable external returns (uint256) requested. +// TypeError: (2316-2337): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) view external returns (uint256) to function (uint256) payable external returns (uint256) requested. +// TypeError: (2400-2424): Invalid type for argument in function call. Invalid implicit conversion from function (uint256) external returns (uint256) to function (uint256) payable external returns (uint256) requested. diff --git a/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_success.sol b/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_success.sol new file mode 100644 index 00000000..4ee515fc --- /dev/null +++ b/test/libsolidity/syntaxTests/types/function_types/function_state_mutability_success.sol @@ -0,0 +1,46 @@ +contract Test +{ + uint y; + function internalPureFunc(uint256 x) internal pure returns (uint256) { return x; } + function internalViewFunc(uint256 x) internal view returns (uint256) { return x + y; } + function internalMutableFunc(uint256 x) internal returns (uint256) { y = x; return x; } + + function externalPureFunc(uint256 x) external pure returns (uint256) { return x; } + function externalViewFunc(uint256 x) external view returns (uint256) { return x + y; } + function externalPayableFunc(uint256 x) external payable returns (uint256) { return x + y; } + function externalMutableFunc(uint256 x) external returns (uint256) { y = x; return x; } + + function funcTakesInternalPure(function(uint256) internal pure returns(uint256) a) internal pure returns (uint256) { return a(4); } + function funcTakesInternalView(function(uint256) internal view returns(uint256) a) internal view returns (uint256) { return a(4); } + function funcTakesInternalMutable(function(uint256) internal returns(uint256) a) internal returns (uint256) { return a(4); } + + function funcTakesExternalPure(function(uint256) external pure returns(uint256) a) internal pure returns (uint256) { return a(4); } + function funcTakesExternalView(function(uint256) external view returns(uint256) a) internal view returns (uint256) { return a(4); } + function funcTakesExternalPayable(function(uint256) external payable returns(uint256) a) internal returns (uint256) { return a(4); } + function funcTakesExternalMutable(function(uint256) external returns(uint256) a) internal returns (uint256) { return a(4); } + + function tests() internal + { + funcTakesInternalPure(internalPureFunc); + + funcTakesInternalView(internalPureFunc); + funcTakesInternalView(internalViewFunc); + + funcTakesInternalMutable(internalPureFunc); + funcTakesInternalMutable(internalViewFunc); + funcTakesInternalMutable(internalMutableFunc); + + funcTakesExternalPure(this.externalPureFunc); + + funcTakesExternalView(this.externalPureFunc); + funcTakesExternalView(this.externalViewFunc); + + funcTakesExternalPayable(this.externalPayableFunc); + + funcTakesExternalMutable(this.externalPureFunc); + funcTakesExternalMutable(this.externalViewFunc); + funcTakesExternalMutable(this.externalPayableFunc); + funcTakesExternalMutable(this.externalMutableFunc); + } +} +// ---- |