-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Place
union and lower AST to it during checking
#289
Conversation
guppylang/checker/core.py
Outdated
from guppylang.definition.struct import StructField | ||
|
||
|
||
#: A "place" is a description for a storage location of a local value that users |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#: A "place" is a description for a storage location of a local value that users | |
#: A "place" is a description for a storage location of a local value that users |
guppylang/checker/stmt_checker.py
Outdated
# For now we complain if someone tries to assign to something that | ||
# is not a place (e.g. `f().a = 4`) | ||
raise GuppyError( | ||
"Assigning to this expression is not supported yet", value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth more details on why not, otherwise I don't necessarily know how to fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant more details in the error message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ops yeah I misunderstood. I added a fix suggestion to the error message
"Mutation of classical fields is not supported yet", lhs | ||
) | ||
place = FieldAccess(value.place, struct_ty.field_dict[attr], lhs) | ||
return with_loc(lhs, PlaceNode(place=place)) | ||
|
||
# The only other thing we support right now are tuples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slightly outdated comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More detail in the error message please!
This is the feature branch for enabling struct field access and mutation of linear fields. Closes #276, closes #280, and closes #156. The difficult bit is that we want to track linearity of individual struct fields separately (akin to Rust's partial moves): ```python @guppy.struct class MyStruct: q1: qubit q2: qubit x: int def main(s: MyStruct): q = h(s.q1) t = h(s.q2) # Using s.q2 is fine, only s.q1 has been used y = s.x + s.x # Classical fields can be used multiple times use(s) # Error: Linear fields of s have already been used ... ``` This is the plan: * We introduce a new notion called `Place`: A place is a description for a storage location of a local value that users can refer to in their program. Roughly, these are values that can be lowered to a static wire within the Hugr and are tracked separately when checking linearity. * For the purposes of this PR, a place is either a local variable or a field projection of another place that holds a struct. I.e. places are paths `a.b.c.d` of zero or more nested struct accesses. In the future, indexing into an array etc will also become a place. * During type checking, we figure out which AST nodes correspond to places and annotate them as such * For linearity checking, we run a liveness analysis pass that tracks usage and assignment of places across the CFG. This way, linearity of different struct fields is tracked individually. * When compiling to Hugr, we keep track of a mapping from places to wires/ports Tracked PRs: * #288: Precursor PR to generalise our program analysis framework to run on places in the future. * #289: Adds the `Place` type and implements the type checking logic to turn `ast.Name` and `ast.Attribute` nodes into places. * #290: Update linearity checker to operate on places instead of variables * #291: Lower places to Hugr * #292: Some missing pieces to handle nested functions correctlt * #293
See #295 for context.
The tests are at #293