-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: raise an error when attempting to modify immutable arrays such a…
…s `algopy.arc4.Address` BREAKING CHANGE: modifying an `algopy.arc4.Address` will now raise an error
- Loading branch information
1 parent
e53fd59
commit 9450c7a
Showing
23 changed files
with
121 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from puya import log | ||
from puya.awst import nodes as awst_nodes | ||
from puya.awst.awst_traverser import AWSTTraverser | ||
|
||
logger = log.get_logger(__name__) | ||
|
||
|
||
class ImmutableValidator(AWSTTraverser): | ||
@classmethod | ||
def validate(cls, module: awst_nodes.AWST) -> None: | ||
validator = cls() | ||
for module_statement in module: | ||
module_statement.accept(validator) | ||
|
||
def visit_assignment_expression(self, expr: awst_nodes.AssignmentExpression) -> None: | ||
super().visit_assignment_expression(expr) | ||
_validate_lvalue(expr.target) | ||
|
||
def visit_assignment_statement(self, statement: awst_nodes.AssignmentStatement) -> None: | ||
super().visit_assignment_statement(statement) | ||
_validate_lvalue(statement.target) | ||
|
||
def visit_array_pop(self, expr: awst_nodes.ArrayPop) -> None: | ||
super().visit_array_pop(expr) | ||
if expr.base.wtype.immutable: | ||
logger.error( | ||
"cannot modify - object is immutable", | ||
location=expr.source_location, | ||
) | ||
|
||
def visit_array_extend(self, expr: awst_nodes.ArrayExtend) -> None: | ||
super().visit_array_extend(expr) | ||
if expr.base.wtype.immutable: | ||
logger.error( | ||
"cannot modify - object is immutable", | ||
location=expr.source_location, | ||
) | ||
|
||
|
||
def _validate_lvalue(lvalue: awst_nodes.Expression) -> None: | ||
if isinstance(lvalue, awst_nodes.FieldExpression | awst_nodes.IndexExpression): | ||
if lvalue.base.wtype.immutable: | ||
logger.error( | ||
"expression is not valid as an assignment target - object is immutable", | ||
location=lvalue.source_location, | ||
) | ||
elif isinstance(lvalue, awst_nodes.TupleExpression): | ||
for item in lvalue.items: | ||
_validate_lvalue(item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 13 additions & 25 deletions
38
test_cases/arc4_types/out/Arc4AddressContract.approval.mir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,26 @@ | ||
// Op Stack (out) | ||
// Op Stack (out) | ||
// test_cases.arc4_types.address.Arc4AddressContract.approval_program() -> uint64: | ||
main_block@0: | ||
// arc4_types/address.py:8 | ||
// address = arc4.Address(Txn.sender) | ||
txn Sender address#0 | ||
txn Sender address#0 | ||
// arc4_types/address.py:9 | ||
// assert address == Txn.sender | ||
txn Sender address#0,tmp%0#0 | ||
l-load-copy address#0 1 address#0,tmp%0#0,address#0 (copy) | ||
l-load tmp%0#0 1 address#0,address#0 (copy),tmp%0#0 | ||
== address#0,tmp%1#0 | ||
assert address#0 | ||
txn Sender address#0,tmp%0#0 | ||
l-load-copy address#0 1 address#0,tmp%0#0,address#0 (copy) | ||
l-load tmp%0#0 1 address#0,address#0 (copy),tmp%0#0 | ||
== address#0,tmp%1#0 | ||
assert address#0 | ||
// arc4_types/address.py:11 | ||
// assert address.native == Txn.sender | ||
txn Sender address#0,tmp%3#0 | ||
l-load address#0 1 tmp%3#0,address#0 | ||
l-load tmp%3#0 1 address#0,tmp%3#0 | ||
== tmp%4#0 | ||
txn Sender address#0,tmp%3#0 | ||
l-load address#0 1 tmp%3#0,address#0 | ||
l-load tmp%3#0 1 address#0,tmp%3#0 | ||
== tmp%4#0 | ||
assert | ||
// arc4_types/address.py:16 | ||
// some_address = arc4.Address(SOME_ADDRESS) | ||
addr "VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA" Address(VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA) | ||
// arc4_types/address.py:19 | ||
// some_address[0] = arc4.Byte(123) | ||
byte 0x7b Address(VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA),0x7b | ||
replace2 0 some_address#1 | ||
// arc4_types/address.py:20 | ||
// assert some_address != SOME_ADDRESS | ||
addr "VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA" some_address#1,Address(VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA) | ||
!= tmp%10#0 | ||
assert | ||
// arc4_types/address.py:21 | ||
// arc4_types/address.py:22 | ||
// return True | ||
int 1 1 | ||
int 1 1 | ||
return | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#pragma version 10 | ||
|
||
test_cases.arc4_types.address.Arc4AddressContract.clear_state_program: | ||
// arc4_types/address.py:24 | ||
// arc4_types/address.py:25 | ||
// return True | ||
pushint 1 // 1 | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
test_cases/arc4_types/out_O2/Arc4AddressContract.approval.teal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
#pragma version 10 | ||
|
||
test_cases.arc4_types.address.Arc4AddressContract.approval_program: | ||
bytecblock base32(VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJPQ) | ||
txn Sender | ||
dupn 2 | ||
== | ||
assert | ||
txn Sender | ||
== | ||
assert | ||
bytec_0 // addr VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA | ||
pushbytes 0x7b | ||
replace2 0 | ||
bytec_0 // addr VCMJKWOY5P5P7SKMZFFOCEROPJCZOTIJMNIYNUCKH7LRO45JMJP6UYBIJA | ||
!= | ||
assert | ||
pushint 1 // 1 | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.