From d0461c49fee20c5213f351bf3f1814112dcf3331 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 20 Sep 2018 10:54:19 +0200 Subject: [PATCH] Make non-payable default for conversion to address. --- libsolidity/analysis/TypeChecker.cpp | 4 +--- libsolidity/ast/Types.cpp | 4 ++-- .../external_function_type_to_address_payable.sol | 7 +++++++ .../types/address/address_to_payable_address_double.sol | 7 +++++++ .../types/address/bytes_long_to_payable_address.sol | 8 ++++++++ .../types/address/bytes_short_to_payable_address.sol | 8 ++++++++ .../types/address/bytes_to_payable_address.sol | 5 +++++ .../syntaxTests/types/address/uint_to_payable_address.sol | 5 +++++ 8 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 test/libsolidity/syntaxTests/functionTypes/external_function_type_to_address_payable.sol create mode 100644 test/libsolidity/syntaxTests/types/address/address_to_payable_address_double.sol create mode 100644 test/libsolidity/syntaxTests/types/address/bytes_long_to_payable_address.sol create mode 100644 test/libsolidity/syntaxTests/types/address/bytes_short_to_payable_address.sol create mode 100644 test/libsolidity/syntaxTests/types/address/bytes_to_payable_address.sol create mode 100644 test/libsolidity/syntaxTests/types/address/uint_to_payable_address.sol diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 3056561b0..c42a0068f 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1778,9 +1778,7 @@ bool TypeChecker::visit(FunctionCall const& _functionCall) } if (resultType->category() == Type::Category::Address) { - bool payable = true; - if (auto const* contractType = dynamic_cast(argType.get())) - payable = contractType->isPayable(); + bool payable = argType->isExplicitlyConvertibleTo(AddressType::addressPayable()); resultType = make_shared(payable ? StateMutability::Payable : StateMutability::NonPayable); } } diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 25702f194..3a98eb601 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2641,8 +2641,8 @@ bool FunctionType::operator==(Type const& _other) const bool FunctionType::isExplicitlyConvertibleTo(Type const& _convertTo) const { - if (m_kind == Kind::External && _convertTo.category() == Category::Address) - return true; + if (m_kind == Kind::External && _convertTo == AddressType::address()) + return true; return _convertTo.category() == category(); } diff --git a/test/libsolidity/syntaxTests/functionTypes/external_function_type_to_address_payable.sol b/test/libsolidity/syntaxTests/functionTypes/external_function_type_to_address_payable.sol new file mode 100644 index 000000000..adffb14b3 --- /dev/null +++ b/test/libsolidity/syntaxTests/functionTypes/external_function_type_to_address_payable.sol @@ -0,0 +1,7 @@ +contract C { + function f() public view returns (address payable) { + return address(this.f); + } +} +// ---- +// TypeError: (85-100): Return argument type address is not implicitly convertible to expected type (type of first return variable) address payable. diff --git a/test/libsolidity/syntaxTests/types/address/address_to_payable_address_double.sol b/test/libsolidity/syntaxTests/types/address/address_to_payable_address_double.sol new file mode 100644 index 000000000..1e755033d --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/address_to_payable_address_double.sol @@ -0,0 +1,7 @@ +contract C { + function f(address a) public pure returns (address payable) { + return address(address(a)); + } +} +// ---- +// TypeError: (94-113): Return argument type address is not implicitly convertible to expected type (type of first return variable) address payable. diff --git a/test/libsolidity/syntaxTests/types/address/bytes_long_to_payable_address.sol b/test/libsolidity/syntaxTests/types/address/bytes_long_to_payable_address.sol new file mode 100644 index 000000000..ef87ac558 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/bytes_long_to_payable_address.sol @@ -0,0 +1,8 @@ +contract C { + function f(bytes32 x) public pure returns (address payable) { + return address(x); + } +} +// ---- +// TypeError: (94-104): Explicit type conversion not allowed from "bytes32" to "address". +// TypeError: (94-104): Return argument type address is not implicitly convertible to expected type (type of first return variable) address payable. diff --git a/test/libsolidity/syntaxTests/types/address/bytes_short_to_payable_address.sol b/test/libsolidity/syntaxTests/types/address/bytes_short_to_payable_address.sol new file mode 100644 index 000000000..2aa602516 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/bytes_short_to_payable_address.sol @@ -0,0 +1,8 @@ +contract C { + function f(bytes10 x) public pure returns (address payable) { + return address(x); + } +} +// ---- +// TypeError: (94-104): Explicit type conversion not allowed from "bytes10" to "address". +// TypeError: (94-104): Return argument type address is not implicitly convertible to expected type (type of first return variable) address payable. diff --git a/test/libsolidity/syntaxTests/types/address/bytes_to_payable_address.sol b/test/libsolidity/syntaxTests/types/address/bytes_to_payable_address.sol new file mode 100644 index 000000000..5b6a6714d --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/bytes_to_payable_address.sol @@ -0,0 +1,5 @@ +contract C { + function f(bytes20 x) public pure returns (address payable) { + return address(x); + } +} diff --git a/test/libsolidity/syntaxTests/types/address/uint_to_payable_address.sol b/test/libsolidity/syntaxTests/types/address/uint_to_payable_address.sol new file mode 100644 index 000000000..9a33985a6 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/uint_to_payable_address.sol @@ -0,0 +1,5 @@ +contract C { + function f(uint x) public pure returns (address payable) { + return address(x); + } +}