Skip to content

Commit

Permalink
fix: error checks for unknown variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lydiagarms committed Jul 24, 2024
1 parent d7aaba2 commit 33c2e09
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/transformers/visitors/checks/accessedVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default {
if (indicator instanceof StateVariableIndicator) indicator.updateAccessed(path);
return;
}

// if this state is on the rhs AND isn't incremented OR is in an incrementation, but its not being incremented:
if (
rightAncestor &&
Expand Down Expand Up @@ -153,10 +154,13 @@ export default {
}
// We need to ouput an error if a partitioned state is being accessed.
//This seems difficult to support because we need the sum of the value of every commitment, how do we enforce that the prover inputs every commitment to the circuit?
else if (rightAncestor){
else if (rightAncestor &&
referencedBinding.isPartitioned
&& (lhsNode.name !== node.name || !state.inIncrementation)
){
const indicator = scope.getReferencedIndicator(node);
if (indicator instanceof StateVariableIndicator) throw new TODOError(
`A partitioned state variable cannot be accessed. Only incrementations/ decrementations of form +=, -= are allowed.`,
`A partitioned state variable cannot be accessed. Only incrementations/ decrementations such as +=, -= are allowed.`,
node,
);
}
Expand Down
15 changes: 7 additions & 8 deletions src/transformers/visitors/checks/incrementedVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,27 +435,26 @@ export default {
if (
nameMatch &&
precedingOperator[index + 1]?.includes('+') && // we have ... + a + ...
precedingOperator[index]?.includes('+') // otherwise we have a = b - a
(index ===0)
//precedingOperator[index]?.includes('+') // otherwise we have a = b - a
) {
discoveredLHS += 1;
isIncremented = { incremented: true, decremented: false };
}

// a = a - something
if (
nameMatch &&
precedingOperator[index + 1]?.includes('-') && // we have ... + a - ...
precedingOperator[index]?.includes('+') // otherwise we have a = b - a
(index ===0)
//precedingOperator[index]?.includes('+') // otherwise we have a = b - a
) {
discoveredLHS += 1;
isIncremented = { incremented: true, decremented: true };
}
// a = something - a

if (
nameMatch &&
precedingOperator[index]?.includes('-') // we have a = b - a
nameMatch
) {
discoveredLHS -= 1;
discoveredLHS += 1;
}
// if none, go to the next operand
if (operand.indexExpression?.expression?.name === 'msg')
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/Assign-type-conversion.zol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ contract Assign {
}

function remove(uint256 remval) public {
b -= uint(uint128(remval));
b += uint(uint128(remval));
}
}
7 changes: 2 additions & 5 deletions test/contracts/KnownUnknown.zol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ contract Assign {


function add( uint256 value ) public {
//unknown b = b + value +a;
unknown b += value +a;
unknown b = b + value +a;
unknown x.prop1 += value + a;
unknown x.prop2 += value;
unknown b += value;
Expand All @@ -24,9 +23,7 @@ contract Assign {
}

function remove( uint256 value ) public {
//unknown b = b - value +a;
unknown b -= value +a;
//unknown b = b - (value +a);
unknown b = b - (value +a);
unknown x.prop1 -= value + a;
unknown x.prop2 -= value;
unknown b -= value;
Expand Down

0 comments on commit 33c2e09

Please sign in to comment.