From 4fc170e787c8de65b4f90e8953e14368f50aff25 Mon Sep 17 00:00:00 2001 From: zmw Date: Mon, 28 Jul 2025 21:06:18 +0800 Subject: [PATCH] Fix const unfold dead loop Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPDFJ Description: Fix const unfold dead loop Signed-off-by: zmw Change-Id: If2c2c9cb62750302ca8f4c0ee85fa9c8cbc15fb5 --- .../ets/constantExpressionLowering.cpp | 11 ++++++--- .../lowering/ets/constantExpressionLowering.h | 1 + .../ets/const_unfold_self_dependence01.ets | 18 +++++++++++++++ .../ets/const_unfold_self_dependence02.ets | 23 +++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets create mode 100644 ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index d70dd6580e..4ded165d8a 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -1267,10 +1267,11 @@ static bool IsInTSEnumMemberInit(const ir::AstNode *n) ir::AstNode *ConstantExpressionLowering::UnfoldResolvedReference(ir::AstNode *resolved, ir::AstNode *node) { - if (unfoldingSet_.count(resolved) > 0) { + checker::RecursionPreserver rPreserver(unfoldingSet_, resolved); + if (*rPreserver) { + isSelfDependence_ = true; return node; } - unfoldingSet_.insert(resolved); ir::AstNode *resNode = nullptr; if (resolved->IsClassProperty()) { @@ -1295,7 +1296,11 @@ ir::AstNode *ConstantExpressionLowering::UnfoldResolvedReference(ir::AstNode *re if (resNode != nullptr) { auto res = MaybeUnfold(resNode); - unfoldingSet_.erase(resolved); + if (isSelfDependence_) { + isSelfDependence_ = false; + return node; + } + return res; } diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.h b/ets2panda/compiler/lowering/ets/constantExpressionLowering.h index 531871759f..ee985d150a 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.h +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.h @@ -55,6 +55,7 @@ private: public_lib::Context *context_ {nullptr}; parser::Program *program_ {nullptr}; varbinder::ETSBinder *varbinder_ {nullptr}; + bool isSelfDependence_ = {false}; std::unordered_set unfoldingSet_; }; diff --git a/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets new file mode 100644 index 0000000000..7317988cd7 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence01.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const a = a + 1; + +/* @@? 16:7 Error TypeError: Circular dependency detected for identifier: a */ diff --git a/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets new file mode 100644 index 0000000000..c702b42915 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/const_unfold_self_dependence02.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const b = c + 1; +const c = d + 1; +const d = e + 1; +const e = f + 1; +const f = g + 1; +const g = b + 1; + +/* @@? 16:7 Error TypeError: Circular dependency detected for identifier: b */ -- Gitee