-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Type check array subscripts (#420)
Closes #416. See #415 for context. * Adds a new `SubscriptAccess` place that will be used to track array subscripts during linearity checking * This place is emitted when checking a subscript AST node * Ensures that subscripts in inout positions also implement a `__setitem__` method
- Loading branch information
Showing
15 changed files
with
220 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:24 | ||
|
||
22: @guppy(module) | ||
23: def test(c: MyImmutableContainer) -> MyImmutableContainer: | ||
24: foo(c[0]) | ||
^^^^ | ||
GuppyTypeError: Expression of type `MyImmutableContainer` is not allowed in a subscripted `@inout` position since it does not implement the `__setitem__` method |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.prelude.builtins import inout | ||
from guppylang.prelude.quantum import qubit, quantum | ||
|
||
module = GuppyModule("test") | ||
module.load(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def foo(q: qubit @inout) -> None: ... | ||
|
||
|
||
@guppy.struct(module) | ||
class MyImmutableContainer: | ||
q: qubit | ||
|
||
@guppy.declare(module) | ||
def __getitem__(self: "MyImmutableContainer" @inout, idx: int) -> qubit: ... | ||
|
||
|
||
@guppy(module) | ||
def test(c: MyImmutableContainer) -> MyImmutableContainer: | ||
foo(c[0]) | ||
return c | ||
|
||
|
||
module.compile() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:9 | ||
|
||
7: @guppy(module) | ||
8: def foo(x: int) -> None: | ||
9: x[0] | ||
^ | ||
GuppyTypeError: Expression of type `int` is not subscriptable |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
|
||
module = GuppyModule("test") | ||
|
||
|
||
@guppy(module) | ||
def foo(x: int) -> None: | ||
x[0] | ||
|
||
|
||
module.compile() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Guppy compilation failed. Error in file $FILE:10 | ||
|
||
8: @guppy(module) | ||
9: def foo(xs: array[int, 42]) -> int: | ||
10: return xs[1.0] | ||
^^^ | ||
GuppyTypeError: Expected argument of type `int`, got `float` |
Oops, something went wrong.