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,35 +584,35 @@ 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 ((
// Stricter mutability is always okay except when super is Payable
if (
(_overriding.isFunction() || _overriding.isVariable()) &&
(
_overriding.stateMutability() > _super.stateMutability() ||
_super.stateMutability() == StateMutability::Payable
) &&
_overriding.stateMutability() != _super.stateMutability()
)
overrideError(
_overriding,
_super,
6959_error,
"Overriding function changes state mutability from \"" +
stateMutabilityToString(_super.stateMutability()) +
"\" to \"" +
stateMutabilityToString(_overriding.stateMutability()) +
"\"."
);
) &&
_overriding.stateMutability() != _super.stateMutability()
)
overrideError(
_overriding,
_super,
6959_error,
"Overriding " +
_overriding.astNodeName() +
" changes state mutability from \"" +
stateMutabilityToString(_super.stateMutability()) +
"\" to \"" +
stateMutabilityToString(_overriding.stateMutability()) +
"\"."
);
if (_overriding.unimplemented() && !_super.unimplemented())
overrideError(
_overriding,
_super,
4593_error,
"Overriding an implemented function with an unimplemented function is not allowed."
);
}
if (_overriding.unimplemented() && !_super.unimplemented())
overrideError(
_overriding,
_super,
4593_error,
"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".