Fix view/pure checker for access to base.

This commit is contained in:
chriseth 2019-07-03 11:18:42 +02:00
parent c3c8bc09d7
commit 2b91022b25
9 changed files with 88 additions and 2 deletions

View File

@ -15,6 +15,7 @@ Compiler Features:
Bugfixes:
* View/Pure Checker: Properly detect state variable access through base class.

View File

@ -389,8 +389,15 @@ void ViewPureChecker::endVisit(MemberAccess const& _memberAccess)
break;
}
default:
{
if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(
_memberAccess.annotation().referencedDeclaration
))
if (varDecl->isStateVariable() && !varDecl->isConstant())
mutability = writes ? StateMutability::NonPayable : StateMutability::View;
break;
}
}
reportMutability(mutability, _memberAccess.location());
}

View File

@ -6,4 +6,4 @@ contract Child is Parent {
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

View File

@ -5,4 +5,4 @@ contract Child is Parent {
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

View File

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

View File

@ -0,0 +1,10 @@
contract A {
uint constant x = 2;
}
contract B is A {
function f() public pure returns (uint) {
return A.x;
}
}
// ----

View File

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

View File

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

View File

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