From 49290fa97c5bdb4230345215c95501547b58b4dd Mon Sep 17 00:00:00 2001 From: Roland Takacs Date: Sat, 30 Aug 2025 16:43:46 +0200 Subject: [PATCH] Prevent infinite recursion due to type aliases Introduce extra checks to catch cases where type aliases reference each other via function types: type foo = () => bar type bar = () => foo These now trigger a compile-time error instead of causing infinite recursion during type resolution. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICVL8H Internal issue: #26796 Signed-off-by: Roland Takacs --- ets2panda/checker/ets/typeCheckingHelpers.cpp | 6 +++++ .../ast/compiler/ets/type-alias-recursion.ets | 24 +++++++++++++++++++ .../ast/compiler/ets/typealias_as_value.ets | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/type-alias-recursion.ets diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 9fac309638..aa12aa2afa 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -817,6 +817,12 @@ bool ETSChecker::IsAllowedTypeAliasRecursion(const ir::TSTypeAliasDeclaration *t return true; }; + if (typeAliasNode->TypeAnnotation()->IsETSFunctionType() && + typeAliasNode->TypeAnnotation()->AsETSFunctionType()->ReturnType()->IsETSTypeReference()) { + isAllowedRerursiveType &= typeAliasDeclarationCheck( + typeAliasNode->TypeAnnotation()->AsETSFunctionType()->ReturnType()->AsETSTypeReference()->Part()); + } + if (typeAliasNode->TypeAnnotation()->IsETSTypeReference()) { isAllowedRerursiveType &= typeAliasDeclarationCheck(typeAliasNode->TypeAnnotation()->AsETSTypeReference()->Part()); diff --git a/ets2panda/test/ast/compiler/ets/type-alias-recursion.ets b/ets2panda/test/ast/compiler/ets/type-alias-recursion.ets new file mode 100644 index 0000000000..5199a8d86f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/type-alias-recursion.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +type foo = () => bar; +type bar = () => foo; + +function baz() : bar { + return baz; +} + +/* @@? 17:1 Error TypeError: Circular type alias reference */ +/* @@? 20:10 Error TypeError: Type '() => () => () => *ERROR_TYPE*' is not compatible with the enclosing method's return type '() => () => *ERROR_TYPE*' */ diff --git a/ets2panda/test/ast/compiler/ets/typealias_as_value.ets b/ets2panda/test/ast/compiler/ets/typealias_as_value.ets index e6f3bb13a7..ee3c8bcf85 100644 --- a/ets2panda/test/ast/compiler/ets/typealias_as_value.ets +++ b/ets2panda/test/ast/compiler/ets/typealias_as_value.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -type RF = (p:RF) => RF +type RF = (p:string) => number function main(){ let v1 = () => {return RF} -- Gitee