From e3ba1298e1b70c6d7a66b565ad3836cab9822f23 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 16 Jul 2025 14:52:23 +0800 Subject: [PATCH] fix bug for instanceof Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMNU9 Signed-off-by: yp9522 --- ets2panda/checker/types/ets/etsUnionType.cpp | 18 +--- .../test/runtime/ets/instanceof_match.ets | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+), 15 deletions(-) create mode 100755 ets2panda/test/runtime/ets/instanceof_match.ets diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 3bfeecc8cc..273cb27b5a 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -427,31 +427,19 @@ bool ETSUnionType::ExtractType(checker::ETSChecker *checker, checker::Type *sour rc = true; if (!(*it)->IsETSTypeParameter()) { it = unionTypes.erase(it); + continue; } + ++it; continue; } if (checker->Relation()->IsSupertypeOf(constituentType, source)) { rc = true; - } else if (!rc && constituentType->IsBuiltinNumeric()) { - if (auto const id = ETSObjectType::GetPrecedence(checker, constituentType->AsETSObjectType()); id > 0U) { - numericTypes.emplace(id, it); - } } ++it; } - - if (rc) { - return true; - } - - if (source->IsBuiltinNumeric() && !numericTypes.empty()) { - unionTypes.erase((*std::prev(numericTypes.end())).second); - return true; - } - - return false; + return rc; } std::pair ETSUnionType::GetComplimentaryType(ETSChecker *const checker, diff --git a/ets2panda/test/runtime/ets/instanceof_match.ets b/ets2panda/test/runtime/ets/instanceof_match.ets new file mode 100755 index 0000000000..952b928611 --- /dev/null +++ b/ets2panda/test/runtime/ets/instanceof_match.ets @@ -0,0 +1,82 @@ +/* + * 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 T1 = string | long | int | double +function test_T1(xxxx : T1, index : int) : void{ + if (xxxx instanceof int) { + arktest.assertEQ(1, index, "instanceof int") + } else if (xxxx instanceof double) { + arktest.assertEQ(2, index, "instanceof double") + } else if (xxxx instanceof number) { + arktest.assertEQ(3, index, "instanceof number") + } else if (xxxx instanceof string) { + arktest.assertEQ(4, index, "instanceof string") + } else if (xxxx instanceof long){ + arktest.assertEQ(5, index, "instanceof long") + } +} + +type T2 = string | number +function test_T2(xxxx : T2, index : int) : void{ + if (xxxx instanceof int) { + arktest.assertEQ(1, index, "instanceof int") + } else if (xxxx instanceof number) { + arktest.assertEQ(2, index, "instanceof number") + } else if (xxxx instanceof string) { + arktest.assertEQ(3, index, "instanceof string") + } else if (xxxx instanceof object) { + arktest.assertEQ(4, index, "instanceof object") + } +} + +class A {} +class B {} +class A1 extends A {} + +type T3 = A | B | string +function test_T3(xxxx : T3, index : int) : void{ + if (xxxx instanceof B) { + arktest.assertEQ(1, index, "instanceof B") + } else if (xxxx instanceof A1) { + arktest.assertEQ(2, index, "instanceof A1") + } else if (xxxx instanceof A) { + arktest.assertEQ(3, index, "instanceof A") + } else if (xxxx instanceof string) { + arktest.assertEQ(4, index, "instanceof string") + } +} + +function main():void { + let v1 : int = 32; + test_T1(v1, 1) + let v2 : long = 100; + test_T1(v2, 5) + let v3 : double = 0.5; + test_T1(v3, 2) + let v4 : string = "wwwwwww"; + test_T1(v4, 4) + + test_T2(33343, 2) + test_T2(0.6, 2) + test_T2("eeeeeeee", 3) + + let a : A = new A() + let b : B = new B() + let c : A1 = new A1() + test_T3(a, 3) + test_T3(b, 1) + test_T3(c, 2) + test_T3("sssssssss", 4) +} -- Gitee