From 58d19a51c6d85b65166eca97d63df51d03e43a4c Mon Sep 17 00:00:00 2001 From: Matheus Aguiar Date: Thu, 30 Jun 2022 11:27:54 -0300 Subject: [PATCH] Added few tests. --- libsolidity/parsing/Parser.cpp | 2 +- ...ultiple_custom_operators_same_contract.sol | 16 ++++++++ .../custom/operator_not_user_implemented.sol | 10 +++++ ...ator_with_different_param_return_types.sol | 26 +++++++++++++ .../operator_wrong_number_params_return.sol | 39 +++++++++++++++++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/semanticTests/operators/custom/multiple_custom_operators_same_contract.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_not_user_implemented.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_with_different_param_return_types.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_wrong_number_params_return.sol diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 96a305235..6f71de48c 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -991,7 +991,7 @@ ASTPointer Parser::parseUsingDirective() if (!util::contains(overridable, operator_)) parserError( 1885_error, - ("The operator " + string{TokenTraits::toString(operator_)} + " cannot be user-implemented. This is only possible for the folloing operators: ") + + ("The operator " + string{TokenTraits::toString(operator_)} + " cannot be user-implemented. This is only possible for the following operators: ") + util::joinHumanReadable(overridable | ranges::views::transform([](Token _t) { return string{TokenTraits::toString(_t)}; })) ); operators.emplace_back(operator_); diff --git a/test/libsolidity/semanticTests/operators/custom/multiple_custom_operators_same_contract.sol b/test/libsolidity/semanticTests/operators/custom/multiple_custom_operators_same_contract.sol new file mode 100644 index 000000000..dcfdbac3d --- /dev/null +++ b/test/libsolidity/semanticTests/operators/custom/multiple_custom_operators_same_contract.sol @@ -0,0 +1,16 @@ +type MyInt is int; +using {add as +} for MyInt; + +function add(MyInt, MyInt) pure returns (bool) { + return true; +} + +contract C { + function f() public pure returns (bool t) { + t = MyInt.wrap(2) + MyInt.wrap(7); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> true diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_not_user_implemented.sol b/test/libsolidity/syntaxTests/operators/custom/operator_not_user_implemented.sol new file mode 100644 index 000000000..e5db8ee7d --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_not_user_implemented.sol @@ -0,0 +1,10 @@ +using { + shiftL as <<, shiftR as >>, + exp as **, neg as ! +} for int256; + +// ---- +// ParserError 1885: (22-24): The operator << cannot be user-implemented. This is only possible for the following operators: |, &, ^, +, -, *, /, %, ==, !=, <, >, <=, >=, ~ +// ParserError 1885: (36-38): The operator >> cannot be user-implemented. This is only possible for the following operators: |, &, ^, +, -, *, /, %, ==, !=, <, >, <=, >=, ~ +// ParserError 1885: (51-53): The operator ** cannot be user-implemented. This is only possible for the following operators: |, &, ^, +, -, *, /, %, ==, !=, <, >, <=, >=, ~ +// ParserError 1885: (62-63): The operator ! cannot be user-implemented. This is only possible for the following operators: |, &, ^, +, -, *, /, %, ==, !=, <, >, <=, >=, ~ diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_with_different_param_return_types.sol b/test/libsolidity/syntaxTests/operators/custom/operator_with_different_param_return_types.sol new file mode 100644 index 000000000..3a2fd769c --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_with_different_param_return_types.sol @@ -0,0 +1,26 @@ +type Int is int128; +using { + add as +, sub as -, + mul as *, div as / + } for Int; + +function add(Int, Int) pure returns (Int) { + return Int.wrap(1); +} + +function sub(int128, Int) pure returns (int128) { + return 0; +} + +function mul(int128, int256) pure returns (Int) { + return Int.wrap(2); +} + +function div(Int, Int) pure returns (int256) { + return Int.wrap(3); +} +// ---- +// TypeError 3100: (42-45): The function "sub" cannot be bound to the type "Int" because the type cannot be implicitly converted to the first argument of the function ("int128"). +// TypeError 3100: (56-59): The function "mul" cannot be bound to the type "Int" because the type cannot be implicitly converted to the first argument of the function ("int128"). +// TypeError 7743: (66-69): The function "div" needs to return exactly one value of type Int to be used for the operator /. +// TypeError 6359: (365-376): Return argument type Int is not implicitly convertible to expected type (type of first return variable) int256. diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_wrong_number_params_return.sol b/test/libsolidity/syntaxTests/operators/custom/operator_wrong_number_params_return.sol new file mode 100644 index 000000000..11ca1be53 --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_wrong_number_params_return.sol @@ -0,0 +1,39 @@ +type Int is int256; + +using { + add as +, sub as -, + mul as *, div as /, + gt as >, lt as < + } for Int; + +function add(Int, Int) pure returns (Int) { + return Int.wrap(0); +} + +function sub(Int, Int, Int) pure returns (Int) { + return Int.wrap(1); +} + +function mul(Int) pure returns (Int) { + return Int.wrap(2); +} + +function div(Int x, Int y) pure { + x = y; +} + +function gt(Int, Int) pure returns (Int) { + return Int.wrap(0); +} + +function lt(Int, Int) pure returns (bool, Int) { + return (true, Int.wrap(1)); +} + +// ---- +// TypeError 1884: (43-46): The function "sub" needs to have two parameters of equal type to be used for the operator -. +// TypeError 8112: (43-46): The function "sub" needs to have exactly one parameter to be used for the operator -. +// TypeError 1884: (57-60): The function "mul" needs to have two parameters of equal type to be used for the operator *. +// TypeError 7743: (67-70): The function "div" needs to return exactly one value of type Int to be used for the operator /. +// TypeError 7743: (81-83): The function "gt" needs to return exactly one value of type bool to be used for the operator >. +// TypeError 7743: (90-92): The function "lt" needs to return exactly one value of type bool to be used for the operator <.