Added few tests.

This commit is contained in:
Matheus Aguiar 2022-06-30 11:27:54 -03:00 committed by wechman
parent 3bd047f188
commit 58d19a51c6
5 changed files with 92 additions and 1 deletions

View File

@ -991,7 +991,7 @@ ASTPointer<UsingForDirective> Parser::parseUsingDirective()
if (!util::contains(overridable, operator_)) if (!util::contains(overridable, operator_))
parserError( parserError(
1885_error, 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)}; })) util::joinHumanReadable(overridable | ranges::views::transform([](Token _t) { return string{TokenTraits::toString(_t)}; }))
); );
operators.emplace_back(operator_); operators.emplace_back(operator_);

View File

@ -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

View File

@ -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: |, &, ^, +, -, *, /, %, ==, !=, <, >, <=, >=, ~

View File

@ -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.

View File

@ -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 <.