Skip to content

Commit

Permalink
fix: fix compilation error when a nested tuple is passed as a named a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
achidlow committed Sep 17, 2024
1 parent 4b2588f commit d849496
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/puya/ir/builder/_tuple_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ def build_tuple_registers(
context, format_tuple_index(base_name, idx), item_type, source_location
)
]


def build_tuple_item_names(
context: IRFunctionBuildContext,
base_name: str,
wtype: wtypes.WTuple,
source_location: SourceLocation | None,
) -> list[str]:
return [r.name for r in build_tuple_registers(context, base_name, wtype, source_location)]
21 changes: 14 additions & 7 deletions src/puya/ir/builder/callsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
wtypes,
)
from puya.errors import CodeError
from puya.ir.builder._tuple_util import build_tuple_item_names
from puya.ir.builder._utils import assign_targets, new_register_version
from puya.ir.context import IRFunctionBuildContext
from puya.ir.models import InvokeSubroutine, Register, Subroutine, Value, ValueProvider, ValueTuple
from puya.ir.utils import format_tuple_index
from puya.parse import SourceLocation


Expand Down Expand Up @@ -116,10 +116,17 @@ def _build_arg_lookup(
lookup.add(name=expr_arg.name, value=value)
else:
values = context.visitor.visit_and_materialise(expr_arg.value)
for tup_idx, tup_value in enumerate(values):
if expr_arg.name is None:
tup_item_name = None
else:
tup_item_name = format_tuple_index(expr_arg.name, tup_idx)
lookup.add(name=tup_item_name, value=tup_value)
if expr_arg.name is None:
for tup_value in values:
lookup.add(name=None, value=tup_value)
else:
item_names = build_tuple_item_names(
context,
base_name=expr_arg.name,
wtype=expr_arg.value.wtype,
source_location=call_location,
)
for tup_value, tup_item_name in zip(values, item_names, strict=True):
lookup.add(name=tup_item_name, value=tup_value)

return lookup
2 changes: 1 addition & 1 deletion test_cases/tuple_support/nested_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_tests(self) -> bool:
_x, z2_1_1 = z2_1
assert z2_1_1 == "There"

(a, b, (c, d, (e,))) = test_rearrange(x[0], z, x[1])
(a, b, (c, d, (e,))) = test_rearrange(x[0], _b=x[1], args=z)
assert (a, b) == (String("Hi"), UInt64(0))
assert (c, d) == (UInt64(2), UInt64(1))
assert e == String("There")
Expand Down
12 changes: 6 additions & 6 deletions test_cases/tuple_support/out/NestedTuples.approval.mir
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ run_tests_block@0:
byte "Hi" // "Hi",0,2,1,"Hi" String("Hi") tuple_support/nested_tuples.py:10
byte "There" // "Hi",0,2,1,"Hi","There" String("There") tuple_support/nested_tuples.py:10
byte "There" // "Hi",0,2,1,"Hi","There","There" String("There") tuple_support/nested_tuples.py:10
callsub test_rearrange // {test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2,{test_rearrange}.3,{test_rearrange}.4 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
cover 4 // store e#0 to l-stack (no copy) e#0,{test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2,{test_rearrange}.3 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
cover 3 // store d#0 to l-stack (no copy) e#0,d#0,{test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
cover 2 // store c#0 to l-stack (no copy) e#0,d#0,c#0,{test_rearrange}.0,{test_rearrange}.1 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
cover 1 // store b#0 to l-stack (no copy) e#0,d#0,c#0,b#0,{test_rearrange}.0 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
// virtual: store a#0 to l-stack (no copy) e#0,d#0,c#0,b#0,a#0 test_rearrange(x[0], z, x[1]) tuple_support/nested_tuples.py:19
callsub test_rearrange // {test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2,{test_rearrange}.3,{test_rearrange}.4 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
cover 4 // store e#0 to l-stack (no copy) e#0,{test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2,{test_rearrange}.3 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
cover 3 // store d#0 to l-stack (no copy) e#0,d#0,{test_rearrange}.0,{test_rearrange}.1,{test_rearrange}.2 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
cover 2 // store c#0 to l-stack (no copy) e#0,d#0,c#0,{test_rearrange}.0,{test_rearrange}.1 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
cover 1 // store b#0 to l-stack (no copy) e#0,d#0,c#0,b#0,{test_rearrange}.0 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
// virtual: store a#0 to l-stack (no copy) e#0,d#0,c#0,b#0,a#0 test_rearrange(x[0], _b=x[1], args=z) tuple_support/nested_tuples.py:19
// virtual: load a#0 from l-stack (no copy) e#0,d#0,c#0,b#0,a#0 (a, b) == (String("Hi"), UInt64(0)) tuple_support/nested_tuples.py:20
byte "Hi" // e#0,d#0,c#0,b#0,a#0,"Hi" String("Hi") tuple_support/nested_tuples.py:20
== // e#0,d#0,c#0,b#0,{==} (a, b) == (String("Hi"), UInt64(0)) tuple_support/nested_tuples.py:20
Expand Down
2 changes: 1 addition & 1 deletion test_cases/tuple_support/out/NestedTuples.approval.teal
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ run_tests:
byte "There"
dup
// tuple_support/nested_tuples.py:19
// (a, b, (c, d, (e,))) = test_rearrange(x[0], z, x[1])
// (a, b, (c, d, (e,))) = test_rearrange(x[0], _b=x[1], args=z)
callsub test_rearrange
cover 4
cover 3
Expand Down
2 changes: 1 addition & 1 deletion test_cases/tuple_support/out/NestedTuples.arc32.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test_cases/tuple_support/out/module.awst
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ contract NestedTuples extends (algopy.arc4.ARC4Contract)
z2_1: tuple<string,string> = z2[1]
(_x, z2_1_1): tuple<string,string> = z2_1
assert(z2_1_1 == 'There')
(a, b, (c, d, (e))): tuple<string,uint64,tuple<uint64,uint64,tuple<string>>> = test_cases.tuple_support.nested_tuples.test_rearrange(x[0], z, x[1])
(a, b, (c, d, (e))): tuple<string,uint64,tuple<uint64,uint64,tuple<string>>> = test_cases.tuple_support.nested_tuples.test_rearrange(x[0], args=z, _b=x[1])
assert(&&(a == 'Hi', b == 0u))
assert(&&(c == 2u, d == 1u))
assert(e == 'There')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ run_tests:
byte "There"
byte "There"
// tuple_support/nested_tuples.py:19
// (a, b, (c, d, (e,))) = test_rearrange(x[0], z, x[1])
// (a, b, (c, d, (e,))) = test_rearrange(x[0], _b=x[1], args=z)
callsub test_rearrange
cover 4
cover 3
Expand Down

0 comments on commit d849496

Please sign in to comment.