diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 97b5f76d8bd976d910878cb9f7089e0866f5bf64..b927ff0afe924d258ec4e245a599525f8585d695 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 0000000000000000000000000000000000000000..a71dab09ef27a171bd6d4bc5711a34d0e4d36113 --- /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 0000000000000000000000000000000000000000..2e6298bed6276f09cbe1fd51f00daa63994efbee --- /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) +}