diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 9bfdaaba99a91efce6d9d970501c6102e85dcab8..a849952ff192a8391e05bf87bf427a4b70f4fcbc 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -843,8 +843,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 635e4faeafb463e0825d271d9bcd447ab9df52d3..a57fbfe01ed7728766b8163107a26e4395d1bf41 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1261,8 +1261,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); @@ -1275,7 +1274,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)) { @@ -1523,8 +1522,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 0000000000000000000000000000000000000000..fe13b872f6e5cd6f397c8274a2570f0affe4b079 --- /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' */