Skip to content

Commit c5ce9c0

Browse files
committed
Propagate purity information for member access to foreign pure variables
1 parent 80d49f3 commit c5ce9c0

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

libsolidity/analysis/TypeChecker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,14 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess)
31583158

31593159
annotation.isPure = isPure;
31603160
}
3161+
if (
3162+
VariableDeclaration const* varDecl = dynamic_cast<decltype(varDecl)>(annotation.referencedDeclaration);
3163+
!annotation.isPure.set() &&
3164+
varDecl &&
3165+
varDecl->isConstant()
3166+
) {
3167+
annotation.isPure = true;
3168+
}
31613169

31623170
if (auto magicType = dynamic_cast<MagicType const*>(exprType))
31633171
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
==== Source: A.sol ====
2+
import "B.sol";
3+
4+
uint256 constant A = B.VAL + 1;
5+
6+
==== Source: B.sol ====
7+
import "A.sol";
8+
9+
library B {
10+
uint256 constant VAL = A + 1;
11+
}
12+
13+
// ----
14+
// TypeError 6161: (B.sol:33-61): The value of the constant VAL has a cyclic dependency via A.
15+
// TypeError 6161: (A.sol:17-47): The value of the constant A has a cyclic dependency via VAL.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
==== Source: A.sol ====
2+
import "B.sol";
3+
4+
uint256 constant A = B + 1;
5+
6+
==== Source: B.sol ====
7+
import "A.sol";
8+
9+
uint256 constant B = A + 1;
10+
11+
// ----
12+
// TypeError 6161: (B.sol:17-43): The value of the constant B has a cyclic dependency via A.
13+
// TypeError 6161: (A.sol:17-43): The value of the constant A has a cyclic dependency via B.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library A {
2+
uint256 constant VAL = B.VAL + 1;
3+
}
4+
5+
library B {
6+
uint256 constant VAL = A.VAL + 1;
7+
}
8+
9+
// ----
10+
// TypeError 6161: (16-48): The value of the constant VAL has a cyclic dependency via VAL.
11+
// TypeError 6161: (69-101): The value of the constant VAL has a cyclic dependency via VAL.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
uint256 constant MAX = 1;
2+
3+
library Internal {
4+
uint256 internal constant SCALE = 100;
5+
}
6+
7+
contract Usage {
8+
uint256 private constant LIMIT = MAX * Internal.SCALE;
9+
}
10+
11+
// ====

0 commit comments

Comments
 (0)