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: Bugfixes:
* NatSpec: Constructors and functions have consistent userdoc output. * NatSpec: Constructors and functions have consistent userdoc output.
* Inheritance: Disallow public state variables overwriting ``pure`` functions.
### 0.6.12 (unreleased) ### 0.6.12 (unreleased)

View File

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

View File

@ -584,35 +584,35 @@ void OverrideChecker::checkOverride(OverrideProxy const& _overriding, OverridePr
"Overridden " + _overriding.astNodeName() + " is here:" "Overridden " + _overriding.astNodeName() + " is here:"
); );
// This is only relevant for a function overriding a function. // Stricter mutability is always okay except when super is Payable
if (_overriding.isFunction()) if (
{ (_overriding.isFunction() || _overriding.isVariable()) &&
// Stricter mutability is always okay except when super is Payable (
if ((
_overriding.stateMutability() > _super.stateMutability() || _overriding.stateMutability() > _super.stateMutability() ||
_super.stateMutability() == StateMutability::Payable _super.stateMutability() == StateMutability::Payable
) && ) &&
_overriding.stateMutability() != _super.stateMutability() _overriding.stateMutability() != _super.stateMutability()
) )
overrideError( overrideError(
_overriding, _overriding,
_super, _super,
6959_error, 6959_error,
"Overriding function changes state mutability from \"" + "Overriding " +
stateMutabilityToString(_super.stateMutability()) + _overriding.astNodeName() +
"\" to \"" + " changes state mutability from \"" +
stateMutabilityToString(_overriding.stateMutability()) + stateMutabilityToString(_super.stateMutability()) +
"\"." "\" to \"" +
); stateMutabilityToString(_overriding.stateMutability()) +
"\"."
);
if (_overriding.unimplemented() && !_super.unimplemented()) if (_overriding.unimplemented() && !_super.unimplemented())
overrideError( overrideError(
_overriding, _overriding,
_super, _super,
4593_error, 4593_error,
"Overriding an implemented function with an unimplemented function is not allowed." "Overriding an implemented function with an unimplemented function is not allowed."
); );
}
} }
} }

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".