diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 3aa4109c2eb8eb2d86c89482000a32026599fdd0..c77a4836c37726bf71809ec14efeed46ab45c470 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1448,6 +1448,17 @@ static checker::SavedCheckerContext ReconstructOwnerClassContext(ETSChecker *che return SavedCheckerContext(checker, status, owner); } +static bool IsInSelfScope(checker::ETSChecker *const checker, ir::MethodDefinition *const methodDef) +{ + // Prevent endless check: function foo() {foo();} + auto scopeScript = checker->Scope()->Node(); + ir::ScriptFunction *currentScript = nullptr; + if (methodDef->AsMethodDefinition()->Value()->IsFunctionExpression()) { + currentScript = methodDef->AsMethodDefinition()->Value()->AsFunctionExpression()->Function(); + } + return currentScript != nullptr && scopeScript != nullptr && currentScript == scopeScript; +} + checker::Type *ETSAnalyzer::GetCallExpressionReturnType(ir::CallExpression *expr, checker::Type *calleeType) const { ETSChecker *checker = GetETSChecker(); @@ -1478,7 +1489,9 @@ checker::Type *ETSAnalyzer::GetCallExpressionReturnType(ir::CallExpression *expr ES2PANDA_ASSERT(methodDef != nullptr); } ES2PANDA_ASSERT(methodDef->IsMethodDefinition()); - methodDef->Check(checker); + if (!IsInSelfScope(checker, methodDef->AsMethodDefinition())) { + methodDef->Check(checker); + } if (!signature->Function()->HasBody()) { return signature->ReturnType(); diff --git a/ets2panda/test/ast/compiler/ets/recursive_function.ets b/ets2panda/test/ast/compiler/ets/recursive_function.ets new file mode 100644 index 0000000000000000000000000000000000000000..4228041c0b777361bbe0b03ab5d52f6831df74d8 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/recursive_function.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +// this case is from fuzzer, it should be compile success +function foo() { + foo(); + return; +} diff --git a/ets2panda/test/ast/parser/ets/recursive_function.ets b/ets2panda/test/ast/parser/ets/recursive_function.ets new file mode 100644 index 0000000000000000000000000000000000000000..627824c452846613b59b2c5a3ba27be2488f375b --- /dev/null +++ b/ets2panda/test/ast/parser/ets/recursive_function.ets @@ -0,0 +1,25 @@ +/* + * 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. + */ + +// this case is from fuzzer. +function foo() { + foo()/* @@ label */e/* @@ label2 */{ + return; +} + +/* @@@ label Error SyntaxError: Unexpected token 'e'. */ +/* @@@ label Error TypeError: Unresolved reference e */ +/* @@@ label2 Error SyntaxError: Unexpected token '{'. */ +/* @@? 26:1 Error SyntaxError: Expected '}', got 'end of stream'. */