Skip to content

Commit

Permalink
feat: Improve error message for shadowed inout arguments (#351)
Browse files Browse the repository at this point in the history
Closes #338
  • Loading branch information
mark-koch authored Aug 12, 2024
1 parent dd96f17 commit d02e199
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
27 changes: 27 additions & 0 deletions guppylang/checker/linearity_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ def _check_assign_targets(self, targets: list[ast.expr]) -> None:
[target] = targets
for tgt in find_nodes(lambda n: isinstance(n, PlaceNode), target):
assert isinstance(tgt, PlaceNode)
# Special error message for shadowing of @inout vars
x = tgt.place.id
if x in self.scope.vars and is_inout_var(self.scope[x]):
raise GuppyError(
f"Assignment shadows argument `{tgt.place}` annotated as `@inout`. "
"Consider assigning to a different name.",
tgt,
)
for tgt_place in leaf_places(tgt.place):
x = tgt_place.id
if x in self.scope and not self.scope.used(x):
Expand Down Expand Up @@ -361,6 +369,25 @@ def check_cfg_linearity(cfg: "CheckedCFG", globals: Globals) -> None:
for bb in cfg.bbs
}

# Check that @inout vars are not being shadowed. This would also be caught by
# the dataflow analysis below, however we can give nicer error messages here.
for bb, scope in scopes.items():
if bb == cfg.entry_bb:
# Arguments are assigned in the entry BB, so would yield a false positive
# in the check below. Shadowing in the entry BB will be caught by the check
# in `_check_assign_targets`.
continue
entry_scope = scopes[cfg.entry_bb]
for x, place in scope.vars.items():
if x in entry_scope:
entry_place = entry_scope[x]
if is_inout_var(entry_place):
raise GuppyError(
f"Assignment shadows argument `{entry_place}` annotated as "
"`@inout`. Consider assigning to a different name.",
place.defined_at,
)

# Mark the @inout variables as implicitly used in the exit BB
exit_scope = scopes[cfg.exit_bb]
for var in cfg.entry_bb.sig.input_row:
Expand Down
11 changes: 6 additions & 5 deletions tests/error/inout_errors/shadow.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Guppy compilation failed. Error in file $FILE:11
Guppy compilation failed. Error in file $FILE:12

9: @guppy(module)
10: def test(q: qubit @inout) -> None:
^^^^^^^^^^^^^^^
GuppyError: Variable `q` with linear type `qubit` is not used
10: @guppy(module)
11: def test(q: qubit @inout) -> None:
12: q = qubit()
^
GuppyError: Assignment shadows argument `q` annotated as `@inout`. Consider assigning to a different name.
11 changes: 6 additions & 5 deletions tests/error/inout_errors/shadow_if.err
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Guppy compilation failed. Error in file $FILE:11
Guppy compilation failed. Error in file $FILE:13

9: @guppy(module)
10: def test(q: qubit @inout, b: bool) -> None:
^^^^^^^^^^^^^^^
GuppyError: Variable `q` with linear type `qubit` is not used on all control-flow paths
11: def test(q: qubit @inout, b: bool) -> None:
12: if b:
13: q = qubit()
^
GuppyError: Assignment shadows argument `q` annotated as `@inout`. Consider assigning to a different name.

0 comments on commit d02e199

Please sign in to comment.