diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 22cd11c5f0b9425aa8162f082780511978759540..fa63695c71631b482e9ffa1daa28599896a01e57 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 0000000000000000000000000000000000000000..f90db62159c2b5cc45f4692a31dbdda74b4816c2 --- /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 920b835f6f19d4cb4ee7ca93c538b30e1092e7df..a53f0a30901ab6818b6ee9db9790fc102dc38bbb 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