feat: Custom modifiers as struct and enum methods - #2277
Open
nicolaassolini-qntm wants to merge 264 commits into
Open
nicolaassolini-qntm wants to merge 264 commits into
nicolaassolini-qntm wants to merge 264 commits into
Conversation
… map in CompilerContext
nicolaassolini-qntm
removed this pull request from stack #2226
September 9, 2026 14:17
…nto na/custom-in-struct
nicolaassolini-qntm
changed the base branch from
main
to
na/forbid-assigment-in-custom
September 9, 2026 14:20
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
Retaining the inner class frame breaks explicit parent-type annotations and enclosing-scope name resolution.
Review details
Suppressed comments (2)
guppylang-internals/src/guppylang_internals/engine.py:212
- This keeps the frame of the inner unitary class, but
Globalsonly reads that frame's locals and globals. The containing struct/enum name is not bound there (or in the outer class frame), so the supported form from #2262, such asdef __call__(self: "S"), cannot resolveS; locally defined helpers and aliases are hidden for the same reason. Since unitary-class statements are rejected and its type parameters are already copied onto each raw function, advance these methods out of both class scopes and add an explicit-self regression test.
is_unitary_call = (
isinstance(member, RawFunctionDef) and member.unitary_class_at is not None
)
self._register_type_member_parent(
ty_id, member_id, adjust_frame=not is_unitary_call
)
guppylang-internals/src/guppylang_internals/definition/struct.py:62
- The updated help text is missing punctuation/conjunction between the field and method forms, producing
name: Type` `@guppy`, annotated methodsin every affected diagnostic. Reword it to “fields of the formname: Type,@guppyannotated methods, or@guppy.unitaryclasses” and update the snapshots.
message: ClassVar[str] = (
"Struct can contain only fields of the form `name: Type` "
"`@guppy`, annotated methods or `@guppy.unitary` classes"
- Files reviewed: 31/31 changed files
- Comments generated: 0 new
- Review effort level: Balanced
qartik
reviewed
Sep 11, 2026
Member
There was a problem hiding this comment.
Some feedback from Gemini 3.8 Flash:
Medium Severity
Incorrect scope frame for unitary methods in structs and enums
- File:
guppylang-internals/src/guppylang_internals/engine.py:211-226- Issue: Passing
adjust_frame=Falseretains the inner@guppy.unitaryclass frame for__call__and custom modifier implementations (daggered,controlled, etc.). When a struct/enum is defined inside a local scope (e.g. factory functions or test functions),Globalscannot resolve outer symbols such as explicit type annotations (e.g.self: "S"from #2262). Furthermore, PR #2310 already rejects non-method statements in@guppy.unitaryclasses, so the rationale in the comment is obsolete.- Fix: Advance the frame past both the unitary class and the struct/enum class scopes so it reaches the enclosing definition scope.
Low Severity / Test Coverage
Missing test coverage for
selfin enum unitary methods
- Files:
tests/integration/test_enum.py,tests/integration/test_modifier.py- Issue: In the added enum tests,
selfis ignored in every custom modifier implementation (passorx(q)). None test accessing or pattern matching onself(e.g.match self: case CustomGates.Enabled(): ...).- Fix: Add an integration test where an enum unitary method matches on
self.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
follows #2274
follows #2310
close #2262
close #2276
As well as introducing the use of
@guppy.unitarywithin structs and enums, I have updated the span in some error messages to ensure consistency