From 23db35a91bb83593045f9df45734b9b73c8f6a82 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Mon, 14 Jul 2025 11:29:19 +0800 Subject: [PATCH] Fixed a bug when compiling with es2panda Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICM1L4 Signed-off-by: yp9522 --- ets2panda/checker/ets/utilityTypeHandlers.cpp | 8 +++ .../runtime/ets/create_partial_type_test.ets | 67 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 ets2panda/test/runtime/ets/create_partial_type_test.ets diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 076bdb34bd..97037ce309 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -142,6 +142,14 @@ Type *ETSChecker::CreatePartialType(Type *const typeToBePartial) return HandleUnionForPartialType(typeToBePartial->AsETSUnionType()); } + if (typeToBePartial->AsETSObjectType() == nullptr) { + return GlobalTypeError(); + } + + if (typeToBePartial->Variable() == nullptr) { + return GlobalTypeError(); + } + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) return CreatePartialTypeClass(typeToBePartial->AsETSObjectType(), typeToBePartial->Variable()->Declaration()->Node()); diff --git a/ets2panda/test/runtime/ets/create_partial_type_test.ets b/ets2panda/test/runtime/ets/create_partial_type_test.ets new file mode 100755 index 0000000000..098f4f9235 --- /dev/null +++ b/ets2panda/test/runtime/ets/create_partial_type_test.ets @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024-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. + */ + +interface Foo { + a: number; + b: number; + bar: string; +} + +interface AAndFoo { + a: number; + foo: number; +} + +type GenericFunction = (sample: Partial) => Partial; + +class ObjectContaining { + private fn: GenericFunction; + + constructor(fn: GenericFunction) { + this.fn = fn; + } + + public call>(sample: U): Partial { + return this.fn(sample); + } +} + +declare let cafoo: ObjectContaining; +declare let cfuu: ObjectContaining; + +function genericImpl(sample: Partial): Partial { + return sample; +} + +class FunctionWrapper { + private fn: GenericFunction; + + constructor(fn: GenericFunction) { + this.fn = fn; + } + + public getFunction(): GenericFunction { + return this.fn; + } +} + +function createGenericFunction(): GenericFunction { + return (sample: Partial): Partial => genericImpl(sample); +} + +function adaptFoo(): ObjectContaining { + const helper = new FunctionWrapper(createGenericFunction()); + return new ObjectContaining(helper.getFunction()); +} \ No newline at end of file -- Gitee