diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index d70dd6580e128c896032c0d83853498d75726e41..4ded165d8a0431bcae0ca01e1e66bfbb6939c289 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 531871759fc22641437a042d9ec846634f106f64..ee985d150aa5ecaaf932e05ebfd31d0fde23c577 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 0000000000000000000000000000000000000000..7317988cd7d970e2846351140271eb6c7c544d00 --- /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 0000000000000000000000000000000000000000..c702b429150baf91107b070a4bdb267bf5de6fd7 --- /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 */