Skip to content

Commit 269396f

Browse files
committed
Reject non-None kernel return annotations (GH-1471)
Signed-off-by: Sebastian Nicolas <snicolas432@gmail.com>
1 parent 6655543 commit 269396f

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
([GH-1466](https://github.com/NVIDIA/warp/issues/1466)).
152152
- Fix tile byte-offset overflow for arrays larger than 2 GiB
153153
([GH-1422](https://github.com/NVIDIA/warp/issues/1422)).
154+
- Raise an error when a kernel is defined with a non-`None` return annotation ([GH-1471](https://github.com/NVIDIA/warp/issues/1471)).
154155

155156
### Documentation
156157

warp/_src/context.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,12 @@ def build_kernel(self, kernel):
22882288
if kernel.adj.return_var is not None:
22892289
raise WarpCodegenTypeError(f"'{kernel.key}': Error, kernels can't have return values")
22902290

2291+
if kernel.adj.arg_types.get("return") is not None:
2292+
raise WarpCodegenTypeError(
2293+
f"'{kernel.key}': Error, kernels can't have return values; "
2294+
"write results to output arguments and either omit the return annotation or use '-> None'"
2295+
)
2296+
22912297
def build_function(self, func):
22922298
if func in self.functions:
22932299
return

warp/tests/test_codegen.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,16 @@ def f3(x: float):
565565
with test.assertRaisesRegex(wp.WarpCodegenTypeError, r".*Error, kernels can't have return values"):
566566
wp.launch(f3, dim=1, inputs=[3.0], device=device)
567567

568-
# TODO: specifying a return type without returning a value is benign, but should be reported to avoid confusion
569-
# @wp.kernel
570-
# def f4(x: float) -> float:
571-
# return
568+
@wp.kernel(module="unique")
569+
def f4(x: float) -> float:
570+
return
572571

573-
# with test.assertRaisesRegex(wp.WarpCodegenTypeError, r".*Error, kernels can't have return values"):
574-
# wp.launch(f4, dim=1, inputs=[3.0], device=device)
572+
with test.assertRaisesRegex(
573+
wp.WarpCodegenTypeError,
574+
r".*Error, kernels can't have return values; "
575+
r"write results to output arguments and either omit the return annotation or use '-> None'",
576+
):
577+
wp.launch(f4, dim=1, inputs=[3.0], device=device)
575578

576579

577580
def test_error_mutating_constant_in_dynamic_loop(test, device):

0 commit comments

Comments
 (0)