From 09bbb601167f05e719589c46fbaa8035a1120970 Mon Sep 17 00:00:00 2001 From: F001 Date: Fri, 1 Aug 2025 10:34:00 +0800 Subject: [PATCH] fix crash when utility type arg is func type Utility types only accept class or interface as type argument. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQA6K Signed-off-by: F001 --- ets2panda/checker/ets/utilityTypeHandlers.cpp | 4 ++- .../ast/compiler/ets/utility_type_param.ets | 32 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/utility_type_param.ets diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 22cd11c5f0b..fa63695c716 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -57,7 +57,9 @@ Type *ETSChecker::HandleUtilityTypeParameterNode(const ir::TSTypeParameterInstan return baseType; } - if (!baseType->IsETSReferenceType()) { + // T must be a class or an interface type in Partial/Readonly/Required. + if (!baseType->IsETSReferenceType() || baseType->IsETSFunctionType() || baseType->IsETSNullType() || + baseType->IsETSUndefinedType() || baseType->IsETSNeverType() || baseType->IsETSVoidType()) { LogError(diagnostic::UTIL_TYPE_OF_NONREFERENCE, {}, typeParams->Start()); return GlobalTypeError(); } diff --git a/ets2panda/test/ast/compiler/ets/utility_type_param.ets b/ets2panda/test/ast/compiler/ets/utility_type_param.ets new file mode 100644 index 00000000000..f90db62159c --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/utility_type_param.ets @@ -0,0 +1,32 @@ +/* + * 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 FuncType = (x: number) => number; + +function testPartialOnFunction(f: Partial): void { +} + +function testRequiredOnFunction(f: Required): void { +} + +function testReadonlyOnFunction(f: Readonly): void { +} + +function main(): void { +} + +/* @@? 18:42 Error TypeError: Only class or interface types can be converted to utility types. */ +/* @@? 21:44 Error TypeError: Only class or interface types can be converted to utility types. */ +/* @@? 24:44 Error TypeError: Only class or interface types can be converted to utility types. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 920b835f6f1..a53f0a30901 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1404,7 +1404,7 @@ semantic: - name: UTIL_TYPE_OF_NONREFERENCE id: 224 - message: "Only reference types can be converted to utility types." + message: "Only class or interface types can be converted to utility types." - name: VARIABLE_REDECLARED id: 351 -- Gitee