-
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 panic
builtin function
#757
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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: No panic message (at $FILE:7:4) | ||
| | ||
5 | @compile_guppy | ||
6 | def foo(x: int) -> None: | ||
7 | panic() | ||
| ^^^^^^^ Missing message argument to panic call | ||
|
||
Note: Add a message: `panic("message")` | ||
|
||
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,7 @@ | ||
from guppylang.std.builtins import panic | ||
from tests.util import compile_guppy | ||
|
||
|
||
@compile_guppy | ||
def foo(x: int) -> None: | ||
panic() |
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: Expected a string literal (at $FILE:7:10) | ||
| | ||
5 | @compile_guppy | ||
6 | def foo(x: int) -> None: | ||
7 | panic((), x) | ||
| ^^ Expected a string literal | ||
|
||
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,7 @@ | ||
from guppylang.std.builtins import panic | ||
from tests.util import compile_guppy | ||
|
||
|
||
@compile_guppy | ||
def foo(x: int) -> None: | ||
panic((), x) |
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: Expected a string literal (at $FILE:7:10) | ||
| | ||
5 | @compile_guppy | ||
6 | def foo(y: bool) -> None: | ||
7 | panic("foo" + "bar", y) | ||
| ^^^^^^^^^^^^^ Expected a string literal | ||
|
||
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,7 @@ | ||
from guppylang.std.builtins import panic | ||
from tests.util import compile_guppy | ||
|
||
|
||
@compile_guppy | ||
def foo(y: bool) -> None: | ||
panic("foo" + "bar", y) |
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,38 @@ | ||
from guppylang import GuppyModule, guppy | ||
from guppylang.std.builtins import panic | ||
from tests.util import compile_guppy | ||
|
||
|
||
def test_basic(validate): | ||
@compile_guppy | ||
def main() -> None: | ||
panic("I panicked!") | ||
|
||
validate(main) | ||
|
||
|
||
def test_discard(validate): | ||
@compile_guppy | ||
def main() -> None: | ||
a = 1 + 2 | ||
panic("I panicked!", False, a) | ||
|
||
validate(main) | ||
|
||
|
||
def test_value(validate): | ||
module = GuppyModule("test") | ||
|
||
@guppy(module) | ||
def foo() -> int: | ||
return panic("I panicked!") | ||
|
||
@guppy(module) | ||
def bar() -> tuple[int, float]: | ||
return panic("I panicked!") | ||
|
||
@guppy(module) | ||
def baz() -> None: | ||
return panic("I panicked!") | ||
|
||
validate(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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should
panic
be generic over the type it returns? This would require also implementingcheck
to infer the return type.Imo the proper way to implement this would be to use a bottom return type like Python's
Never
orNoReturn
forpanic
, but that's too much for this PRThere 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 couldn't think of a good reason for panic returning anything other than None, do you think it is important?
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.
For example, it's currently not possible to make the type checker understand that panics are an early exit. So, say you want to implement
Option.unwrap
yourself, then you still need to explicitly return something in the error case: