From 650ee9f3f030504f3dd3b1a4780c6d49f20b83c2 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Sun, 22 Jun 2025 17:57:17 +0800 Subject: [PATCH] Fix ETS_NEVER type problem Issue: #ICGXAH Signed-off-by: oh-rgx --- .../compiler/lowering/ets/unboxLowering.cpp | 3 + .../ast/parser/ets/keyof_predefined_type.ets | 4 +- ets2panda/test/runtime/ets/AdjustType.ets | 95 +++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/AdjustType.ets diff --git a/ets2panda/compiler/lowering/ets/unboxLowering.cpp b/ets2panda/compiler/lowering/ets/unboxLowering.cpp index 1ad03961fd..0173794776 100644 --- a/ets2panda/compiler/lowering/ets/unboxLowering.cpp +++ b/ets2panda/compiler/lowering/ets/unboxLowering.cpp @@ -673,6 +673,9 @@ static ir::Expression *AdjustType(UnboxContext *uctx, ir::Expression *expr, chec expectedType = uctx->checker->GetApparentType(expectedType); checker::Type *actualType = expr->Check(uctx->checker); + if (expectedType->HasTypeFlag(checker::TypeFlag::ETS_NEVER)) { + return expr; + } if (actualType->IsETSPrimitiveType() && checker::ETSChecker::IsReferenceType(expectedType)) { expr = InsertPrimitiveConversionIfNeeded(uctx, expr, expectedType); ES2PANDA_ASSERT( diff --git a/ets2panda/test/ast/parser/ets/keyof_predefined_type.ets b/ets2panda/test/ast/parser/ets/keyof_predefined_type.ets index ca91aaa14d..6700401caf 100644 --- a/ets2panda/test/ast/parser/ets/keyof_predefined_type.ets +++ b/ets2panda/test/ast/parser/ets/keyof_predefined_type.ets @@ -18,5 +18,5 @@ function main():void{ let c2:keyof Int = /* @@ label2 */"field1" } -/* @@@ label1 Error TypeError: Type '"field1"' cannot be assigned to type '"toDouble"|"$_hashCode"|"add"|"toByte"|"unboxed"|"isGreaterThan"|"compareTo"|"toLong"|"createFromJSONValue"|"isLessEqualThan"|"isLessThan"|"toFloat"|"toString"|"mul"|"sub"|"isGreaterEqualThan"|"equals"|"div"|"toInt"|"toShort"|"toChar"|"valueOf"|"MIN_VALUE"|"MAX_VALUE"|"BIT_SIZE"|"BYTE_SIZE"' */ -/* @@@ label2 Error TypeError: Type '"field1"' cannot be assigned to type '"toDouble"|"$_hashCode"|"add"|"toByte"|"unboxed"|"isGreaterThan"|"compareTo"|"toLong"|"createFromJSONValue"|"isLessEqualThan"|"isLessThan"|"toFloat"|"toString"|"mul"|"sub"|"isGreaterEqualThan"|"equals"|"div"|"toInt"|"toShort"|"toChar"|"valueOf"|"MIN_VALUE"|"MAX_VALUE"|"BIT_SIZE"|"BYTE_SIZE"' */ +/* @@@ label1 Error TypeError: Type '"field1"' cannot be assigned to type '"toDouble"|"toLong"|"byteValue"|"doubleValue"|"longValue"|"equals"|"isGreaterEqualThan"|"intValue"|"toInt"|"div"|"shortValue"|"isLessThan"|"toShort"|"toChar"|"createFromJSONValue"|"isLessEqualThan"|"mul"|"sub"|"toString"|"toFloat"|"compareTo"|"isGreaterThan"|"unboxed"|"$_hashCode"|"add"|"toByte"|"floatValue"|"valueOf"|"MIN_VALUE"|"MAX_VALUE"|"BIT_SIZE"|"BYTE_SIZE"' */ +/* @@@ label2 Error TypeError: Type '"field1"' cannot be assigned to type '"toDouble"|"toLong"|"byteValue"|"doubleValue"|"longValue"|"equals"|"isGreaterEqualThan"|"intValue"|"toInt"|"div"|"shortValue"|"isLessThan"|"toShort"|"toChar"|"createFromJSONValue"|"isLessEqualThan"|"mul"|"sub"|"toString"|"toFloat"|"compareTo"|"isGreaterThan"|"unboxed"|"$_hashCode"|"add"|"toByte"|"floatValue"|"valueOf"|"MIN_VALUE"|"MAX_VALUE"|"BIT_SIZE"|"BYTE_SIZE"' */ diff --git a/ets2panda/test/runtime/ets/AdjustType.ets b/ets2panda/test/runtime/ets/AdjustType.ets new file mode 100644 index 0000000000..efe99a8b8d --- /dev/null +++ b/ets2panda/test/runtime/ets/AdjustType.ets @@ -0,0 +1,95 @@ +/* + * 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 fun1() : string { + let input = true; + if (input instanceof Boolean) { + return "Boolean"; + } else { + // Unused variables, for testing the fix + let asddaf = input; + return "No Boolean"; + } +} + +function fun2() : string { + let input = 1.2; + if (input instanceof Double) { + return "Double"; + } else { + // Unused variables, for testing the fix + let asddaf = input; + return "No Double"; + } +} + +function fun3() : string { + let input = 1; + if (input instanceof Int) { + return "Int"; + } else { + // Unused variables, for testing the fix + let asddaf = input; + return "No Int"; + } +} + +function fun4() : string { + let input = "str"; + if (input instanceof String) { + return "String"; + } else { + // Unused variables, for testing the fix + let asddaf = input; + return "No String"; + } +} + +function fun5() : string { + let input = "str"; + if (input instanceof Boolean) { + return "Boolean"; + } else { + return "String"; + } +} + +function fun6() : string { + let input = "str"; + if (input instanceof Double) { + return "Double"; + } else { + return "String"; + } +} + +function fun7() : string { + let input = "str"; + if (input instanceof Int) { + return "Int"; + } else { + return "String"; + } +} + +function main() { + arktest.assertEQ(fun1(), "Boolean"); + arktest.assertEQ(fun2(), "Double"); + arktest.assertEQ(fun3(), "Int"); + arktest.assertEQ(fun4(), "String"); + arktest.assertEQ(fun5(), "String"); + arktest.assertEQ(fun6(), "String"); + arktest.assertEQ(fun7(), "String"); +} -- Gitee