From 01347ab7d79fda3afba8649de4602845e5be2398 Mon Sep 17 00:00:00 2001 From: whitingyan <1712428442@qq.com> Date: Tue, 18 Mar 2025 13:13:36 +0800 Subject: [PATCH 1/2] init --- python/taichi/__init__.py | 2 ++ python/taichi/lang/ast/ast_transformer.py | 32 +++++++++++++------ .../taichi/lang/ast/ast_transformer_utils.py | 9 ++++-- python/taichi/lang/ast/hint.py | 5 +++ 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 python/taichi/lang/ast/hint.py diff --git a/python/taichi/__init__.py b/python/taichi/__init__.py index 034052ed5bd73..0882b41aa3d93 100644 --- a/python/taichi/__init__.py +++ b/python/taichi/__init__.py @@ -16,6 +16,8 @@ # Issue#2223: Do not reorder, or we're busted with partially initialized module from taichi import aot # isort:skip +from taichi.lang.ast.hint import new, hide + def __getattr__(attr): if attr == "cfg": diff --git a/python/taichi/lang/ast/ast_transformer.py b/python/taichi/lang/ast/ast_transformer.py index ba7890d9b6b79..6a2fa6bef7ebc 100644 --- a/python/taichi/lang/ast/ast_transformer.py +++ b/python/taichi/lang/ast/ast_transformer.py @@ -16,6 +16,7 @@ from taichi.lang._ndrange import _Ndrange, ndrange from taichi.lang.argpack import ArgPackType from taichi.lang.ast.ast_transformer_utils import Builder, LoopStatus, ReturnStatus +from taichi.lang.ast import hint from taichi.lang.ast.symbol_resolver import ASTResolver from taichi.lang.exception import ( TaichiIndexError, @@ -105,18 +106,29 @@ def build_assign_annotated(ctx, target, value, is_static_assign, annotation): f'Kernel argument "{target.id}" is immutable in the kernel. ' f"If you want to change its value, please create a new variable." ) - anno = impl.expr_init(annotation) - if is_static_assign: - raise TaichiSyntaxError("Static assign cannot be used on annotated assignment") - if is_local and not ctx.is_var_declared(target.id): - var = ti_ops.cast(value, anno) - var = impl.expr_init(var) + if annotation is hint.new: + var = impl.expr_init(value) ctx.create_variable(target.id, var) + elif annotation is hint.hide: + var = impl.expr_init(value) + ctx.hide_variable(target.id, var) else: - var = build_stmt(ctx, target) - if var.ptr.get_rvalue_type() != anno: - raise TaichiSyntaxError("Static assign cannot have type overloading") - var._assign(value) + anno = impl.expr_init(annotation) + if is_static_assign: + raise TaichiSyntaxError( + "Static assign cannot be used on annotated assignment" + ) + if is_local and not ctx.is_var_declared(target.id): + var = ti_ops.cast(value, anno) + var = impl.expr_init(var) + ctx.create_variable(target.id, var) + else: + var = build_stmt(ctx, target) + if var.ptr.get_rvalue_type() != anno: + raise TaichiSyntaxError( + "Static assign cannot have type overloading" + ) + var._assign(value) return var @staticmethod diff --git a/python/taichi/lang/ast/ast_transformer_utils.py b/python/taichi/lang/ast/ast_transformer_utils.py index 4a10d5e3546c6..b3f96f32ea58b 100644 --- a/python/taichi/lang/ast/ast_transformer_utils.py +++ b/python/taichi/lang/ast/ast_transformer_utils.py @@ -240,10 +240,15 @@ def is_var_declared(self, name): return False def create_variable(self, name, var): - if name in self.current_scope(): - raise TaichiSyntaxError("Recreating variables is not allowed") + # if name in self.current_scope(): + # raise TaichiSyntaxError("Recreating variables is not allowed") self.current_scope()[name] = var + def hide_variable(self, name, var): + for s in reversed(self.local_scopes): + if name in s: + s[name] = var + def check_loop_var(self, loop_var): if self.is_var_declared(loop_var): raise TaichiSyntaxError( diff --git a/python/taichi/lang/ast/hint.py b/python/taichi/lang/ast/hint.py new file mode 100644 index 0000000000000..04731deaea97c --- /dev/null +++ b/python/taichi/lang/ast/hint.py @@ -0,0 +1,5 @@ +from typing import Any + + +type new = Any +type hide = Any From 7a08fb7bf1e2165b3c6377b5e8eb82094f410d22 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Mar 2025 05:34:26 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/lang/ast/ast_transformer.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/python/taichi/lang/ast/ast_transformer.py b/python/taichi/lang/ast/ast_transformer.py index 6a2fa6bef7ebc..8c3cfedd588a1 100644 --- a/python/taichi/lang/ast/ast_transformer.py +++ b/python/taichi/lang/ast/ast_transformer.py @@ -115,9 +115,7 @@ def build_assign_annotated(ctx, target, value, is_static_assign, annotation): else: anno = impl.expr_init(annotation) if is_static_assign: - raise TaichiSyntaxError( - "Static assign cannot be used on annotated assignment" - ) + raise TaichiSyntaxError("Static assign cannot be used on annotated assignment") if is_local and not ctx.is_var_declared(target.id): var = ti_ops.cast(value, anno) var = impl.expr_init(var) @@ -125,9 +123,7 @@ def build_assign_annotated(ctx, target, value, is_static_assign, annotation): else: var = build_stmt(ctx, target) if var.ptr.get_rvalue_type() != anno: - raise TaichiSyntaxError( - "Static assign cannot have type overloading" - ) + raise TaichiSyntaxError("Static assign cannot have type overloading") var._assign(value) return var