diff --git a/ets2panda/compiler/lowering/ets/unboxLowering.cpp b/ets2panda/compiler/lowering/ets/unboxLowering.cpp index 1ad03961fdd765a58aee4ca529b10ce68857a718..0173794776f678c60628106bf2f0a3abfd610894 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 ca91aaa14d05e138436e4e3e8bb44978d79b2b3e..6700401cafcec143c7e3b1fe97292b9e1974e249 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 0000000000000000000000000000000000000000..efe99a8b8d08126a90253500db5d7aa24426cf64 --- /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"); +}