Skip to content

Commit

Permalink
Adjust comments and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tatiana-s committed Jan 16, 2025
1 parent 7caa1e1 commit 43f7110
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
3 changes: 2 additions & 1 deletion guppylang/checker/expr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,8 @@ def visit_MakeIter(self, node: MakeIter) -> tuple[ast.expr, Type]:

# If the iterator was created by a `for` loop, we can add some extra checks to
# produce nicer errors for linearity violations. Namely, `break` and `return`
# are not allowed when looping over a linear iterator (`continue` is allowed)
# are not allowed when looping over a non-copyable iterator (`continue` is
# allowed)
if not ty.droppable and isinstance(node.origin_node, ast.For):
breaks = breaks_in_loop(node.origin_node) or return_nodes_in_ast(
node.origin_node
Expand Down
14 changes: 7 additions & 7 deletions guppylang/tys/ty.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TypeBase(ToHugr[ht.Type], Transformable["Type"], ABC):
@cached_property
@abstractmethod
def copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""

@cached_property
@abstractmethod
Expand All @@ -46,7 +46,7 @@ def linear(self) -> bool:

@property
def affine(self) -> bool:
"""Whether this type should be treated linearly."""
"""Whether this type should be treated in an affine way."""
return not self.copyable and self.droppable

@property
Expand Down Expand Up @@ -322,7 +322,7 @@ def __lt__(self, other: "NumericType.Kind") -> bool:

@property
def copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""
return True

@property
Expand Down Expand Up @@ -514,7 +514,7 @@ def __init__(self, element_types: Sequence["Type"], preserve: bool = False) -> N

@property
def intrinsically_copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""
return True

@property
Expand Down Expand Up @@ -555,7 +555,7 @@ def __init__(self, element_types: Sequence["Type"]) -> None:

@property
def intrinsically_copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""
return True

@property
Expand Down Expand Up @@ -596,7 +596,7 @@ class OpaqueType(ParametrizedTypeBase):

@property
def intrinsically_copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""
return not self.defn.never_copyable

@property
Expand Down Expand Up @@ -648,7 +648,7 @@ def field_dict(self) -> "dict[str, StructField]":

@cached_property
def intrinsically_copyable(self) -> bool:
"""Whether objects of this type can be copied."""
"""Whether objects of this type can be implicitly copied."""
return all(f.ty.copyable for f in self.fields)

@cached_property
Expand Down
11 changes: 11 additions & 0 deletions tests/error/linear_errors/nested_array.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Error: Subscript returned (at $FILE:10:11)
|
8 | @guppy(module)
9 | def foo(xs: array[array[int, 10], 20]) -> array[int, 10]:
10 | return xs[0]
| ^^^^^ Cannot return a subscript of `xs` with affine type
| `array[array[int, 10], 20]`

Note: Subscripts on affine types are only allowed to be borrowed, not returned

Guppy compilation failed due to 1 previous error
12 changes: 12 additions & 0 deletions tests/error/linear_errors/nested_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from guppylang.decorator import guppy
from guppylang.module import GuppyModule
from guppylang.std.builtins import array


module = GuppyModule("test")

@guppy(module)
def foo(xs: array[array[int, 10], 20]) -> array[int, 10]:
return xs[0]

module.compile()
34 changes: 34 additions & 0 deletions tests/integration/test_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest

from hugr import tys as ht

from hugr import Wire

from guppylang.decorator import guppy
Expand Down Expand Up @@ -244,6 +246,38 @@ def main(q: qubit) -> qubit:
validate(module.compile())


def test_affine(validate):
module = GuppyModule("test")
T = guppy.type_var("T", copyable=False, droppable=True, module=module)

@guppy.declare(module)
def foo(x: T) -> T: ...

@guppy(module)
def main(a: array[int, 7]) -> None:
foo(a)

validate(module.compile())


def test_relevant(validate):
module = GuppyModule("test")
T = guppy.type_var("T", copyable=True, droppable=False, module=module)

@guppy.type(ht.Bool, copyable=True, droppable=False, module=module)
class R: ...

@guppy.declare(module)
def foo(x: T) -> T: ...

@guppy(module)
def main(r: R) -> R:
r_copy = r
return foo(r_copy)

validate(module.compile())


def test_pass_nonlinear(validate):
module = GuppyModule("test")
module.load_all(quantum)
Expand Down

0 comments on commit 43f7110

Please sign in to comment.