Skip to content

Commit

Permalink
[Lang] Remove the support for 'is' (taichi-dev#7930)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 661bbce</samp>

Remove `is` and `is not` operators from Taichi scope and update test
case. This change prevents confusion and errors when comparing Taichi
tensors and scalars.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 661bbce</samp>

* Remove support for `is` and `is not` operators in Taichi scope and
raise syntax error instead
([link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL1026-L1027),
[link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-3e22417ffade4af0564893b98dc5101d714b8ba6fd4423ab5bc5129e360fee8fL1034-R1038),
[link](https://github.com/taichi-dev/taichi/pull/7930/files?diff=unified&w=0#diff-8981be068a363e39524dc2e29d28d4c13a097d0037fc3a1176b249ce5bf35ef8L102-R103))
  • Loading branch information
lin-hitonami authored and quadpixels committed May 13, 2023
1 parent 246ebbe commit d235b94
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 56 deletions.
17 changes: 4 additions & 13 deletions python/taichi/lang/ast/ast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,28 +1012,19 @@ def build_Compare(ctx, node):
ops_static = {
ast.In: lambda l, r: l in r,
ast.NotIn: lambda l, r: l not in r,
ast.Is: lambda l, r: l is r,
ast.IsNot: lambda l, r: l is not r,
}
if ctx.is_in_static_scope():
ops = {**ops, **ops_static}
operands = [node.left.ptr] + [comparator.ptr for comparator in node.comparators]
val = True
for i, node_op in enumerate(node.ops):
if isinstance(node_op, (ast.Is, ast.IsNot)):
name = "is" if isinstance(node_op, ast.Is) else "is not"
raise TaichiSyntaxError(f'Operator "{name}" in Taichi scope is not supported.')
l = operands[i]
r = operands[i + 1]
op = ops.get(type(node_op))
if isinstance(node_op, (ast.Is, ast.IsNot)):
name = "is" if isinstance(node_op, ast.Is) else "is not"
warnings.warn_explicit(
f'Operator "{name}" in Taichi scope is deprecated, '
f"and it will be removed in Taichi v1.6.0. "
f"Please avoid using it.",
DeprecationWarning,
ctx.file,
node.lineno + ctx.lineno_offset,
module="taichi",
)

if op is None:
if type(node_op) in ops_static:
raise TaichiSyntaxError(f'"{type(node_op).__name__}" is only supported inside `ti.static`.')
Expand Down
11 changes: 0 additions & 11 deletions tests/python/test_ast_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,6 @@ def foo(a: ti.template()):
assert a[i] == b[i]


@test_utils.test()
def test_compare_fail():
with pytest.raises(ti.TaichiCompilationError, match='"Is" is only supported inside `ti.static`.'):

@ti.kernel
def foo():
None is None

foo()


@test_utils.test()
def test_single_compare():
@ti.kernel
Expand Down
27 changes: 0 additions & 27 deletions tests/python/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,33 +171,6 @@ def foo(a: ti.template()) -> ti.i32:
foo(ti.i32)


@test_utils.test()
def test_static_is():
@ti.kernel
def is_f32(tp: ti.template()) -> ti.i32:
return ti.static(tp is ti.f32)

@ti.kernel
def is_not_f32(tp: ti.template()) -> ti.i32:
return ti.static(tp is not ti.f32)

assert is_f32(ti.f32) == 1
assert is_f32(ti.i32) == 0
assert is_not_f32(ti.f32) == 0
assert is_not_f32(ti.i32) == 1


@test_utils.test()
def test_non_static_is():
with pytest.raises(ti.TaichiCompilationError, match='"Is" is only supported inside `ti.static`.'):

@ti.kernel
def is_f32(tp: ti.template()) -> ti.i32:
return tp is ti.f32

is_f32(ti.f32)


@test_utils.test(default_ip=ti.i64, require=ti.extension.data64)
def test_compare_ret_type():
# The purpose of this test is to make sure a comparison returns i32
Expand Down
7 changes: 2 additions & 5 deletions tests/python/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ def test_deprecate_rwtexture_ndim():


@test_utils.test()
def test_deprecate_is_is_not():
with pytest.warns(
DeprecationWarning,
match='Operator "is" in Taichi scope is deprecated, ' "and it will be removed in Taichi v1.6.0.",
):
def test_remove_is_is_not():
with pytest.raises(ti.TaichiSyntaxError, match='Operator "is" in Taichi scope is not supported'):

@ti.kernel
def func():
Expand Down

0 comments on commit d235b94

Please sign in to comment.