From 3b3a2537ced85c0fe33171bbd34c44340aa102e3 Mon Sep 17 00:00:00 2001 From: zhaoshuting Date: Mon, 11 Aug 2025 09:36:22 +0800 Subject: [PATCH] Cherry-pick !7804 from 0702 to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICUJZ2 Signed-off-by: zhaoshuting --- ets2panda/checker/ets/helpers.cpp | 16 ++++++ .../test/ast/compiler/ets/array_type_test.ets | 25 +++++++++ .../test/runtime/ets/array_type_test.ets | 54 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/array_type_test.ets create mode 100644 ets2panda/test/runtime/ets/array_type_test.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 58e96dfd9c..52c63a3323 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -765,6 +765,22 @@ static bool SetPreferredTypeForExpression(ETSChecker *checker, ir::Identifier *i if (init->IsNumberLiteral() && annotationType != nullptr) { init->SetPreferredType(annotationType); } + if (init->IsBinaryExpression() && annotationType != nullptr) { + auto *binExpr = init->AsBinaryExpression(); + if (binExpr->OperatorType() == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING && + !binExpr->Right()->IsLiteral()) { + binExpr->Right()->SetPreferredType(annotationType); + } + } + if (init->IsConditionalExpression() && annotationType != nullptr) { + auto *conditionalExpr = init->AsConditionalExpression(); + if (!conditionalExpr->Consequent()->IsLiteral()) { + conditionalExpr->Consequent()->SetPreferredType(annotationType); + } + if (!conditionalExpr->Alternate()->IsLiteral()) { + conditionalExpr->Alternate()->SetPreferredType(annotationType); + } + } if (typeAnnotation != nullptr && init->IsArrowFunctionExpression()) { // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) diff --git a/ets2panda/test/ast/compiler/ets/array_type_test.ets b/ets2panda/test/ast/compiler/ets/array_type_test.ets new file mode 100644 index 0000000000..c9d5e6de74 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/array_type_test.ets @@ -0,0 +1,25 @@ +/* + * 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. + */ + +interface Options { + menuList?: [string, number, boolean]; +} + +function hee(opt: Options): [string, number, boolean] { + let menuList: [string, number, boolean] = opt.menuList ?? []; + return menuList; +} + +/* @@? 21:63 Error TypeError: Initializer has 0 elements, but tuple requires 3 */ diff --git a/ets2panda/test/runtime/ets/array_type_test.ets b/ets2panda/test/runtime/ets/array_type_test.ets new file mode 100644 index 0000000000..826e6e4434 --- /dev/null +++ b/ets2panda/test/runtime/ets/array_type_test.ets @@ -0,0 +1,54 @@ +/* + * 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. + */ + +interface OptionsA { + menuList?: Array; +} + +interface OptionsB { + menuList: FixedArray; +} + +function heeA(opt: OptionsA): Array { + let menuList: Array = opt.menuList ?? []; + return menuList; +} + +function heeB(opt: OptionsB): FixedArray { + let menuList: FixedArray = opt.menuList == undefined ? [] : opt.menuList; + return menuList; +} + +function main(): void { + const options1: OptionsA = { menuList: ["Home", "About", "Contact"] }; + const options2: OptionsA = { menuList: [] }; + const options3: OptionsA = {}; + const options4: OptionsA = { menuList: undefined }; + arktest.assertEQ(heeA(options1).length, 3); + arktest.assertEQ(heeA(options1)[0], "Home"); + arktest.assertEQ(heeA(options1)[1], "About"); + arktest.assertEQ(heeA(options1)[2], "Contact"); + arktest.assertEQ(heeA(options2).length, 0); + arktest.assertEQ(heeA(options3).length, 0); + arktest.assertEQ(heeA(options4).length, 0); + + const options5: OptionsB = { menuList: ["Home", "About", "Contact"] }; + const options6: OptionsB = { menuList: [] }; + arktest.assertEQ(heeB(options5).length, 3); + arktest.assertEQ(heeB(options5)[0], "Home"); + arktest.assertEQ(heeB(options5)[1], "About"); + arktest.assertEQ(heeB(options5)[2], "Contact"); + arktest.assertEQ(heeB(options6).length, 0); +} -- Gitee