Skip to content
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

[Error] Return NotImplemented for operations between field and Expr/Matrix/Struct #6474

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/taichi/lang/ast/ast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def build_BinOp(ctx, node):
try:
node.ptr = op(node.left.ptr, node.right.ptr)
except TypeError as e:
raise TaichiTypeError(str(e))
raise TaichiTypeError(str(e)) from None
return node.ptr

@staticmethod
Expand Down
8 changes: 8 additions & 0 deletions python/taichi/lang/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from taichi._lib import core as _ti_core
from taichi.lang import expr, impl
from taichi.lang.exception import TaichiSyntaxError
from taichi.lang.field import Field
from taichi.lang.util import cook_dtype, is_taichi_class, taichi_scope

unary_ops = []
Expand Down Expand Up @@ -51,6 +52,8 @@ def rev_foo(x, y):

@functools.wraps(foo)
def wrapped(a, b):
if isinstance(a, Field) or isinstance(b, Field):
return NotImplemented
if is_taichi_class(a):
return a._element_wise_binary(imp_foo, b)
if is_taichi_class(b):
Expand Down Expand Up @@ -79,6 +82,9 @@ def cab_foo(c, a, b):

@functools.wraps(foo)
def wrapped(a, b, c):
if isinstance(a, Field) or isinstance(b, Field) or isinstance(
c, Field):
return NotImplemented
if is_taichi_class(a):
return a._element_wise_ternary(abc_foo, b, c)
if is_taichi_class(b):
Expand All @@ -101,6 +107,8 @@ def imp_foo(x, y):

@functools.wraps(foo)
def wrapped(a, b):
if isinstance(a, Field) or isinstance(b, Field):
return NotImplemented
if is_taichi_class(a):
return a._element_wise_writeback_binary(imp_foo, b)
if is_taichi_class(b):
Expand Down
13 changes: 13 additions & 0 deletions tests/python/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,16 @@ def test_python_for_in():
match="Struct for is only available in Taichi scope"):
for i in x:
pass


@test_utils.test()
def test_matrix_mult_field():
x = ti.field(int, shape=())
with pytest.raises(ti.TaichiTypeError, match="unsupported operand type"):

@ti.kernel
def foo():
a = ti.Vector([1, 1, 1])
b = a * x

foo()