From 4d38df6920119267a31ebf97b548e8195c59c486 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Thu, 13 Jun 2019 15:51:15 +0200 Subject: [PATCH] Set state mutability of function type members ``gas`` and ``value`` to pure. --- Changelog.md | 1 + libsolidity/ast/Types.cpp | 4 ++-- .../viewPureChecker/gas_value_without_call.sol | 13 +++++++++++++ .../viewPureChecker/gas_with_call_nonpayable.sol | 16 ++++++++++++++++ .../viewPureChecker/staticcall_gas_view.sol | 11 +++++++++++ .../value_with_call_nonpayable.sol | 16 ++++++++++++++++ 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/gas_value_without_call.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/gas_with_call_nonpayable.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/staticcall_gas_view.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/value_with_call_nonpayable.sol diff --git a/Changelog.md b/Changelog.md index deed908a4..243284272 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ Compiler Features: Bugfixes: * Yul / Inline Assembly Parser: Disallow trailing commas in function call arguments. + * Set state mutability of the function type members ``gas`` and ``value`` to pure (while their return type inherits state mutability from the function type). Build System: diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 6877ecbd6..65d9abf06 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2912,7 +2912,7 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con strings(1, ""), Kind::SetValue, false, - StateMutability::NonPayable, + StateMutability::Pure, nullptr, m_gasSet, m_valueSet @@ -2929,7 +2929,7 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con strings(1, ""), Kind::SetGas, false, - StateMutability::NonPayable, + StateMutability::Pure, nullptr, m_gasSet, m_valueSet diff --git a/test/libsolidity/syntaxTests/viewPureChecker/gas_value_without_call.sol b/test/libsolidity/syntaxTests/viewPureChecker/gas_value_without_call.sol new file mode 100644 index 000000000..2df3bbe4f --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/gas_value_without_call.sol @@ -0,0 +1,13 @@ +contract C { + function f() external payable {} + function g(address a) external pure { + a.call.value(42); + a.call.gas(42); + a.staticcall.gas(42); + a.delegatecall.gas(42); + } + function h() external view { + this.f.value(42); + this.f.gas(42); + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/gas_with_call_nonpayable.sol b/test/libsolidity/syntaxTests/viewPureChecker/gas_with_call_nonpayable.sol new file mode 100644 index 000000000..0a58a516c --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/gas_with_call_nonpayable.sol @@ -0,0 +1,16 @@ +contract C { + function f(address a) external view returns (bool success) { + (success,) = a.call.gas(42)(""); + } + function g(address a) external view returns (bool success) { + (success,) = a.call.gas(42)(""); + } + function h() external payable {} + function i() external view { + this.h.gas(42)(); + } +} +// ---- +// TypeError: (90-108): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. +// TypeError: (190-208): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. +// TypeError: (279-295): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/staticcall_gas_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/staticcall_gas_view.sol new file mode 100644 index 000000000..6f8b31bfc --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/staticcall_gas_view.sol @@ -0,0 +1,11 @@ +contract C { + function f() external view {} + function test(address a) external view returns (bool status) { + // This used to incorrectly raise an error about violating the view mutability. + (status,) = a.staticcall.gas(42)(""); + this.f.gas(42)(); + } +} +// ==== +// EVMVersion: >=byzantium +// ---- diff --git a/test/libsolidity/syntaxTests/viewPureChecker/value_with_call_nonpayable.sol b/test/libsolidity/syntaxTests/viewPureChecker/value_with_call_nonpayable.sol new file mode 100644 index 000000000..cf5d885c9 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/value_with_call_nonpayable.sol @@ -0,0 +1,16 @@ +contract C { + function f(address a) external view returns (bool success) { + (success,) = a.call.value(42)(""); + } + function g(address a) external view returns (bool success) { + (success,) = a.call.value(42)(""); + } + function h() external payable {} + function i() external view { + this.h.value(42)(); + } +} +// ---- +// TypeError: (90-110): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. +// TypeError: (192-212): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. +// TypeError: (283-301): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.