diff options
author | chriseth <chris@ethereum.org> | 2018-11-26 21:26:55 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-26 21:26:55 +0800 |
commit | 7921e5f0b9ff954fd08be6a4f5014f844c0cf10d (patch) | |
tree | 9e33fff524f26fad8f5c21424bc80f1adf376195 | |
parent | 9ac7c748f8b954e712ba78f1431a1b5151a4ad86 (diff) | |
parent | ac5803bf3e5dc43adf8bf64c1041603259e8a16f (diff) | |
download | dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar.gz dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar.bz2 dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar.lz dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar.xz dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.tar.zst dexon-solidity-7921e5f0b9ff954fd08be6a4f5014f844c0cf10d.zip |
Merge pull request #5486 from ethereum/allowExternalPublicOverride
Allow external public override
6 files changed, 39 insertions, 10 deletions
diff --git a/Changelog.md b/Changelog.md index ff26882d..e6f01553 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ ### 0.5.1 (unreleased) Language Features: + * Allow public functions to override external functions. Compiler Features: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index a6c23ada..d503b9ec 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -413,16 +413,14 @@ void TypeChecker::checkFunctionOverride(FunctionDefinition const& _function, Fun if (_function.visibility() != _super.visibility()) { - // visibility is enforced to be external in interfaces, but a contract can override that with public - if ( - _super.inContractKind() == ContractDefinition::ContractKind::Interface && - _function.inContractKind() != ContractDefinition::ContractKind::Interface && + // Visibility change from external to public is fine. + // Any other change is disallowed. + if (!( + _super.visibility() == FunctionDefinition::Visibility::External && _function.visibility() == FunctionDefinition::Visibility::Public - ) - return; - overrideError(_function, _super, "Overriding function visibility differs."); + )) + overrideError(_function, _super, "Overriding function visibility differs."); } - else if (_function.stateMutability() != _super.stateMutability()) overrideError( _function, @@ -433,7 +431,6 @@ void TypeChecker::checkFunctionOverride(FunctionDefinition const& _function, Fun stateMutabilityToString(_function.stateMutability()) + "\"." ); - else if (functionType != superType) overrideError(_function, _super, "Overriding function return types differ."); } diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index e591432a..e9667483 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -14062,6 +14062,21 @@ BOOST_AUTO_TEST_CASE(flipping_sign_tests) ABI_CHECK(callContractFunction("f()"), encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(external_public_override) +{ + char const* sourceCode = R"( + contract A { + function f() external returns (uint) { return 1; } + } + contract B is A { + function f() public returns (uint) { return 2; } + function g() public returns (uint) { return f(); } + } + )"; + compileAndRun(sourceCode); + ABI_CHECK(callContractFunction("f()"), encodeArgs(2)); + ABI_CHECK(callContractFunction("g()"), encodeArgs(2)); +} BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/syntaxTests/inheritance/override/external_turns_public_no_params.sol b/test/libsolidity/syntaxTests/inheritance/override/external_turns_public_no_params.sol new file mode 100644 index 00000000..3d0394f5 --- /dev/null +++ b/test/libsolidity/syntaxTests/inheritance/override/external_turns_public_no_params.sol @@ -0,0 +1,7 @@ +contract A { + function f() external pure {} +} +contract B is A { + function f() public pure { + } +} diff --git a/test/libsolidity/syntaxTests/inheritance/super_on_external.sol b/test/libsolidity/syntaxTests/inheritance/super_on_external.sol new file mode 100644 index 00000000..21f3b1c2 --- /dev/null +++ b/test/libsolidity/syntaxTests/inheritance/super_on_external.sol @@ -0,0 +1,10 @@ +contract A { + function f() external pure {} +} +contract B is A { + function f() public pure { + super.f(); + } +} +// ---- +// TypeError: (106-113): Member "f" not found or not visible after argument-dependent lookup in contract super B. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/182_equal_overload.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/182_equal_overload.sol index cb9eb3fa..6f0c7df7 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/182_equal_overload.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/182_equal_overload.sol @@ -4,4 +4,3 @@ contract C { } // ---- // DeclarationError: (17-66): Function with same name and arguments defined twice. -// TypeError: (17-66): Overriding function visibility differs. |