From 4fd5bbf50b24c9457fa87a091101295b0fbde7fc Mon Sep 17 00:00:00 2001 From: Matheus Aguiar Date: Thu, 22 Jun 2023 10:56:08 -0300 Subject: [PATCH] Restrict mobileType of TypeType --- libsolidity/ast/Types.h | 1 + ...licit_conversion_of_super_in_operators.sol | 2 +- .../unnamed_types_in_inline_array_3.sol | 7 +++++++ .../metaTypes/array_type_from_ternary.sol | 8 ++++++++ .../metaTypes/contract_from_ternary.sol | 9 +++++++++ .../metaTypes/explicit_type_conversion.sol | 8 ++++++++ .../metaTypes/library_from_ternary.sol | 12 ++++++++++++ .../metaTypes/struct_from_ternary.sol | 14 ++++++++++++++ ..._expression_nested_ternary_max_keyword.sol | 8 ++++++++ .../metaTypes/type_from_ternary_condition.sol | 8 ++++++++ .../abi_decode_array_of_unitary_tuples.sol | 17 +++++++++++++++++ .../abidecode/abi_decode_enum.sol | 8 ++++++++ ..._array_length_ternary_with_max_keyword.sol | 9 +++++++++ ...ecode_invalid_argument_array_of_tuples.sol | 7 +++++++ ...invalid_argument_indexed_array_literal.sol | 7 +++++++ ...id_argument_indexed_ternary_expression.sol | 9 +++++++++ ..._argument_ternary_condition_expression.sol | 8 ++++++++ ..._ternary_expression_inside_parentheses.sol | 8 ++++++++ ...alid_argument_ternary_with_side_effect.sol | 9 +++++++++ ...abi_decode_invalid_argument_tuple_type.sol | 7 +++++++ ...ecode_invalid_argument_type_expression.sol | 7 +++++++ .../abidecode/abi_decode_member_acess.sol | 19 +++++++++++++++++++ .../abi_decode_unitary_nested_tuple.sol | 13 +++++++++++++ 23 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_3.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/array_type_from_ternary.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/contract_from_ternary.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/explicit_type_conversion.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/library_from_ternary.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/struct_from_ternary.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_expression_nested_ternary_max_keyword.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_from_ternary_condition.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_array_of_unitary_tuples.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_enum.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_length_ternary_with_max_keyword.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_of_tuples.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_array_literal.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_ternary_expression.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_condition_expression.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_expression_inside_parentheses.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_with_side_effect.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_tuple_type.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_type_expression.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_member_acess.sol create mode 100644 test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_unitary_nested_tuple.sol diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 02b730863..fd1b6a7d3 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -1598,6 +1598,7 @@ public: bool hasSimpleZeroValueInMemory() const override { solAssert(false, ""); } std::string toString(bool _withoutDataLocation) const override { return "type(" + m_actualType->toString(_withoutDataLocation) + ")"; } MemberList::MemberMap nativeMembers(ASTNode const* _currentScope) const override; + Type const* mobileType() const override { return nullptr; } BoolResult isExplicitlyConvertibleTo(Type const& _convertTo) const override; protected: diff --git a/test/libsolidity/syntaxTests/conversion/implicit_conversion_of_super_in_operators.sol b/test/libsolidity/syntaxTests/conversion/implicit_conversion_of_super_in_operators.sol index 40e9075e1..234657a22 100644 --- a/test/libsolidity/syntaxTests/conversion/implicit_conversion_of_super_in_operators.sol +++ b/test/libsolidity/syntaxTests/conversion/implicit_conversion_of_super_in_operators.sol @@ -53,4 +53,4 @@ contract C { // TypeError 7366: (480-493): Operator -= not compatible with types type(contract super C) and contract C. // TypeError 4247: (503-508): Expression has to be an lvalue. // TypeError 7366: (503-516): Operator += not compatible with types type(contract super C) and contract C. -// TypeError 1080: (527-546): True expression's type type(contract super C) does not match false expression's type contract C. +// TypeError 9717: (534-539): Invalid mobile type in true expression. diff --git a/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_3.sol b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_3.sol new file mode 100644 index 000000000..78c5493e5 --- /dev/null +++ b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_3.sol @@ -0,0 +1,7 @@ +contract C { + function f() public pure { + [uint]; + } +} +// ---- +// TypeError 9563: (53-57): Invalid mobile type. diff --git a/test/libsolidity/syntaxTests/metaTypes/array_type_from_ternary.sol b/test/libsolidity/syntaxTests/metaTypes/array_type_from_ternary.sol new file mode 100644 index 000000000..b4f11310c --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/array_type_from_ternary.sol @@ -0,0 +1,8 @@ +contract C { + function f(bool c) public { + (c ? uint[2] : uint[2])[3]; + } +} +// ---- +// TypeError 9717: (58-65): Invalid mobile type in true expression. +// TypeError 3703: (68-75): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/contract_from_ternary.sol b/test/libsolidity/syntaxTests/metaTypes/contract_from_ternary.sol new file mode 100644 index 000000000..7f4f17552 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/contract_from_ternary.sol @@ -0,0 +1,9 @@ +contract C { + function f() public pure { } + function g(bool c) public { + (c ? C : C).f(); + } +} +// ---- +// TypeError 9717: (91-92): Invalid mobile type in true expression. +// TypeError 3703: (95-96): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/explicit_type_conversion.sol b/test/libsolidity/syntaxTests/metaTypes/explicit_type_conversion.sol new file mode 100644 index 000000000..3afd4d296 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/explicit_type_conversion.sol @@ -0,0 +1,8 @@ +contract C { + function f(bool c) pure public returns (int) { + return (c ? int : int)(0); + } +} +// ---- +// TypeError 9717: (84-87): Invalid mobile type in true expression. +// TypeError 3703: (90-93): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/library_from_ternary.sol b/test/libsolidity/syntaxTests/metaTypes/library_from_ternary.sol new file mode 100644 index 000000000..e1e147a15 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/library_from_ternary.sol @@ -0,0 +1,12 @@ +library L { + function f() public pure { } +} + +contract C { + function g(bool c) public { + (c ? L : L).f(); + } +} +// ---- +// TypeError 9717: (106-107): Invalid mobile type in true expression. +// TypeError 3703: (110-111): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/struct_from_ternary.sol b/test/libsolidity/syntaxTests/metaTypes/struct_from_ternary.sol new file mode 100644 index 000000000..5d531d23f --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/struct_from_ternary.sol @@ -0,0 +1,14 @@ +struct S { + uint x; + uint y; + uint z; +} + +contract C { + function f(bool c) public pure { + S memory s = (c ? S : S)(0, 1, 2); + } +} +// ---- +// TypeError 9717: (126-127): Invalid mobile type in true expression. +// TypeError 3703: (130-131): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/type_expression_nested_ternary_max_keyword.sol b/test/libsolidity/syntaxTests/metaTypes/type_expression_nested_ternary_max_keyword.sol new file mode 100644 index 000000000..9e926738d --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_expression_nested_ternary_max_keyword.sol @@ -0,0 +1,8 @@ +contract C { + function f(bool b) public pure returns (uint) { + return type(b ? uint : uint).max; + } +} +// ---- +// TypeError 9717: (89-93): Invalid mobile type in true expression. +// TypeError 3703: (96-100): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/type_from_ternary_condition.sol b/test/libsolidity/syntaxTests/metaTypes/type_from_ternary_condition.sol new file mode 100644 index 000000000..b01564173 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_from_ternary_condition.sol @@ -0,0 +1,8 @@ +contract C { + function f(bool c) pure public { + type(c ? uint : uint); + } +} +// ---- +// TypeError 9717: (67-71): Invalid mobile type in true expression. +// TypeError 3703: (74-78): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_array_of_unitary_tuples.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_array_of_unitary_tuples.sol new file mode 100644 index 000000000..1f12fcf31 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_array_of_unitary_tuples.sol @@ -0,0 +1,17 @@ +contract C { + function f() pure public { + abi.decode("", ((uint)[2])); + abi.decode("", ((uint)[])); + abi.decode("", ((uint)[][3])); + abi.decode("", ((uint)[4][])); + abi.decode("", ((uint)[5][6])); + abi.decode("", (((uint))[5][6])); + } +} +// ---- +// Warning 6133: (52-79): Statement has no effect. +// Warning 6133: (89-115): Statement has no effect. +// Warning 6133: (125-154): Statement has no effect. +// Warning 6133: (164-193): Statement has no effect. +// Warning 6133: (203-233): Statement has no effect. +// Warning 6133: (243-275): Statement has no effect. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_enum.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_enum.sol new file mode 100644 index 000000000..a44a65739 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_enum.sol @@ -0,0 +1,8 @@ +contract C { + enum Color { red, green, blue } + function f() pure public { + abi.decode("", (Color)); + } +} +// ---- +// Warning 6133: (88-111): Statement has no effect. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_length_ternary_with_max_keyword.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_length_ternary_with_max_keyword.sol new file mode 100644 index 000000000..f958f74f3 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_length_ternary_with_max_keyword.sol @@ -0,0 +1,9 @@ +contract C { + function f() pure public { + bool x; + abi.decode("", (uint[type(x = true ? uint8 : uint8).max])); + } +} +// ---- +// TypeError 9717: (105-110): Invalid mobile type in true expression. +// TypeError 3703: (113-118): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_of_tuples.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_of_tuples.sol new file mode 100644 index 000000000..3b5dc9059 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_array_of_tuples.sol @@ -0,0 +1,7 @@ +contract C { + function f() pure public { + abi.decode("", ((uint, int)[5][6])); + } +} +// ---- +// TypeError 2614: (68-79): Indexed expression has to be a type, mapping or array (is tuple(type(uint256),type(int256))) diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_array_literal.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_array_literal.sol new file mode 100644 index 000000000..6f481e0d3 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_array_literal.sol @@ -0,0 +1,7 @@ +contract C { + function f() pure public { + abi.decode("", ([uint][2])); + } +} +// ---- +// TypeError 9563: (69-73): Invalid mobile type. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_ternary_expression.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_ternary_expression.sol new file mode 100644 index 000000000..97b15f4bb --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_indexed_ternary_expression.sol @@ -0,0 +1,9 @@ +contract C { + function f() pure public { + bool x; + abi.decode("", ((x = true ? uint : uint)[0])); + } +} +// ---- +// TypeError 9717: (96-100): Invalid mobile type in true expression. +// TypeError 3703: (103-107): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_condition_expression.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_condition_expression.sol new file mode 100644 index 000000000..6e64de723 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_condition_expression.sol @@ -0,0 +1,8 @@ +contract C { + function f() pure public { + abi.decode("", (true ? uint : uint)); + } +} +// ---- +// TypeError 9717: (75-79): Invalid mobile type in true expression. +// TypeError 3703: (82-86): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_expression_inside_parentheses.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_expression_inside_parentheses.sol new file mode 100644 index 000000000..891164812 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_expression_inside_parentheses.sol @@ -0,0 +1,8 @@ +contract C { + function f() pure public { + abi.decode("", ((true ? uint : uint))); + } +} +// ---- +// TypeError 9717: (76-80): Invalid mobile type in true expression. +// TypeError 3703: (83-87): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_with_side_effect.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_with_side_effect.sol new file mode 100644 index 000000000..62592e429 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_ternary_with_side_effect.sol @@ -0,0 +1,9 @@ +contract C { + function f() pure public { + int x; + abi.decode("", ((x = 1) > 0 ? int : int)); + } +} +// ---- +// TypeError 9717: (97-100): Invalid mobile type in true expression. +// TypeError 3703: (103-106): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_tuple_type.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_tuple_type.sol new file mode 100644 index 000000000..317244a09 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_tuple_type.sol @@ -0,0 +1,7 @@ +contract C { + function f() pure public { + abi.decode("", ((uint, int))); + } +} +// ---- +// TypeError 1039: (68-79): Argument has to be a type name. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_type_expression.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_type_expression.sol new file mode 100644 index 000000000..84640abd2 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_invalid_argument_type_expression.sol @@ -0,0 +1,7 @@ +contract C { + function f() pure public { + abi.decode("", (type(uint))); + } +} +// ---- +// TypeError 1039: (68-78): Argument has to be a type name. diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_member_acess.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_member_acess.sol new file mode 100644 index 000000000..5078fcdeb --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_member_acess.sol @@ -0,0 +1,19 @@ +library L { + struct S { int a; } + enum State { idle, running, blocked } +} + +contract D { + struct X { uint b; } + enum Color { red, green, blue } +} + +contract C { + function f() pure public { + abi.decode("", (L.S)); + abi.decode("", (L.State)); + abi.decode("", (D.X)); + abi.decode("", (D.Color)); + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_unitary_nested_tuple.sol b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_unitary_nested_tuple.sol new file mode 100644 index 000000000..8da0f0a09 --- /dev/null +++ b/test/libsolidity/syntaxTests/specialFunctions/abidecode/abi_decode_unitary_nested_tuple.sol @@ -0,0 +1,13 @@ +struct S { int a; } + +contract C { + function f() pure public { + abi.decode("", (((uint)))); + abi.decode("", ((((uint))))); + abi.decode("", (((S)))); + } +} +// ---- +// Warning 6133: (73-99): Statement has no effect. +// Warning 6133: (109-137): Statement has no effect. +// Warning 6133: (147-170): Statement has no effect.