Disallow public state variables overriding pure functions.

This commit is contained in:
chriseth 2020-07-14 20:11:01 +02:00 committed by Mathias Baumann
parent 69a596b0ff
commit d3647b13e4
4 changed files with 36 additions and 27 deletions

View File

@ -29,6 +29,7 @@ Compiler Features:
Bugfixes:
* NatSpec: Constructors and functions have consistent userdoc output.
* Inheritance: Disallow public state variables overwriting ``pure`` functions.
### 0.6.12 (unreleased)

View File

@ -310,7 +310,7 @@ of the variable:
contract A
{
function f() external pure virtual returns(uint) { return 5; }
function f() external view virtual returns(uint) { return 5; }
}
contract B is A

View File

@ -584,11 +584,10 @@ void OverrideChecker::checkOverride(OverrideProxy const& _overriding, OverridePr
"Overridden " + _overriding.astNodeName() + " is here:"
);
// This is only relevant for a function overriding a function.
if (_overriding.isFunction())
{
// Stricter mutability is always okay except when super is Payable
if ((
if (
(_overriding.isFunction() || _overriding.isVariable()) &&
(
_overriding.stateMutability() > _super.stateMutability() ||
_super.stateMutability() == StateMutability::Payable
) &&
@ -598,7 +597,9 @@ void OverrideChecker::checkOverride(OverrideProxy const& _overriding, OverridePr
_overriding,
_super,
6959_error,
"Overriding function changes state mutability from \"" +
"Overriding " +
_overriding.astNodeName() +
" changes state mutability from \"" +
stateMutabilityToString(_super.stateMutability()) +
"\" to \"" +
stateMutabilityToString(_overriding.stateMutability()) +
@ -614,7 +615,6 @@ void OverrideChecker::checkOverride(OverrideProxy const& _overriding, OverridePr
);
}
}
}
void OverrideChecker::overrideListError(
OverrideProxy const& _item,

View File

@ -0,0 +1,8 @@
abstract contract C {
function foo() external pure virtual returns (uint);
}
contract X is C {
uint public override foo;
}
// ----
// TypeError 6959: (100-124): Overriding public state variable changes state mutability from "pure" to "view".