From 319e8eccb7c0b5cf6d3c47bbca89082c379cc234 Mon Sep 17 00:00:00 2001 From: "584648456@qq.com" Date: Sat, 19 Apr 2025 14:18:47 +0800 Subject: [PATCH] Title:bug fix #DTS2025031837133 Description(optional):Added reference checking recursive processing Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IC2EBA Signed-off-by: semon <584648456@qq.com> --- ets2panda/checker/ETSAnalyzer.cpp | 14 ++++++++++ .../ets/arrow_function_recursion_1.ets | 26 +++++++++++++++++++ .../ets/arrow_function_recursion_2.ets | 26 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 ets2panda/test/runtime/ets/arrow_function_recursion_1.ets create mode 100644 ets2panda/test/runtime/ets/arrow_function_recursion_2.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 5e0df49a9b..5389783229 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -877,6 +877,19 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const return expr->TsType(); } +static void ArrowFunctionRecursiveHandel(const ir::ArrowFunctionExpression *expr, Signature *signature, + ETSChecker *checker) +{ + auto *parent = expr->Parent(); + auto *variable = parent->IsVariableDeclarator() ? parent->AsVariableDeclarator()->Id()->AsIdentifier()->Variable() + : parent->IsClassProperty() + ? parent->AsClassProperty()->AsClassElement()->Key()->AsIdentifier()->Variable() + : nullptr; + if (variable != nullptr && variable->TsType() == nullptr) { + variable->SetTsType(checker->CreateETSArrowType(signature)); + } +} + checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const { ETSChecker *checker = GetETSChecker(); @@ -928,6 +941,7 @@ checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const auto *signature = expr->Function()->Signature(); checker->Context().SetContainingSignature(signature); + ArrowFunctionRecursiveHandel(expr, signature, checker); expr->Function()->Body()->Check(checker); auto *funcType = checker->CreateETSArrowType(signature); diff --git a/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets b/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets new file mode 100644 index 0000000000..3d738efa16 --- /dev/null +++ b/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets @@ -0,0 +1,26 @@ +/* + * 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 calculateFibonacci = (n : number): number => { + if (n === 0 || n === 1){ + return n + } else { + return calculateFibonacci(n - 1) + calculateFibonacci( n - 2) + } +} + + +const result = calculateFibonacci(9) +console.log("test calculateFibonacci(9)", result) \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets b/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets new file mode 100644 index 0000000000..e238835fd5 --- /dev/null +++ b/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets @@ -0,0 +1,26 @@ +/* + * 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. + */ + +function main(){ +const calculateFibonacci = (n : number): number => { + if (n === 0 || n === 1){ + return n + } else { + return calculateFibonacci(n - 1) + calculateFibonacci( n - 2) + } + } + const result = calculateFibonacci(9) + console.log("test calculateFibonacci(9)", result) +} \ No newline at end of file -- Gitee