Skip to content

Commit

Permalink
fix: Allow string py expressions in result and panic (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatiana-s authored Jan 8, 2025
1 parent 821771a commit 53401cc
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 12 deletions.
3 changes: 3 additions & 0 deletions guppylang/std/_internal/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
is_sized_iter_type,
nat_type,
sized_iter_type,
string_type,
)
from guppylang.tys.const import Const, ConstValue
from guppylang.tys.subst import Inst, Subst
Expand Down Expand Up @@ -318,6 +319,7 @@ class Hint(Note):
def synthesize(self, args: list[ast.expr]) -> tuple[ast.expr, Type]:
check_num_args(2, len(args), self.node)
[tag, value] = args
tag, _ = ExprChecker(self.ctx).check(tag, string_type())
if not isinstance(tag, ast.Constant) or not isinstance(tag.value, str):
raise GuppyTypeError(ExpectedError(tag, "a string literal"))
if len(tag.value.encode("utf-8")) > TAG_MAX_LEN:
Expand Down Expand Up @@ -375,6 +377,7 @@ def synthesize(self, args: list[ast.expr]) -> tuple[ast.expr, Type]:
err.add_sub_diagnostic(PanicChecker.NoMessageError.Suggestion(None))
raise GuppyTypeError(err)
case [msg, *rest]:
msg, _ = ExprChecker(self.ctx).check(msg, string_type())
if not isinstance(msg, ast.Constant) or not isinstance(msg.value, str):
raise GuppyTypeError(ExpectedError(msg, "a string literal"))

Expand Down
4 changes: 2 additions & 2 deletions tests/error/misc_errors/panic_msg_not_str.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Expected a string literal (at $FILE:7:10)
Error: Type mismatch (at $FILE:7:10)
|
5 | @compile_guppy
6 | def foo(x: int) -> None:
7 | panic((), x)
| ^^ Expected a string literal
| ^^ Expected expression of type `str`, got `()`

Guppy compilation failed due to 1 previous error
4 changes: 2 additions & 2 deletions tests/error/misc_errors/panic_tag_not_static.err
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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
7 | panic("foo" if y else "bar", y)
| ^^^^^^^^^^^^^^^^^^^^^ Expected a string literal

Guppy compilation failed due to 1 previous error
2 changes: 1 addition & 1 deletion tests/error/misc_errors/panic_tag_not_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

@compile_guppy
def foo(y: bool) -> None:
panic("foo" + "bar", y)
panic("foo" if y else "bar", y)
6 changes: 3 additions & 3 deletions tests/error/misc_errors/result_tag_not_static.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Expected a string literal (at $FILE:7:11)
|
5 | @compile_guppy
6 | def foo(y: bool) -> None:
7 | result("foo" + "bar", y)
| ^^^^^^^^^^^^^ Expected a string literal
6 | def foo(y: bool, x: int) -> None:
7 | result("foo" if y else "bar", x)
| ^^^^^^^^^^^^^^^^^^^^^ Expected a string literal

Guppy compilation failed due to 1 previous error
4 changes: 2 additions & 2 deletions tests/error/misc_errors/result_tag_not_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@


@compile_guppy
def foo(y: bool) -> None:
result("foo" + "bar", y)
def foo(y: bool, x: int) -> None:
result("foo" if y else "bar", x)
4 changes: 2 additions & 2 deletions tests/error/misc_errors/result_tag_not_str.err
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Error: Expected a string literal (at $FILE:7:11)
Error: Type mismatch (at $FILE:7:11)
|
5 | @compile_guppy
6 | def foo(x: int) -> None:
7 | result((), x)
| ^^ Expected a string literal
| ^^ Expected expression of type `str`, got `()`

Guppy compilation failed due to 1 previous error
7 changes: 7 additions & 0 deletions tests/integration/test_panic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ def baz() -> None:
return panic("I panicked!")

validate(module.compile())

def test_py_message(validate):
@compile_guppy
def main(x: int) -> None:
panic(py("I" + "panicked" + "!"))

validate(main)
7 changes: 7 additions & 0 deletions tests/integration/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ def main(x: int, y: float, z: bool) -> None:
result("foo", z)

validate(main)

def test_py_tag(validate):
@compile_guppy
def main(x: int) -> None:
result(py("a" + "b"), x)

validate(main)

0 comments on commit 53401cc

Please sign in to comment.