From d3f548e40a30c97676faa49b5f3270d6e6f32355 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Tue, 29 Jul 2025 14:24:41 +0800 Subject: [PATCH] Adapt undefined type in CreatePartialType Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPI4P Signed-off-by: yp9522 --- ets2panda/checker/ets/utilityTypeHandlers.cpp | 13 +++--- .../ets/partial_type_undefined_fixed_test.ets | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/partial_type_undefined_fixed_test.ets diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 93f6b5eccc..15020036b1 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -123,9 +123,6 @@ static T *CloneNodeIfNotNullptr(T *node, ArenaAllocator *allocator) Type *ETSChecker::CreatePartialType(Type *const typeToBePartial) { ES2PANDA_ASSERT(typeToBePartial->IsETSReferenceType()); - if (typeToBePartial->IsTypeError() || typeToBePartial->IsETSNeverType() || typeToBePartial->IsETSAnyType()) { - return typeToBePartial; - } if (typeToBePartial->IsGradualType()) { // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) @@ -141,9 +138,13 @@ Type *ETSChecker::CreatePartialType(Type *const typeToBePartial) return HandleUnionForPartialType(typeToBePartial->AsETSUnionType()); } - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - return CreatePartialTypeClass(typeToBePartial->AsETSObjectType(), - typeToBePartial->Variable()->Declaration()->Node()); + if (typeToBePartial->IsETSObjectType()) { + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + return CreatePartialTypeClass(typeToBePartial->AsETSObjectType(), + typeToBePartial->Variable()->Declaration()->Node()); + } + + return typeToBePartial; } Type *ETSChecker::CreatePartialTypeParameter(ETSTypeParameter *typeToBePartial) diff --git a/ets2panda/test/ast/compiler/ets/partial_type_undefined_fixed_test.ets b/ets2panda/test/ast/compiler/ets/partial_type_undefined_fixed_test.ets new file mode 100644 index 0000000000..c29db7bce1 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/partial_type_undefined_fixed_test.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023-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. + */ + +/*--- +desc: Test handling of undefined in mapped generic types (corrected) +name: generics_mapped_undefined1_fixed +---*/ + +class A { + value: string; + constructor(value: string) { + this.value = value; + } +} + +class B { + foo(): Partial { + return undefined; + } +} + +function main(): void { + let b = new B(); + let result = b.foo(); + assertEQ(result === undefined, true); +} + +/* @@? 30:16 Error TypeError: Type 'undefined' is not compatible with the enclosing method's return type 'Partial' */ +/* @@? 37:5 Error TypeError: Unresolved reference assertEQ */ \ No newline at end of file -- Gitee