From c01fdcdfaf0ffeed9a0b3fa8c2dbbdd4ccae90eb Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Tue, 26 Aug 2025 19:47:21 +0800 Subject: [PATCH] Fix never type cast crause crash Issue: #ICUXAE Signed-off-by: oh-rgx --- ets2panda/checker/ETSchecker.h | 3 +-- ets2panda/checker/ets/helpers.cpp | 8 +++--- .../test/ast/compiler/ets/never_type_cast.ets | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/never_type_cast.ets diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 490d90f351..de4af4f3f8 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -819,8 +819,7 @@ public: [[nodiscard]] checker::Type *ResolveSmartType(checker::Type *sourceType, checker::Type *targetType, std::optional value = std::nullopt); [[nodiscard]] std::pair CheckTestNullishCondition(Type *testedType, Type *actualType, bool strict); - [[nodiscard]] std::pair CheckTestObjectCondition(ETSObjectType *testedType, Type *actualType, - bool strict); + [[nodiscard]] std::pair CheckTestObjectCondition(ETSObjectType *testedType, Type *actualType); [[nodiscard]] std::pair CheckTestObjectCondition(ETSArrayType *testedType, Type *actualType); void ApplySmartCast(varbinder::Variable const *variable, checker::Type *smartType) noexcept; diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 58e96dfd9c..77e246ead2 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1256,8 +1256,7 @@ std::pair ETSChecker::CheckTestObjectCondition(ETSArrayType *tes } // Auxiliary method to reduce the size of common 'CheckTestSmartCastConditions' function. -std::pair ETSChecker::CheckTestObjectCondition(ETSObjectType *testedType, Type *actualType, - bool const strict) +std::pair ETSChecker::CheckTestObjectCondition(ETSObjectType *testedType, Type *actualType) { if (actualType->IsETSUnionType()) { return actualType->AsETSUnionType()->GetComplimentaryType(this, testedType); @@ -1270,7 +1269,7 @@ std::pair ETSChecker::CheckTestObjectCondition(ETSObjectType *te if (Relation()->IsIdenticalTo(objectType, testedType) || objectType->AssemblerName() == testedType->AssemblerName()) { - return {testedType, strict ? GetGlobalTypesHolder()->GlobalETSNeverType() : actualType}; + return {testedType, actualType}; } if (Relation()->IsSupertypeOf(objectType, testedType)) { @@ -1518,8 +1517,7 @@ std::optional CheckerContext::ResolveSmartCastTypes() checker->CheckTestNullishCondition(testCondition_.testedType, smartType, testCondition_.strict); } else if (testCondition_.testedType->IsETSObjectType()) { auto *const testedType = testCondition_.testedType->AsETSObjectType(); - std::tie(consequentType, alternateType) = - checker->CheckTestObjectCondition(testedType, smartType, testCondition_.strict); + std::tie(consequentType, alternateType) = checker->CheckTestObjectCondition(testedType, smartType); } else if (testCondition_.testedType->IsETSArrayType()) { auto *const testedType = testCondition_.testedType->AsETSArrayType(); std::tie(consequentType, alternateType) = checker->CheckTestObjectCondition(testedType, smartType); diff --git a/ets2panda/test/ast/compiler/ets/never_type_cast.ets b/ets2panda/test/ast/compiler/ets/never_type_cast.ets new file mode 100644 index 0000000000..fe13b872f6 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/never_type_cast.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +function foo() { + let input = true; + if (input instanceof Boolean) { + console.log("This is a test function"); + } else { + let b = input as (string | number | null); + console.log("This is a test function with b: " + b); + } +} +foo(); + +/* @@? 21:17 Error TypeError: Cannot cast type 'Boolean' to 'String|Double|null' */ -- Gitee