2022-10-11 18:19:41 +08:00
|
|
|
import platform
|
|
|
|
|
|
2020-10-26 00:20:44 -04:00
|
|
|
import pytest
|
|
|
|
|
|
2021-06-23 17:12:25 +08:00
|
|
|
import taichi as ti
|
2022-02-10 12:37:36 +08:00
|
|
|
from tests import test_utils
|
2021-06-23 17:12:25 +08:00
|
|
|
|
2020-10-26 00:20:44 -04:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=ti.cpu)
|
2020-10-26 00:20:44 -04:00
|
|
|
def test_pass_float_as_i32():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.i32):
|
|
|
|
|
pass
|
|
|
|
|
|
2022-03-29 16:31:13 +08:00
|
|
|
with pytest.raises(
|
|
|
|
|
ti.TaichiRuntimeTypeError,
|
2023-07-10 19:22:00 +08:00
|
|
|
match=r"Argument \(0,\) \(type=<class 'float'>\) cannot be converted into required type i32",
|
2022-03-29 16:31:13 +08:00
|
|
|
) as e:
|
2020-10-26 00:20:44 -04:00
|
|
|
foo(1.2)
|
[Lang] Make scalar kernel arguments immutable (#5990)
Related issue = fixes #5619
<!--
Thank you for your contribution!
If it is your first time contributing to Taichi, please read our
Contributor Guidelines:
https://docs.taichi-lang.org/docs/contributor_guide
- Please always prepend your PR title with tags such as [CUDA], [Lang],
[Doc], [Example]. For a complete list of valid PR tags, please check out
https://github.com/taichi-dev/taichi/blob/master/misc/prtags.json.
- Use upper-case tags (e.g., [Metal]) for PRs that change public APIs.
Otherwise, please use lower-case tags (e.g., [metal]).
- More details:
https://docs.taichi-lang.org/docs/contributor_guide#pr-title-format-and-tags
- Please fill in the issue number that this PR relates to.
- If your PR fixes the issue **completely**, use the `close` or `fixes`
prefix so that GitHub automatically closes the issue when the PR is
merged. For example,
Related issue = close #2345
- If the PR does not belong to any existing issue, free to leave it
blank.
-->
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-09-13 18:16:55 +08:00
|
|
|
|
|
|
|
|
|
2022-11-03 14:01:27 +08:00
|
|
|
@test_utils.test(arch=ti.cpu)
|
|
|
|
|
def test_pass_float_as_ndarray():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.types.ndarray()):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ti.TaichiRuntimeTypeError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid type for argument a, got 1.2",
|
2022-11-03 14:01:27 +08:00
|
|
|
):
|
|
|
|
|
foo(1.2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=ti.cpu)
|
|
|
|
|
def test_random_python_class_as_ndarray():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.types.ndarray()):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class Bla:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ti.TaichiRuntimeTypeError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid type for argument a, got",
|
2022-11-03 14:01:27 +08:00
|
|
|
):
|
|
|
|
|
b = Bla()
|
|
|
|
|
foo(b)
|
|
|
|
|
|
|
|
|
|
|
2022-10-11 18:19:41 +08:00
|
|
|
@test_utils.test(exclude=[ti.metal])
|
|
|
|
|
def test_pass_u64():
|
|
|
|
|
if ti.lang.impl.current_cfg().arch == ti.vulkan and platform.system() == "Darwin":
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.u64):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
foo(2**64 - 1)
|
|
|
|
|
|
|
|
|
|
|
[Lang] Make scalar kernel arguments immutable (#5990)
Related issue = fixes #5619
<!--
Thank you for your contribution!
If it is your first time contributing to Taichi, please read our
Contributor Guidelines:
https://docs.taichi-lang.org/docs/contributor_guide
- Please always prepend your PR title with tags such as [CUDA], [Lang],
[Doc], [Example]. For a complete list of valid PR tags, please check out
https://github.com/taichi-dev/taichi/blob/master/misc/prtags.json.
- Use upper-case tags (e.g., [Metal]) for PRs that change public APIs.
Otherwise, please use lower-case tags (e.g., [metal]).
- More details:
https://docs.taichi-lang.org/docs/contributor_guide#pr-title-format-and-tags
- Please fill in the issue number that this PR relates to.
- If your PR fixes the issue **completely**, use the `close` or `fixes`
prefix so that GitHub automatically closes the issue when the PR is
merged. For example,
Related issue = close #2345
- If the PR does not belong to any existing issue, free to leave it
blank.
-->
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-09-13 18:16:55 +08:00
|
|
|
@test_utils.test()
|
|
|
|
|
def test_argument_redefinition():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.i32):
|
|
|
|
|
a = 1
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match='Kernel argument "a" is immutable in the kernel') as e:
|
|
|
|
|
foo(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_argument_augassign():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.i32):
|
|
|
|
|
a += 1
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match='Kernel argument "a" is immutable in the kernel') as e:
|
|
|
|
|
foo(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_argument_annassign():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.i32):
|
|
|
|
|
a: ti.i32 = 1
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match='Kernel argument "a" is immutable in the kernel') as e:
|
|
|
|
|
foo(5)
|
2023-06-09 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_pass_struct_mismatch():
|
|
|
|
|
sphere_type = ti.types.struct(center=ti.math.vec3, radius=float)
|
|
|
|
|
circle_type = ti.types.struct(center=ti.math.vec2, radius=float)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(sphere: sphere_type):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ti.TaichiRuntimeTypeError,
|
2023-07-10 19:22:09 +08:00
|
|
|
match=r"Argument <class 'taichi.lang.struct.Struct.* cannot be converted into required type <ti"
|
2023-07-10 19:22:00 +08:00
|
|
|
r".StructType center=<taichi.lang.matrix.VectorType object at .*>, radius=f32, struct_methods={}>",
|
2023-06-09 16:08:54 +08:00
|
|
|
) as e:
|
|
|
|
|
foo(circle_type(center=ti.math.vec2([1, 2]), radius=2.33))
|