Skip to content

Commit

Permalink
fix: Fix redefinition of structs (#499)
Browse files Browse the repository at this point in the history
Closes #498.

The problem was that the autogenerated `__new__` method of structs
wasn't added to `globals.defs`, yielding a `KeyError` when trying to
unregister it before overriding.
  • Loading branch information
mark-koch authored Sep 16, 2024
1 parent cc8a424 commit 0b156e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions guppylang/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def check(self) -> None:
for method_def in defn.generated_methods():
generated[method_def.id] = method_def
self._globals.impls[defn.id][method_def.name] = method_def.id
self._globals.defs.update(generated)

# Now, we can check all other definitions
other_defs = self._check_defs(
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/test_redefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ def foo(self: "Test") -> int:
validate(module.compile())


def test_redefine_after_error(validate):
module = GuppyModule("test")

@guppy.struct(module)
class Foo:
x: int

@guppy(module)
def foo() -> int:
return y

try:
module.compile()
except:
pass

@guppy.struct(module)
class Foo:
x: int

@guppy(module)
def foo(f: Foo) -> int:
return f.x

validate(module.compile())


@pytest.mark.skip("See https://github.com/CQCL/guppylang/issues/456")
def test_struct_redefinition(validate):
module = GuppyModule("test")
Expand Down

0 comments on commit 0b156e9

Please sign in to comment.