diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index a9b83658ec1a1784c81a34dac4632baec21d12a2..6c6997b85ca6338a41de854d49079df393ecad6e 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 0000000000000000000000000000000000000000..ea4da1ee58457a7dcc19febe4c36a1db766ecc03 --- /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 b0f2d51def4648f80f50a76557359097f32c0c7a..d886e32944bab02a919a0cb54c1b1d8a5d69a69d 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 760183394c72cfc72f60a15adf4ce811237c4708..8b40c4774a533a1177fe5c5d8bc8ded79b7f3738 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 5dffbbbbf62629a9dc45140ad69982e1902a4991..ac1a72448a1eafc7354becb8775db21d4e432f99 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]