From 17ca4e7047e0084deb1d260e4f263c1ca0c19f3d Mon Sep 17 00:00:00 2001 From: zengzengran Date: Sun, 29 Jun 2025 11:50:47 +0800 Subject: [PATCH] Empty array expression type cannot be infered Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJPTN Description: According to specification 7.4.2 Array Type Inference from Types of Elements, an empty array expression without a type annotation cannot be inferred a type and should report CTE. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/checker/ets/helpers.cpp | 7 +++- .../compiler/ets/empty_array_expression.ets | 40 +++++++++++++++++++ .../ets/inferTypeOfArrayNegative2.ets | 6 ++- .../ets/inferTypeOfArray-expected.txt | 4 ++ .../test/parser/ets/literals-expected.txt | 1 + 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/empty_array_expression.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index a9b83658ec..6c6997b85c 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -551,8 +551,11 @@ checker::Type *ETSChecker::CheckArrayElements(ir::ArrayExpression *init) } if (elementTypes.empty()) { - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - return ProgramAllocator()->New(GlobalETSObjectType()); + if (init->GetPreferredType() != nullptr) { + return init->GetPreferredType(); + } + LogError(diagnostic::UNRESOLVABLE_ARRAY, {}, init->Start()); + return GetGlobalTypesHolder()->GlobalTypeError(); } auto const isNumeric = [](checker::Type *ct) { return ct->HasTypeFlag(TypeFlag::ETS_CONVERTIBLE_TO_NUMERIC); }; auto const isChar = [](checker::Type *ct) { return ct->HasTypeFlag(TypeFlag::CHAR); }; diff --git a/ets2panda/test/ast/compiler/ets/empty_array_expression.ets b/ets2panda/test/ast/compiler/ets/empty_array_expression.ets new file mode 100644 index 0000000000..ea4da1ee58 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/empty_array_expression.ets @@ -0,0 +1,40 @@ +/* + * 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. + */ + +let var1 = /* @@ label1 */[]; +const con1 = /* @@ label2 */[]; + +function foo(){ + let var2 = /* @@ label3 */[]; + const con2 = /* @@ label4 */[]; +} + +class A{ + prop1 = /* @@ label5 */[]; + static prop1 = /* @@ label6 */[]; + foo(){ + let var3 = /* @@ label7 */[]; + const con3 = /* @@ label8 */[]; + } +} + +/* @@@ label1 Error TypeError: Can't resolve array type */ +/* @@@ label2 Error TypeError: Can't resolve array type */ +/* @@@ label3 Error TypeError: Can't resolve array type */ +/* @@@ label4 Error TypeError: Can't resolve array type */ +/* @@@ label5 Error TypeError: Can't resolve array type */ +/* @@@ label6 Error TypeError: Can't resolve array type */ +/* @@@ label7 Error TypeError: Can't resolve array type */ +/* @@@ label8 Error TypeError: Can't resolve array type */ diff --git a/ets2panda/test/ast/compiler/ets/inferTypeOfArrayNegative2.ets b/ets2panda/test/ast/compiler/ets/inferTypeOfArrayNegative2.ets index b0f2d51def..d886e32944 100644 --- a/ets2panda/test/ast/compiler/ets/inferTypeOfArrayNegative2.ets +++ b/ets2panda/test/ast/compiler/ets/inferTypeOfArrayNegative2.ets @@ -18,4 +18,8 @@ a[0] = 1 a[1] = "1" let b = a[0] + a[1] -/* @@? 19:9 Error TypeError: Bad operand type, the types of the operands must be numeric type, enum or String. */ +/* @@? 16:9 Error TypeError: Can't resolve array type */ +/* @@? 17:1 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 18:1 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 19:9 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 19:16 Error TypeError: Indexed access is not supported for such expression type. */ diff --git a/ets2panda/test/compiler/ets/inferTypeOfArray-expected.txt b/ets2panda/test/compiler/ets/inferTypeOfArray-expected.txt index 760183394c..8b40c4774a 100644 --- a/ets2panda/test/compiler/ets/inferTypeOfArray-expected.txt +++ b/ets2panda/test/compiler/ets/inferTypeOfArray-expected.txt @@ -1834,3 +1834,7 @@ } } } +TypeError: Can't resolve array type [inferTypeOfArray.ets:17:9] +TypeError: Can't resolve array type [inferTypeOfArray.ets:21:9] +TypeError: Indexed access is not supported for such expression type. [inferTypeOfArray.ets:23:1] +TypeError: Indexed access is not supported for such expression type. [inferTypeOfArray.ets:24:1] diff --git a/ets2panda/test/parser/ets/literals-expected.txt b/ets2panda/test/parser/ets/literals-expected.txt index 5dffbbbbf6..ac1a72448a 100644 --- a/ets2panda/test/parser/ets/literals-expected.txt +++ b/ets2panda/test/parser/ets/literals-expected.txt @@ -1600,3 +1600,4 @@ } } } +TypeError: Can't resolve array type [literals.ets:35:15] -- Gitee