diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 6113f3e558ccd32adfa17323cf060c3734ccc708..9a831ccdf35c2082e808eb06f843aac964756372 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -772,6 +772,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 0000000000000000000000000000000000000000..c9d5e6de74a2a78ac3481c6952467ba89fb72af4 --- /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 0000000000000000000000000000000000000000..826e6e4434df4fd8955708735e580ebf08680e91 --- /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); +}