-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow borrowing inside comprehensions (#723)
Fixes #719
- Loading branch information
Showing
18 changed files
with
409 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Error: Linearity violation (at $FILE:21:16) | ||
| | ||
19 | @guppy(module) | ||
20 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
21 | return [baz(q) for q in qs if bar(q)] | ||
| ^ Variable `q` with linear type `qubit` cannot be borrowed | ||
| ... | ||
| | ||
21 | return [baz(q) for q in qs if bar(q)] | ||
| - since it was already consumed here | ||
|
||
Guppy compilation failed due to 1 previous error |
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,24 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit @owned) -> int: ... | ||
|
||
|
||
@guppy.declare(module) | ||
def baz(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [baz(q) for q in qs if bar(q)] | ||
|
||
|
||
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,10 @@ | ||
Error: Linearity violation (at $FILE:17:16) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(n: int, q: qubit @owned) -> list[int]: | ||
17 | return [bar(q) for _ in range(n)] | ||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Help: Make sure that `q` is consumed or returned to avoid the leak | ||
|
||
Guppy compilation failed due to 1 previous error |
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,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(n: int, q: qubit @owned) -> list[int]: | ||
return [bar(q) for _ in range(n)] | ||
|
||
|
||
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,8 @@ | ||
Error: Linearity violation (at $FILE:17:23) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
17 | return [bar(q) for q in qs] | ||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Guppy compilation failed due to 1 previous error |
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,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> int: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [bar(q) for q in qs] | ||
|
||
|
||
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,11 @@ | ||
Error: Linearity violation (at $FILE:17:18) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[int]: | ||
17 | return [0 for q in qs if bar(q)] | ||
| ^ Variable `q` with linear type `qubit` may be leaked ... | ||
| | ||
17 | return [0 for q in qs if bar(q)] | ||
| ------ if this expression is `False` | ||
|
||
Guppy compilation failed due to 1 previous error |
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,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> bool: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[int]: | ||
return [0 for q in qs if bar(q)] | ||
|
||
|
||
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,8 @@ | ||
Error: Linearity violation (at $FILE:17:18) | ||
| | ||
15 | @guppy(module) | ||
16 | def foo(qs: list[qubit] @owned) -> list[qubit]: | ||
17 | return [r for q in qs for r in bar(q)] | ||
| ^ Variable `q` with linear type `qubit` is leaked | ||
|
||
Guppy compilation failed due to 1 previous error |
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,20 @@ | ||
import guppylang.std.quantum as quantum | ||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.std.quantum import qubit | ||
from guppylang.std.builtins import owned | ||
|
||
module = GuppyModule("test") | ||
module.load_all(quantum) | ||
|
||
|
||
@guppy.declare(module) | ||
def bar(q: qubit) -> list[qubit]: ... | ||
|
||
|
||
@guppy(module) | ||
def foo(qs: list[qubit] @owned) -> list[qubit]: | ||
return [r for q in qs for r in bar(q)] | ||
|
||
|
||
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
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
Oops, something went wrong.