From 257b2f2d0e07ad677e49508a07d16e95a422428a Mon Sep 17 00:00:00 2001 From: wechman Date: Fri, 2 Sep 2022 12:33:19 +0200 Subject: [PATCH] Add ASTJSON custom operator test --- test/libsolidity/ASTJSON/custom_operator.json | 583 ++++++++++++++++++ test/libsolidity/ASTJSON/custom_operator.sol | 12 + .../binary_operator.sol | 27 +- .../unary_operator.sol | 17 +- 4 files changed, 615 insertions(+), 24 deletions(-) create mode 100644 test/libsolidity/ASTJSON/custom_operator.json create mode 100644 test/libsolidity/ASTJSON/custom_operator.sol diff --git a/test/libsolidity/ASTJSON/custom_operator.json b/test/libsolidity/ASTJSON/custom_operator.json new file mode 100644 index 000000000..dce6ff471 --- /dev/null +++ b/test/libsolidity/ASTJSON/custom_operator.json @@ -0,0 +1,583 @@ +{ + "absolutePath": "a", + "exportedSymbols": + { + "C": + [ + 40 + ], + "UInt": + [ + 2 + ], + "add": + [ + 24 + ] + }, + "id": 41, + "nodeType": "SourceUnit", + "nodes": + [ + { + "canonicalName": "UInt", + "id": 2, + "name": "UInt", + "nameLocation": "5:4:1", + "nodeType": "UserDefinedValueTypeDefinition", + "src": "0:19:1", + "underlyingType": + { + "id": 1, + "name": "uint8", + "nodeType": "ElementaryTypeName", + "src": "13:5:1", + "typeDescriptions": + { + "typeIdentifier": "t_uint8", + "typeString": "uint8" + } + } + }, + { + "functionList": + [ + { + "function": + { + "id": 3, + "name": "add", + "nameLocations": + [ + "27:3:1" + ], + "nodeType": "IdentifierPath", + "referencedDeclaration": 24, + "src": "27:3:1" + }, + "operator": "+" + } + ], + "global": false, + "id": 6, + "nodeType": "UsingForDirective", + "src": "20:26:1", + "typeName": + { + "id": 5, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 4, + "name": "UInt", + "nameLocations": + [ + "41:4:1" + ], + "nodeType": "IdentifierPath", + "referencedDeclaration": 2, + "src": "41:4:1" + }, + "referencedDeclaration": 2, + "src": "41:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + } + }, + { + "body": + { + "id": 23, + "nodeType": "Block", + "src": "92:28:1", + "statements": + [ + { + "expression": + { + "arguments": + [ + { + "hexValue": "30", + "id": 20, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "115:1:1", + "typeDescriptions": + { + "typeIdentifier": "t_rational_0_by_1", + "typeString": "int_const 0" + }, + "value": "0" + } + ], + "expression": + { + "argumentTypes": + [ + { + "typeIdentifier": "t_rational_0_by_1", + "typeString": "int_const 0" + } + ], + "expression": + { + "id": 18, + "name": "UInt", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 2, + "src": "105:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_type$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "type(UInt)" + } + }, + "id": 19, + "isConstant": false, + "isLValue": false, + "isPure": true, + "lValueRequested": false, + "memberLocation": "110:4:1", + "memberName": "wrap", + "nodeType": "MemberAccess", + "src": "105:9:1", + "typeDescriptions": + { + "typeIdentifier": "t_function_wrap_pure$_t_uint8_$returns$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "function (uint8) pure returns (UInt)" + } + }, + "id": 21, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "functionCall", + "lValueRequested": false, + "nameLocations": [], + "names": [], + "nodeType": "FunctionCall", + "src": "105:12:1", + "tryCall": false, + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "functionReturnParameters": 17, + "id": 22, + "nodeType": "Return", + "src": "98:19:1" + } + ] + }, + "id": 24, + "implemented": true, + "kind": "freeFunction", + "modifiers": [], + "name": "add", + "nameLocation": "56:3:1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 13, + "nodeType": "ParameterList", + "parameters": + [ + { + "constant": false, + "id": 9, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "60:4:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + }, + "typeName": + { + "id": 8, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 7, + "name": "UInt", + "nameLocations": + [ + "60:4:1" + ], + "nodeType": "IdentifierPath", + "referencedDeclaration": 2, + "src": "60:4:1" + }, + "referencedDeclaration": 2, + "src": "60:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "visibility": "internal" + }, + { + "constant": false, + "id": 12, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "66:4:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + }, + "typeName": + { + "id": 11, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 10, + "name": "UInt", + "nameLocations": + [ + "66:4:1" + ], + "nodeType": "IdentifierPath", + "referencedDeclaration": 2, + "src": "66:4:1" + }, + "referencedDeclaration": 2, + "src": "66:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "visibility": "internal" + } + ], + "src": "59:12:1" + }, + "returnParameters": + { + "id": 17, + "nodeType": "ParameterList", + "parameters": + [ + { + "constant": false, + "id": 16, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "scope": 24, + "src": "86:4:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + }, + "typeName": + { + "id": 15, + "nodeType": "UserDefinedTypeName", + "pathNode": + { + "id": 14, + "name": "UInt", + "nameLocations": + [ + "86:4:1" + ], + "nodeType": "IdentifierPath", + "referencedDeclaration": 2, + "src": "86:4:1" + }, + "referencedDeclaration": 2, + "src": "86:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "visibility": "internal" + } + ], + "src": "85:6:1" + }, + "scope": 41, + "src": "47:73:1", + "stateMutability": "pure", + "virtual": false, + "visibility": "internal" + }, + { + "abstract": false, + "baseContracts": [], + "canonicalName": "C", + "contractDependencies": [], + "contractKind": "contract", + "fullyImplemented": true, + "id": 40, + "linearizedBaseContracts": + [ + 40 + ], + "name": "C", + "nameLocation": "130:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "body": + { + "id": 38, + "nodeType": "Block", + "src": "163:44:1", + "statements": + [ + { + "expression": + { + "commonType": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + }, + "function": 24, + "id": 36, + "isConstant": false, + "isLValue": false, + "isPure": false, + "lValueRequested": false, + "leftExpression": + { + "arguments": + [ + { + "hexValue": "30", + "id": 30, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "183:1:1", + "typeDescriptions": + { + "typeIdentifier": "t_rational_0_by_1", + "typeString": "int_const 0" + }, + "value": "0" + } + ], + "expression": + { + "argumentTypes": + [ + { + "typeIdentifier": "t_rational_0_by_1", + "typeString": "int_const 0" + } + ], + "expression": + { + "id": 27, + "name": "UInt", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 2, + "src": "173:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_type$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "type(UInt)" + } + }, + "id": 29, + "isConstant": false, + "isLValue": false, + "isPure": true, + "lValueRequested": false, + "memberLocation": "178:4:1", + "memberName": "wrap", + "nodeType": "MemberAccess", + "src": "173:9:1", + "typeDescriptions": + { + "typeIdentifier": "t_function_wrap_pure$_t_uint8_$returns$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "function (uint8) pure returns (UInt)" + } + }, + "id": 31, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "functionCall", + "lValueRequested": false, + "nameLocations": [], + "names": [], + "nodeType": "FunctionCall", + "src": "173:12:1", + "tryCall": false, + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "nodeType": "BinaryOperation", + "operator": "+", + "rightExpression": + { + "arguments": + [ + { + "hexValue": "31", + "id": 34, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "number", + "lValueRequested": false, + "nodeType": "Literal", + "src": "198:1:1", + "typeDescriptions": + { + "typeIdentifier": "t_rational_1_by_1", + "typeString": "int_const 1" + }, + "value": "1" + } + ], + "expression": + { + "argumentTypes": + [ + { + "typeIdentifier": "t_rational_1_by_1", + "typeString": "int_const 1" + } + ], + "expression": + { + "id": 32, + "name": "UInt", + "nodeType": "Identifier", + "overloadedDeclarations": [], + "referencedDeclaration": 2, + "src": "188:4:1", + "typeDescriptions": + { + "typeIdentifier": "t_type$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "type(UInt)" + } + }, + "id": 33, + "isConstant": false, + "isLValue": false, + "isPure": true, + "lValueRequested": false, + "memberLocation": "193:4:1", + "memberName": "wrap", + "nodeType": "MemberAccess", + "src": "188:9:1", + "typeDescriptions": + { + "typeIdentifier": "t_function_wrap_pure$_t_uint8_$returns$_t_userDefinedValueType$_UInt_$2_$", + "typeString": "function (uint8) pure returns (UInt)" + } + }, + "id": 35, + "isConstant": false, + "isLValue": false, + "isPure": true, + "kind": "functionCall", + "lValueRequested": false, + "nameLocations": [], + "names": [], + "nodeType": "FunctionCall", + "src": "188:12:1", + "tryCall": false, + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "src": "173:27:1", + "typeDescriptions": + { + "typeIdentifier": "t_userDefinedValueType$_UInt_$2", + "typeString": "UInt" + } + }, + "id": 37, + "nodeType": "ExpressionStatement", + "src": "173:27:1" + } + ] + }, + "functionSelector": "26121ff0", + "id": 39, + "implemented": true, + "kind": "function", + "modifiers": [], + "name": "f", + "nameLocation": "147:1:1", + "nodeType": "FunctionDefinition", + "parameters": + { + "id": 25, + "nodeType": "ParameterList", + "parameters": [], + "src": "148:2:1" + }, + "returnParameters": + { + "id": 26, + "nodeType": "ParameterList", + "parameters": [], + "src": "163:0:1" + }, + "scope": 40, + "src": "138:69:1", + "stateMutability": "pure", + "virtual": false, + "visibility": "public" + } + ], + "scope": 41, + "src": "121:88:1", + "usedErrors": [] + } + ], + "src": "0:210:1" +} diff --git a/test/libsolidity/ASTJSON/custom_operator.sol b/test/libsolidity/ASTJSON/custom_operator.sol new file mode 100644 index 000000000..3c937fae3 --- /dev/null +++ b/test/libsolidity/ASTJSON/custom_operator.sol @@ -0,0 +1,12 @@ +type UInt is uint8; +using {add as +} for UInt; +function add(UInt, UInt) pure returns (UInt) { + return UInt.wrap(0); +} +contract C { + function f() public pure { + UInt.wrap(0) + UInt.wrap(1); + } +} + +// ---- diff --git a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol index 0bbd8bc43..a78f46709 100644 --- a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol +++ b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol @@ -1,25 +1,18 @@ -struct S { bool f; } +struct S { Z z; } +struct Z { int x; } -using {add as +} for S; +using {addS as +} for S; +using {addZ as +} for Z; -function add(S storage _s, S storage) pure returns (S storage) { - return _s; - _s.f = true; -} +function addS(S memory, S memory) pure returns (S memory) {} +function addZ(Z memory, Z memory) pure returns (Z memory) { revert(); } contract C { - function get() private returns (S storage) { - S storage s; - return s; - } - - function f() public { - S storage s; - get() + s; + function f() public pure { + S(Z(1)) + S(Z(2) + Z(3)); + S(Z(4)) + S(Z(5)); // Unreachable } } // ---- -// Warning 5740: (131-142): Unreachable code. -// TypeError 3464: (238-239): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. -// TypeError 3464: (311-312): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. +// Warning 5740: (310-327): Unreachable code. diff --git a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/unary_operator.sol b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/unary_operator.sol index 05a130225..c5bb25066 100644 --- a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/unary_operator.sol +++ b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/unary_operator.sol @@ -1,15 +1,18 @@ -struct S { int f; } +struct S { Z z; } +struct Z { int x; } -using {sub as -} for S; +using {unsubS as -} for S; +using {unsubZ as -} for Z; -function sub(S storage x) pure returns (S storage) { return x; } +function unsubS(S memory) pure returns (S memory) {} +function unsubZ(Z memory) pure returns (Z memory) { revert(); } contract C { - function f() public { - S storage y; - -y; + function f() public pure { + -S(-Z(1)); + -S(Z(2)); // Unreachable } } // ---- -// TypeError 3464: (181-182): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. +// Warning 5740: (283-291): Unreachable code.