mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Fix view/pure checker for access to base.
This commit is contained in:
parent
c3c8bc09d7
commit
2b91022b25
@ -15,6 +15,7 @@ Compiler Features:
|
|||||||
|
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
|
* View/Pure Checker: Properly detect state variable access through base class.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -389,8 +389,15 @@ void ViewPureChecker::endVisit(MemberAccess const& _memberAccess)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
{
|
||||||
|
if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(
|
||||||
|
_memberAccess.annotation().referencedDeclaration
|
||||||
|
))
|
||||||
|
if (varDecl->isStateVariable() && !varDecl->isConstant())
|
||||||
|
mutability = writes ? StateMutability::NonPayable : StateMutability::View;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
reportMutability(mutability, _memberAccess.location());
|
reportMutability(mutability, _memberAccess.location());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,4 +6,4 @@ contract Child is Parent {
|
|||||||
function foo() public returns (uint256) { return Parent.m_aMember; }
|
function foo() public returns (uint256) { return Parent.m_aMember; }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// Warning: (158-226): Function state mutability can be restricted to pure
|
// Warning: (158-226): Function state mutability can be restricted to view
|
||||||
|
@ -5,4 +5,4 @@ contract Child is Parent {
|
|||||||
function foo() public returns (uint256) { return Parent.m_aMember; }
|
function foo() public returns (uint256) { return Parent.m_aMember; }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// Warning: (83-151): Function state mutability can be restricted to pure
|
// Warning: (83-151): Function state mutability can be restricted to view
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
contract A {
|
||||||
|
uint[] x;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A {
|
||||||
|
function f() public view {
|
||||||
|
A.x.length = 2;
|
||||||
|
}
|
||||||
|
function g() public pure returns (uint) {
|
||||||
|
return A.x.length;
|
||||||
|
}
|
||||||
|
function h() public pure returns (uint) {
|
||||||
|
return A.x[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// TypeError: (87-97): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
|
||||||
|
// TypeError: (170-173): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||||
|
// TypeError: (170-180): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||||
|
// TypeError: (249-252): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||||
|
// TypeError: (249-255): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
@ -0,0 +1,10 @@
|
|||||||
|
contract A {
|
||||||
|
uint constant x = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A {
|
||||||
|
function f() public pure returns (uint) {
|
||||||
|
return A.x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
@ -0,0 +1,11 @@
|
|||||||
|
contract A {
|
||||||
|
function x() public {}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A {
|
||||||
|
function f() public view {
|
||||||
|
A.x();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// TypeError: (100-105): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
|
@ -0,0 +1,21 @@
|
|||||||
|
contract A {
|
||||||
|
struct S { uint x; }
|
||||||
|
S s;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A {
|
||||||
|
function f() public view {
|
||||||
|
A.s = A.S(2);
|
||||||
|
}
|
||||||
|
function g() public view {
|
||||||
|
A.s.x = 2;
|
||||||
|
}
|
||||||
|
function h() public pure returns (uint) {
|
||||||
|
return A.s.x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// TypeError: (107-110): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
|
||||||
|
// TypeError: (166-171): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
|
||||||
|
// TypeError: (244-247): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||||
|
// TypeError: (244-249): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
@ -0,0 +1,15 @@
|
|||||||
|
contract A {
|
||||||
|
uint x;
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A {
|
||||||
|
function f() public pure returns (uint) {
|
||||||
|
return A.x;
|
||||||
|
}
|
||||||
|
function g() public view {
|
||||||
|
A.x = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// TypeError: (107-110): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||||
|
// TypeError: (157-160): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable.
|
Loading…
Reference in New Issue
Block a user