From 924efeb434d73ab77cb0077cb40acd66c8041027 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Fri, 8 Aug 2025 16:54:48 +0800 Subject: [PATCH] Cherry-pick bug-fix !7783 to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSALN Signed-off-by: anjiaqi --- ets2panda/checker/types/ets/etsUnionType.cpp | 2 +- ...TypeParamWithConstrainedGenericType_01.ets | 39 ++++++++++++++++++ ...TypeParamWithConstrainedGenericType_02.ets | 40 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_01.ets create mode 100644 ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_02.ets diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 97b5f76d8b..b927ff0afe 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -426,8 +426,8 @@ bool ETSUnionType::ExtractType(checker::ETSChecker *checker, checker::Type *sour rc = true; if (!(*it)->IsETSTypeParameter()) { it = unionTypes.erase(it); + continue; } - continue; } if (checker->Relation()->IsSupertypeOf(constituentType, source)) { diff --git a/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_01.ets b/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_01.ets new file mode 100644 index 0000000000..a71dab09ef --- /dev/null +++ b/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_01.ets @@ -0,0 +1,39 @@ +/* + * 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. + */ + +class A { +} + +class B extends A { +} + +function foo(a: T | string): T | string { + if (a instanceof A) { + const x: T = a as T; + return x; + } + else { + const y: string = a as string; + return y; + } +} +function main(){ + const a = new A(); + arktest.assertEQ(foo(a), a) + const b = new B(); + arktest.assertEQ(foo(b), b) + arktest.assertEQ(foo(""), "") + arktest.assertEQ(foo("123"), "123") +} \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_02.ets b/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_02.ets new file mode 100644 index 0000000000..2e6298bed6 --- /dev/null +++ b/ets2panda/test/runtime/ets/unionTypeParamWithConstrainedGenericType_02.ets @@ -0,0 +1,40 @@ +/* + * 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. + */ + +class A { + x: string | undefined +} + +interface I { + x: string +} + +function foo(a: T | I): T | I { + if (a instanceof A) { + const x: T = a as T; + return x; + } + else { + const y: I = a as I; + return y; + } +} + +function main(){ + const a = new A(); + arktest.assertEQ(foo(a), a) + const i: I = {x: "a"}; + arktest.assertEQ(foo(i), i) +} -- Gitee