Skip to content

Commit

Permalink
feat: add empty arc4.Bool constructor to mimic bool()
Browse files Browse the repository at this point in the history
  • Loading branch information
achidlow committed Mar 21, 2024
1 parent 146748e commit 6175b59
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
arc-28/EventEmitter 117 77 40 77 0
arc4_numeric_comparisons/UIntNOrdering 1220 908 312 908 0
arc4_types/Arc4Arrays 588 376 212 376 0
arc4_types/Arc4BoolEval 731 19 712 19 0
arc4_types/Arc4BoolEval 741 19 722 19 0
arc4_types/Arc4BoolType 329 57 272 57 0
arc4_types/Arc4DynamicBytes 247 128 119 128 0
arc4_types/Arc4DynamicStringArray 230 112 118 112 0
Expand Down
22 changes: 11 additions & 11 deletions src/puya/awst_build/eb/arc4/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import structlog

from puya.awst import wtypes
from puya.awst.nodes import (
ARC4Encode,
Literal,
)
from puya.awst.nodes import ARC4Encode, BoolConstant, Expression, Literal
from puya.awst_build.eb.arc4.base import (
ARC4ClassExpressionBuilder,
ARC4EncodedExpressionBuilder,
Expand Down Expand Up @@ -43,18 +40,21 @@ def call(
location: SourceLocation,
) -> ExpressionBuilder:
match args:
case []:
native_bool: Expression = BoolConstant(value=False, source_location=location)
case [val]:
return var_expression(
ARC4Encode(
value=expect_operand_wtype(val, wtypes.bool_wtype),
source_location=location,
wtype=self.produces(),
)
)
native_bool = expect_operand_wtype(val, wtypes.bool_wtype)
case _:
raise CodeError(
f"arc4.Bool expects exactly one parameter of type {wtypes.bool_wtype}"
)
return var_expression(
ARC4Encode(
value=native_bool,
source_location=location,
wtype=self.produces(),
)
)


class ARC4BoolExpressionBuilder(ARC4EncodedExpressionBuilder):
Expand Down
2 changes: 1 addition & 1 deletion src/puyapy-stubs/arc4.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ UInt512: typing.TypeAlias = BigUIntN[typing.Literal[512]]
class Bool(_ABIEncoded):
"""An ARC4 encoded bool"""

def __init__(self, value: bool) -> None: ... # noqa: FBT001
def __init__(self, value: bool = False, /) -> None: ... # noqa: FBT001
@property
def native(self) -> bool:
"""Return the bool representation of the value after ARC4 decoding"""
Expand Down
2 changes: 2 additions & 0 deletions test_cases/arc4_types/bool_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def approval_program(self) -> bool:

assert arc4.Tuple((arc4.Bool(False),))

assert arc4.Bool() == arc4.Bool(False)

return True

def clear_state_program(self) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolEvalContract.approval.mir
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ main_block@0:
// virtual: store tmp%11#0 to l-stack (no copy) tmp%11#0 arc4.Address(Txn.sender) arc4_types/bool_eval.py:20
// virtual: load tmp%11#0 from l-stack (no copy) tmp%11#0 assert arc4.Address(Txn.sender) arc4_types/bool_eval.py:20
assert // assert arc4.Address(Txn.sender) arc4_types/bool_eval.py:20
int 1 // 1 True arc4_types/bool_eval.py:70
return // return True arc4_types/bool_eval.py:70
int 1 // 1 True arc4_types/bool_eval.py:72
return // return True arc4_types/bool_eval.py:72

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.approval_program:
global ZeroAddress
!=
assert
// arc4_types/bool_eval.py:70
// arc4_types/bool_eval.py:72
// return True
int 1
return
4 changes: 2 additions & 2 deletions test_cases/arc4_types/out/Arc4BoolEvalContract.clear.mir
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

// test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
main_block@0:
int 1 // 1 True arc4_types/bool_eval.py:73
return // return True arc4_types/bool_eval.py:73
int 1 // 1 True arc4_types/bool_eval.py:75
return // return True arc4_types/bool_eval.py:75

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

test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program:
// arc4_types/bool_eval.py:73
// arc4_types/bool_eval.py:75
// return True
int 1
return
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
6 changes: 5 additions & 1 deletion test_cases/arc4_types/out/Arc4BoolEvalContract.ssa.ir
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let dynamic_arr#1: bytes = concat_result%42#0
let tmp%43#0: uint64 = (!= dynamic_arr#1 0x0000)
(assert tmp%43#0)
let tmp%44#0: any = (setbit 0x00 0u 0u)
let tmp%45#0: any = (setbit 0x00 0u 0u)
let tmp%46#0: uint64 = (== tmp%44#0 tmp%45#0)
(assert tmp%46#0)
return 1u

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let dynamic_arr#1: bytes = (concat len_16_bit%41#0 concatenated%37#0)
let tmp%43#0: uint64 = (!= dynamic_arr#1 0x0000)
(assert tmp%43#0)
let tmp%44#0: any = 0x00
let tmp%45#0: any = 0x00
let tmp%46#0: uint64 = (== tmp%44#0 tmp%45#0)
(assert tmp%46#0)
return 1u

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let dynamic_arr#1: bytes = (concat len_16_bit%41#0 concatenated%37#0)
let tmp%43#0: uint64 = (!= dynamic_arr#1 0x0000)
(assert tmp%43#0)
let tmp%46#0: uint64 = 1u
(assert tmp%46#0)
return 1u

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
1 change: 1 addition & 0 deletions test_cases/arc4_types/out/bool_eval.awst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ contract Arc4BoolEvalContract
assert(reinterpret_cast<puyapy.Bytes>(dynamic_arr) != hex<"0000">)
assert(true)
assert(true)
assert(reinterpret_cast<puyapy.Bytes>(arc4_encode(false, puyapy.arc4.Bool)) == reinterpret_cast<puyapy.Bytes>(arc4_encode(false, puyapy.arc4.Bool)))
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.approval_program:
!=
assert
// arc4_types/bool_eval.py:70
// assert arc4.Bool() == arc4.Bool(False)
byte 0x00
int 0
int 0
setbit
byte 0x00
int 0
int 0
setbit
==
assert
// arc4_types/bool_eval.py:72
// return True
int 1
return
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma version 10

test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program:
// arc4_types/bool_eval.py:73
// arc4_types/bool_eval.py:75
// return True
int 1
return
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let dynamic_arr#0: bytes = concat_result%42#0
let tmp%43#0: uint64 = (!= dynamic_arr#0 0x0000)
(assert tmp%43#0)
let tmp%44#0: any = (setbit 0x00 0u 0u)
let tmp%45#0: any = (setbit 0x00 0u 0u)
let tmp%46#0: uint64 = (== tmp%44#0 tmp%45#0)
(assert tmp%46#0)
return 1u

program clear-state:
subroutine test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.clear_state_program() -> uint64:
block@0: // L72
block@0: // L74
return 1u
10 changes: 8 additions & 2 deletions test_cases/arc4_types/puya.log
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ arc4_types/bool_eval.py:59:9 warning: assertion is always true, ignoring
arc4_types/bool_eval.py:66:9 warning: assertion is always true, ignoring
arc4_types/bool_eval.py:68:9 warning: assertion is always true, ignoring
debug: Terminated block@0: // L12
debug: Sealing block@0: // L72
debug: Terminated block@0: // L72
debug: Sealing block@0: // L74
debug: Terminated block@0: // L74
debug: Sealing block@0: // L10
debug: Terminated block@0: // L10
debug: Looking for 'item_index_internal%6' in an unsealed block creating an incomplete Phi: block@1: // for_header_L16
Expand Down Expand Up @@ -2499,6 +2499,8 @@ debug: Simplified (!= 0x00000000000000000000000000000000000000000000000000000000
debug: Simplified (== 0x0000 0x0000) to 1u
debug: Simplified ((extract 2 0) 0x0000) to 0x
debug: Simplified (concat 0x 0x0000000000000000) to 0x0000000000000000
debug: Simplified (setbit 0x00 0u 0u) to 0x00
debug: Simplified (setbit 0x00 0u 0u) to 0x00
debug: Optimizer: Remove Unused Variables
debug: Removing unused variable dynamic_arr#0
debug: Removing unused variable data%36#0
Expand Down Expand Up @@ -2527,6 +2529,7 @@ debug: Optimizer: Intrinsic Simplifier
debug: Simplified (== 0x00 0x00) to 1u
debug: Simplified (!= 0x80 0x00) to 1u
debug: Simplified (concat 0x 0x0000000000000000) to 0x0000000000000000
debug: Simplified (== 0x00 0x00) to 1u
debug: Optimizer: Remove Unused Variables
debug: Removing unused variable tmp%0#0
debug: Removing unused variable tmp%2#0
Expand Down Expand Up @@ -2557,6 +2560,8 @@ debug: Removing unused variable tmp%33#0
debug: Removing unused variable tmp%34#0
debug: Removing unused variable expr_value_trimmed%35#0
debug: Removing unused variable data%36#1
debug: Removing unused variable tmp%44#0
debug: Removing unused variable tmp%45#0
debug: Optimizer: Simplify Control Ops
debug: Optimizer: Remove Linear Jump
debug: Optimizer: Remove Empty Blocks
Expand All @@ -2583,6 +2588,7 @@ debug: Optimizer: Remove Unused Variables
debug: Removing unused variable tmp%1#0
debug: Removing unused variable tmp%3#0
debug: Removing unused variable concatenated%37#0
debug: Removing unused variable tmp%46#0
debug: Optimizer: Simplify Control Ops
debug: Optimizer: Remove Linear Jump
debug: Optimizer: Remove Empty Blocks
Expand Down

0 comments on commit 6175b59

Please sign in to comment.