From f3fc19080aba715b44c22323fd5ffa436aa8274c Mon Sep 17 00:00:00 2001 From: Matheus Aguiar Date: Tue, 27 Jun 2023 14:52:15 -0300 Subject: [PATCH] Override mobileType of MagicType to null --- Changelog.md | 1 + libsolidity/ast/Types.h | 2 ++ .../module_from_ternary_expression.sol | 15 +++++++++++++++ .../tuple_from_ternary_expression.sol | 9 +++++++++ .../smtCheckerTests/special/msg_parens_1.sol | 4 ++-- ...odule_function_from_ternary_expression.sol | 19 +++++++++++++++++++ .../unnamed_type_tuple_in_inline_array.sol | 7 +++++++ .../unnamed_types_in_inline_array_1.sol | 2 +- .../unnamed_types_in_inline_array_2.sol | 2 +- ...word_from_ternary_with_type_expression.sol | 8 ++++++++ ...Code_from_ternary_with_type_expression.sol | 16 ++++++++++++++++ .../metaTypes/type_expression_tuple_max.sol | 6 ++++++ .../syntaxTests/metaTypes/type_max.sol | 6 ++++++ .../type_max_from_ternary_expression.sol | 8 ++++++++ .../metaTypes/type_runtimecode.sol | 9 +++++++++ ...e_runtimecode_from_ternary_expression_.sol | 14 ++++++++++++++ 16 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 test/libsolidity/semanticTests/expressions/module_from_ternary_expression.sol create mode 100644 test/libsolidity/semanticTests/expressions/tuple_from_ternary_expression.sol create mode 100644 test/libsolidity/syntaxTests/imports/module_function_from_ternary_expression.sol create mode 100644 test/libsolidity/syntaxTests/inline_arrays/unnamed_type_tuple_in_inline_array.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/max_keyword_from_ternary_with_type_expression.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/runtimeCode_from_ternary_with_type_expression.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_expression_tuple_max.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_max.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_max_from_ternary_expression.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_runtimecode.sol create mode 100644 test/libsolidity/syntaxTests/metaTypes/type_runtimecode_from_ternary_expression_.sol diff --git a/Changelog.md b/Changelog.md index 723df4583..7b19d37c0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -22,6 +22,7 @@ Compiler Features: Bugfixes: + * Code Generator: Disallow complex expressions whose results are types, built-ins, modules or some unassignable functions. The legacy code generation pipeline would not actually evaluate them, discarding any side-effects they might have. * Code Generator: Fix not entirely deterministic order of functions in unoptimized Yul output. The choice of C++ compiler in some cases would result in different (but equivalent) bytecode (especially from native binaries vs emscripten binaries) * Commandline Interface: Fix internal error when using ``--stop-after parsing`` and requesting some of the outputs that require full analysis or compilation. * Commandline Interface: It is no longer possible to specify both ``--optimize-yul`` and ``--no-optimize-yul`` at the same time. diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 1fc0a7e17..02b730863 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -1694,6 +1694,8 @@ public: Type const* typeArgument() const; + Type const* mobileType() const override { return nullptr; } + protected: std::vector> makeStackItems() const override { return {}; } private: diff --git a/test/libsolidity/semanticTests/expressions/module_from_ternary_expression.sol b/test/libsolidity/semanticTests/expressions/module_from_ternary_expression.sol new file mode 100644 index 000000000..7aa8a5caa --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/module_from_ternary_expression.sol @@ -0,0 +1,15 @@ +==== Source: A ==== +contract C { +} +==== Source: B ==== +import "A" as M; + +contract C { + function f() public pure returns (bool) { + bool flag; + ((flag = true) ? M : M).C; + return flag; + } +} +// ---- +// f() -> true diff --git a/test/libsolidity/semanticTests/expressions/tuple_from_ternary_expression.sol b/test/libsolidity/semanticTests/expressions/tuple_from_ternary_expression.sol new file mode 100644 index 000000000..c8a2ced2c --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/tuple_from_ternary_expression.sol @@ -0,0 +1,9 @@ +contract C { + function f() public pure returns (bool){ + bool flag; + ((flag = true) ? (1, 2, 3) : (3, 2, 1)); + return flag; + } +} +// ---- +// f() -> true diff --git a/test/libsolidity/smtCheckerTests/special/msg_parens_1.sol b/test/libsolidity/smtCheckerTests/special/msg_parens_1.sol index 6eb19e5e8..6368eb2dd 100644 --- a/test/libsolidity/smtCheckerTests/special/msg_parens_1.sol +++ b/test/libsolidity/smtCheckerTests/special/msg_parens_1.sol @@ -9,5 +9,5 @@ contract C { // SMTIgnoreOS: macos // SMTIgnoreCex: yes // ---- -// Warning 6328: (46-71): CHC: Assertion violation happens here. -// Warning 6328: (75-113): CHC: Assertion violation happens here. +// TypeError 9717: (90-93): Invalid mobile type in true expression. +// TypeError 3703: (96-99): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/imports/module_function_from_ternary_expression.sol b/test/libsolidity/syntaxTests/imports/module_function_from_ternary_expression.sol new file mode 100644 index 000000000..d97bbee3d --- /dev/null +++ b/test/libsolidity/syntaxTests/imports/module_function_from_ternary_expression.sol @@ -0,0 +1,19 @@ +==== Source: A ==== +function f() pure returns (uint) { + return 42; +} +==== Source: B ==== +function f() pure returns (uint) { + return 24; +} +==== Source: C ==== +import "A" as A; +import "B" as B; + +contract C { + function f(bool b) public pure returns (uint) { + return (b ? A : B).f(); + } +} +// ---- +// TypeError 1080: (C:116-125): True expression's type module "A" does not match false expression's type module "B". diff --git a/test/libsolidity/syntaxTests/inline_arrays/unnamed_type_tuple_in_inline_array.sol b/test/libsolidity/syntaxTests/inline_arrays/unnamed_type_tuple_in_inline_array.sol new file mode 100644 index 000000000..21f112c5c --- /dev/null +++ b/test/libsolidity/syntaxTests/inline_arrays/unnamed_type_tuple_in_inline_array.sol @@ -0,0 +1,7 @@ +contract C { + function f() public { + [(1, 2, 3), (4, 5, 6)]; + } +} +// ---- +// TypeError 9656: (47-69): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element. diff --git a/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_1.sol b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_1.sol index d00f818a7..b848b7b12 100644 --- a/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_1.sol +++ b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_1.sol @@ -4,4 +4,4 @@ contract C { } } // ---- -// TypeError 9656: (47-52): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element. +// TypeError 9563: (48-51): Invalid mobile type. diff --git a/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_2.sol b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_2.sol index 0abd5ca25..7f659209c 100644 --- a/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_2.sol +++ b/test/libsolidity/syntaxTests/inline_arrays/unnamed_types_in_inline_array_2.sol @@ -4,4 +4,4 @@ contract C { } } // ---- -// TypeError 9656: (47-56): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element. +// TypeError 9563: (48-55): Invalid mobile type. diff --git a/test/libsolidity/syntaxTests/metaTypes/max_keyword_from_ternary_with_type_expression.sol b/test/libsolidity/syntaxTests/metaTypes/max_keyword_from_ternary_with_type_expression.sol new file mode 100644 index 000000000..ce4a12623 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/max_keyword_from_ternary_with_type_expression.sol @@ -0,0 +1,8 @@ +contract C { + function max(bool isUint) pure public returns (uint8) { + return (isUint ? type(uint8) : type(int8)).max; + } +} +// ---- +// TypeError 9717: (98-109): Invalid mobile type in true expression. +// TypeError 3703: (112-122): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/runtimeCode_from_ternary_with_type_expression.sol b/test/libsolidity/syntaxTests/metaTypes/runtimeCode_from_ternary_with_type_expression.sol new file mode 100644 index 000000000..b2b9b2adc --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/runtimeCode_from_ternary_with_type_expression.sol @@ -0,0 +1,16 @@ +contract A { + function f() public {} +} + +contract B { + function g() public {} +} + +contract C { + function ab(bool getA) pure public returns (bytes memory) { + return (getA ? type(A) : type(B)).runtimeCode; + } +} +// ---- +// TypeError 9717: (186-193): Invalid mobile type in true expression. +// TypeError 3703: (196-203): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/type_expression_tuple_max.sol b/test/libsolidity/syntaxTests/metaTypes/type_expression_tuple_max.sol new file mode 100644 index 000000000..c5a5e82c3 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_expression_tuple_max.sol @@ -0,0 +1,6 @@ +contract C { + function max() public pure returns (uint8) { + return (type(uint8)).max; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/metaTypes/type_max.sol b/test/libsolidity/syntaxTests/metaTypes/type_max.sol new file mode 100644 index 000000000..4d3b5886e --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_max.sol @@ -0,0 +1,6 @@ +contract C { + function max() public pure returns (uint8) { + return type(uint8).max; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/metaTypes/type_max_from_ternary_expression.sol b/test/libsolidity/syntaxTests/metaTypes/type_max_from_ternary_expression.sol new file mode 100644 index 000000000..873b2e679 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_max_from_ternary_expression.sol @@ -0,0 +1,8 @@ +contract C { + function max(bool isUint) public returns (uint8) { + return (isUint ? type(uint8) : type(int8)).max; + } +} +// ---- +// TypeError 9717: (93-104): Invalid mobile type in true expression. +// TypeError 3703: (107-117): Invalid mobile type in false expression. diff --git a/test/libsolidity/syntaxTests/metaTypes/type_runtimecode.sol b/test/libsolidity/syntaxTests/metaTypes/type_runtimecode.sol new file mode 100644 index 000000000..6b054b4da --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_runtimecode.sol @@ -0,0 +1,9 @@ +contract A { +} + +contract C { + function f() public pure returns (bytes memory) { + return type(A).runtimeCode; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/metaTypes/type_runtimecode_from_ternary_expression_.sol b/test/libsolidity/syntaxTests/metaTypes/type_runtimecode_from_ternary_expression_.sol new file mode 100644 index 000000000..8fcf65480 --- /dev/null +++ b/test/libsolidity/syntaxTests/metaTypes/type_runtimecode_from_ternary_expression_.sol @@ -0,0 +1,14 @@ +contract A { +} + +contract B { +} + +contract C { + function f(bool getA) public returns (bytes memory) { + return (getA ? type(A) : type(B)).runtimeCode; + } +} +// ---- +// TypeError 9717: (126-133): Invalid mobile type in true expression. +// TypeError 3703: (136-143): Invalid mobile type in false expression.