Skip to content

Commit

Permalink
feat: support comparisons between arc4.Bool and bool
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx authored and achidlow committed Jun 25, 2024
1 parent 3b99b52 commit 1787f06
Show file tree
Hide file tree
Showing 34 changed files with 332 additions and 146 deletions.
2 changes: 1 addition & 1 deletion examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
arc4_types/Arc4Address 91 62 29 62 0
arc4_types/Arc4Arrays 670 376 294 376 0
arc4_types/Arc4BoolEval 761 14 747 14 0
arc4_types/Arc4BoolType 376 76 300 76 0
arc4_types/Arc4BoolType 412 76 336 76 0
arc4_types/Arc4DynamicBytes 389 190 199 190 0
arc4_types/Arc4DynamicStringArray 285 112 173 112 0
arc4_types/Arc4MutableParams 418 233 185 230 3
Expand Down
11 changes: 9 additions & 2 deletions src/puya/awst_build/eb/arc4/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
LiteralBuilder,
NodeBuilder,
)
from puya.awst_build.utils import require_instance_builder
from puya.parse import SourceLocation

logger = log.get_logger(__name__)
Expand Down Expand Up @@ -92,5 +93,11 @@ def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
def compare(
self, other: InstanceBuilder, op: BuilderComparisonOp, location: SourceLocation
) -> InstanceBuilder:
# TODO(first): support comparisons with native bool
return compare_bytes(lhs=self, op=op, rhs=other, source_location=location)
match other:
case InstanceBuilder(pytype=pytypes.BoolType):
lhs = require_instance_builder(self.member_access("native", location))
return lhs.compare(other, op, location)
case InstanceBuilder(pytype=pytypes.ARC4BoolType):
return compare_bytes(lhs=self, op=op, rhs=other, source_location=location)
case _:
return NotImplemented
2 changes: 2 additions & 0 deletions stubs/algopy-stubs/arc4.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ class Bool(_ABIEncoded):
"""An ARC4 encoded bool"""

def __init__(self, value: bool = False, /) -> None: ... # noqa: FBT001, FBT002
def __eq__(self, other: Bool | bool) -> bool: ... # type: ignore[override]
def __ne__(self, other: Bool | bool) -> bool: ... # type: ignore[override]
@property
def native(self) -> bool:
"""Return the bool representation of the value after ARC4 decoding"""
Expand Down
5 changes: 5 additions & 0 deletions test_cases/arc4_types/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def approval_program(self) -> bool:

assert dynamic_boolean_array.bytes == Bytes.from_hex("0003A0")

assert ARC4Bool(True) == True # noqa: E712
assert ARC4Bool(False) != True # noqa: E712
assert False == ARC4Bool(False) # noqa: E712, SIM300
assert False != ARC4Bool(True) # noqa: E712, SIM300

return True

def clear_state_program(self) -> bool:
Expand Down
80 changes: 40 additions & 40 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.approval.mir

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,34 @@ main_ternary_merge@3:
int 0
getbit
assert // conditional expr
// arc4_types/bool.py:44
// arc4_types/bool.py:49
// return True
int 1
return


// test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> uint64:
test_stuff:
// arc4_types/bool.py:49-50
// arc4_types/bool.py:54-55
// @subroutine
// def test_stuff(self, true: ARC4Bool, false: ARC4Bool) -> bool:
proto 2 1
// arc4_types/bool.py:51
// arc4_types/bool.py:56
// assert true.native
frame_dig -2
int 0
getbit
dup
assert
// arc4_types/bool.py:53
// arc4_types/bool.py:58
// assert not false.native
frame_dig -1
int 0
getbit
dup
!
assert
// arc4_types/bool.py:55
// arc4_types/bool.py:60
// assert true == ARC4Bool(true.native)
byte 0x00
int 0
Expand All @@ -67,7 +67,7 @@ test_stuff:
frame_dig -2
==
assert
// arc4_types/bool.py:56
// arc4_types/bool.py:61
// assert false == ARC4Bool(false.native)
byte 0x00
int 0
Expand All @@ -76,6 +76,6 @@ test_stuff:
frame_dig -1
==
assert
// arc4_types/bool.py:58
// arc4_types/bool.py:63
// return true.native
retsub
4 changes: 2 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.clear.mir
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

// test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> uint64:
main_block@0:
int 1 // 1 True arc4_types/bool.py:47
return // return True arc4_types/bool.py:47
int 1 // 1 True arc4_types/bool.py:52
return // return True arc4_types/bool.py:52

2 changes: 1 addition & 1 deletion test_cases/arc4_types/out/Arc4BoolTypeContract.clear.teal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma version 10

test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program:
// arc4_types/bool.py:47
// arc4_types/bool.py:52
// return True
int 1
return
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -34,5 +34,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
20 changes: 18 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.ssa.ir
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,26 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
let dynamic_boolean_array#0: bytes = array_data%1#0
let tmp%5#0: bool = (== dynamic_boolean_array#0 0x0003a0)
(assert tmp%5#0)
let encoded_bool%22#0: bytes = (setbit 0x00 0u 1u)
let tmp%6#0: bool = (getbit encoded_bool%22#0 0u)
let tmp%7#0: bool = (== tmp%6#0 1u)
(assert tmp%7#0)
let encoded_bool%23#0: bytes = (setbit 0x00 0u 0u)
let tmp%8#0: bool = (getbit encoded_bool%23#0 0u)
let tmp%9#0: bool = (!= tmp%8#0 1u)
(assert tmp%9#0)
let encoded_bool%24#0: bytes = (setbit 0x00 0u 0u)
let tmp%10#0: bool = (getbit encoded_bool%24#0 0u)
let tmp%11#0: bool = (== tmp%10#0 0u)
(assert tmp%11#0)
let encoded_bool%25#0: bytes = (setbit 0x00 0u 1u)
let tmp%12#0: bool = (getbit encoded_bool%25#0 0u)
let tmp%13#0: bool = (!= tmp%12#0 0u)
(assert tmp%13#0)
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -106,5 +122,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
19 changes: 17 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.ssa.opt_pass_1.ir
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,25 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
let dynamic_boolean_array#0: bytes = (concat 0x0003 array_head_and_tail%16#0)
let tmp%5#0: bool = (== dynamic_boolean_array#0 0x0003a0)
(assert tmp%5#0)
let encoded_bool%22#0: bytes = 0x80
let tmp%6#0: bool = (getbit encoded_bool%22#0 0u)
let tmp%7#0: bool = (== tmp%6#0 1u)
(assert tmp%7#0)
let encoded_bool%23#0: bytes = 0x00
let tmp%8#0: bool = (getbit encoded_bool%23#0 0u)
let tmp%9#0: bool = (!= tmp%8#0 1u)
(assert tmp%9#0)
let encoded_bool%24#0: bytes = 0x00
let tmp%10#0: bool = (getbit encoded_bool%24#0 0u)
let tmp%11#0: bool = (! tmp%10#0)
(assert tmp%11#0)
let encoded_bool%25#0: bytes = 0x80
let tmp%12#0: bool = (getbit encoded_bool%25#0 0u)
(assert tmp%12#0)
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -98,5 +113,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -49,5 +49,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -48,5 +48,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -47,5 +47,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -46,5 +46,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -45,5 +45,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -41,5 +41,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -39,5 +39,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -35,5 +35,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
15 changes: 13 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolTypeContract.ssa.opt_pass_2.ir
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:
let dynamic_boolean_array#0: bytes = (concat 0x0003 array_head_and_tail%16#0)
let tmp%5#0: bool = (== dynamic_boolean_array#0 0x0003a0)
(assert tmp%5#0)
let tmp%6#0: bool = 1u
let tmp%7#0: bool = (== tmp%6#0 1u)
(assert tmp%7#0)
let tmp%8#0: bool = 0u
let tmp%9#0: bool = (!= tmp%8#0 1u)
(assert tmp%9#0)
let tmp%10#0: bool = 0u
let tmp%11#0: bool = (! tmp%10#0)
(assert tmp%11#0)
let tmp%12#0: bool = 1u
(assert tmp%12#0)
return 1u

subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.test_stuff(true: bytes, false: bytes) -> bool:
block@0: // L49
block@0: // L54
let tmp%0#0: bool = (getbit true#0 0u)
(assert tmp%0#0)
let tmp%1#0: bool = (getbit false#0 0u)
Expand All @@ -75,5 +86,5 @@ contract test_cases.arc4_types.bool.Arc4BoolTypeContract:

program clear-state:
subroutine test_cases.arc4_types.bool.Arc4BoolTypeContract.clear_state_program() -> bool:
block@0: // L46
block@0: // L51
return 1u
Loading

0 comments on commit 1787f06

Please sign in to comment.