From 7d12eb574564a68f355613f60ca95cb340996d6e Mon Sep 17 00:00:00 2001 From: wechman Date: Thu, 28 Jul 2022 08:39:10 +0200 Subject: [PATCH] Add user operator test cases --- .../custom/operator_bound_for_two_types.sol | 29 +++++++++++++ .../operator_bound_inside_contracts.sol | 43 +++++++++++++++++++ ...ound_to_function_imported_transitively.sol | 28 ++++++++++++ ...erator_all_types_bound_inside_contract.sol | 12 ++++++ ...tract_level_to_contract_level_function.sol | 12 ++++++ .../custom/operator_bound_to_all_types.sol | 8 ++++ ...rator_bound_to_contract_level_function.sol | 12 ++++++ 7 files changed, 144 insertions(+) create mode 100644 test/libsolidity/semanticTests/operators/custom/operator_bound_for_two_types.sol create mode 100644 test/libsolidity/semanticTests/operators/custom/operator_bound_inside_contracts.sol create mode 100644 test/libsolidity/semanticTests/operators/custom/operator_bound_to_function_imported_transitively.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_all_types_bound_inside_contract.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_bound_on_contract_level_to_contract_level_function.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_bound_to_all_types.sol create mode 100644 test/libsolidity/syntaxTests/operators/custom/operator_bound_to_contract_level_function.sol diff --git a/test/libsolidity/semanticTests/operators/custom/operator_bound_for_two_types.sol b/test/libsolidity/semanticTests/operators/custom/operator_bound_for_two_types.sol new file mode 100644 index 000000000..f933ceb08 --- /dev/null +++ b/test/libsolidity/semanticTests/operators/custom/operator_bound_for_two_types.sol @@ -0,0 +1,29 @@ +type SmallInt is int; +type BigInt is int; + +using {add1 as +} for SmallInt; +using {add2 as +} for BigInt; + +function add1(SmallInt, SmallInt) pure returns (SmallInt) { + return SmallInt.wrap(1); +} + +function add2(BigInt, BigInt) pure returns (BigInt) { + return BigInt.wrap(2); +} + +contract C { + function f() public pure returns (SmallInt) { + return SmallInt.wrap(0) + SmallInt.wrap(0); + } + + function g() public pure returns (BigInt) { + return BigInt.wrap(0) + BigInt.wrap(0); + } +} + +// ==== +// compileViaYul: also +// ---- +// f() -> 1 +// g() -> 2 diff --git a/test/libsolidity/semanticTests/operators/custom/operator_bound_inside_contracts.sol b/test/libsolidity/semanticTests/operators/custom/operator_bound_inside_contracts.sol new file mode 100644 index 000000000..3c32d0a4a --- /dev/null +++ b/test/libsolidity/semanticTests/operators/custom/operator_bound_inside_contracts.sol @@ -0,0 +1,43 @@ +type Int is int; + +function add1(Int, Int) pure returns (Int) { + return Int.wrap(1); +} + +function add2(Int, Int) pure returns (Int) { + return Int.wrap(2); +} + +contract C1 { + using {add1 as +} for Int; + + function f() public returns (Int) { + return Int.wrap(0) + Int.wrap(0); + } +} + +contract C2 { + using {add2 as +} for Int; + + function f() public returns (Int) { + return Int.wrap(0) + Int.wrap(0); + } +} + +contract C { + function test1() public returns (Int) { + C1 c = new C1(); + return c.f(); + } + + function test2() public returns (Int) { + C2 c = new C2(); + return c.f(); + } +} + +// ==== +// compileViaYul: also +// ---- +// test1() -> 1 +// test2() -> 2 diff --git a/test/libsolidity/semanticTests/operators/custom/operator_bound_to_function_imported_transitively.sol b/test/libsolidity/semanticTests/operators/custom/operator_bound_to_function_imported_transitively.sol new file mode 100644 index 000000000..e4c3f5b22 --- /dev/null +++ b/test/libsolidity/semanticTests/operators/custom/operator_bound_to_function_imported_transitively.sol @@ -0,0 +1,28 @@ +==== Source: a.sol ==== +pragma abicoder v2; +library L { + type Int is int128; + + function add(Int, Int) pure public returns (Int) { + return Int.wrap(7); + } +} +==== Source: b.sol ==== +pragma abicoder v2; +import "a.sol" as a; +==== Source: c.sol ==== +pragma abicoder v2; +import "b.sol" as b; + +contract C { + using {b.a.L.add as +} for b.a.L.Int; + + function f() pure public returns (b.a.L.Int) { + return b.a.L.Int.wrap(0) + b.a.L.Int.wrap(0); + } +} + +// ==== +// compileViaYul: also +// ---- +// f() -> 7 diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_all_types_bound_inside_contract.sol b/test/libsolidity/syntaxTests/operators/custom/operator_all_types_bound_inside_contract.sol new file mode 100644 index 000000000..c286aca60 --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_all_types_bound_inside_contract.sol @@ -0,0 +1,12 @@ +type Int is int; + +function add(Int, Int) returns (Int) { + return Int.wrap(0); +} + +contract C { + using {add as +} for *; +} + +// ---- +// SyntaxError 3349: (101-124): The type has to be specified explicitly when attaching specific functions. diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_bound_on_contract_level_to_contract_level_function.sol b/test/libsolidity/syntaxTests/operators/custom/operator_bound_on_contract_level_to_contract_level_function.sol new file mode 100644 index 000000000..4cea4232c --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_bound_on_contract_level_to_contract_level_function.sol @@ -0,0 +1,12 @@ +type Int is int; + +contract C { + using {add as +} for Int; + + function add(Int, Int) public returns (Int) { + return 0; + } +} + +// ---- +// TypeError 4167: (42-45): Only file-level functions and library functions can be bound to a type in a "using" statement diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_all_types.sol b/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_all_types.sol new file mode 100644 index 000000000..2396b4ef7 --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_all_types.sol @@ -0,0 +1,8 @@ +using {add as +} for *; + +function add(int, int) returns (int) { + return 0; +} + +// ---- +// SyntaxError 8118: (0-23): The type has to be specified explicitly at file level (cannot use '*'). diff --git a/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_contract_level_function.sol b/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_contract_level_function.sol new file mode 100644 index 000000000..e8728ea27 --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/custom/operator_bound_to_contract_level_function.sol @@ -0,0 +1,12 @@ +type Int is int; + +using {C.add as +} for Int; + +contract C { + function add(Int, Int) public returns (Int) { + return 0; + } +} + +// ---- +// TypeError 4167: (25-30): Only file-level functions and library functions can be bound to a type in a "using" statement