Skip to content

[Bug]: Unknown variable during package serialization when using generic structs #2988

@maximilianruesch

Description

@maximilianruesch

Description

Using the following Guppy code:

from typing import Generic
from guppylang import guppy, array

def test_hugr():
    N = guppy.nat_var("N")
    M = guppy.nat_var("M")

    @guppy.struct
    class MyStruct(Generic[N]):
        state: array[bool, N]

    @guppy.declare
    def struct_factory() -> MyStruct[1]: ...
    @guppy.declare
    def struct_consumer(_: MyStruct[M]) -> None: ...

    @guppy
    def main() -> None:
        struct_consumer(struct_factory())

    pkg = main.compile()
    pkg.to_bytes() # Fails with "unknown var: ?0"

the serialization to bytes fails with the indicated message.

The full traceback is:

        pkg = main.compile()
>       pkg.to_bytes() # Fails with "unknown var: ?0"
        ^^^^^^^^^^^^^^

tests/test_hugr.py:22: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../hugr/hugr-py/src/hugr/package.py:126: in to_bytes
    return _make_envelope(self, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../hugr/hugr-py/src/hugr/envelope.py:95: in _make_envelope
    package_bytes = bytes(package.to_model())
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Package(modules=[Module(root=Region(kind=<RegionKind.MODULE: 2>, sources=[], targets=[], children=[Node(operation=Defi...or'), Literal(value='{"name": "guppylang (guppylang-internals-v0.31.0)", "version": "0.21.10"}')])], signature=None))])

    def __bytes__(self):
        """Convert the package into its binary representation."""
>       return rust.package_to_bytes(self)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
E       ValueError: unknown var: ?0

../hugr/hugr-py/src/hugr/model/__init__.py:342: ValueError

Steps to Reproduce

See above, run that inside a Python environment with the latest main.

Expected Behaviour

It should serialize just fine.

Actual Behaviour

It does in fact not "serialize just fine".

Component

Python Bindings

Environment

Additional Context

I believe to have identified the bad commit using git bisect: 8750a6b

I presume there is a spot where the change made is missing.

Metadata

Metadata

Assignees

Labels

A-rustArea: Rust codeC-bugfixCategory: PR with bug fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions