diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 095b9f0503e36efd4eabfe0252c84ded480c63d2..b1281f49951fb554c91230b7b9bb9ae12b0e20fd 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -717,6 +717,12 @@ bool ETSChecker::CheckInit(ir::Identifier *ident, ir::TypeNode *typeAnnotation, { if (typeAnnotation == nullptr) { if (init->IsArrayExpression()) { + auto arrayExpr = init->AsArrayExpression(); + if (arrayExpr->Elements().empty()) { + LogError(diagnostic::CANNOT_INFER_OBJ_LIT, {ident->Name()}, ident->Start()); + return false; + } + annotationType = CheckArrayElements(init->AsArrayExpression()); } else if (init->IsETSNewArrayInstanceExpression()) { annotationType = init->AsETSNewArrayInstanceExpression()->TypeReference()->GetType(this); diff --git a/ets2panda/test/ast/compiler/ets/expressions_test.ets b/ets2panda/test/ast/compiler/ets/expressions_test.ets new file mode 100755 index 0000000000000000000000000000000000000000..1eef3ff0e6e906993aa5d825cf0392b2f1cdca30 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/expressions_test.ets @@ -0,0 +1,20 @@ +/* + * 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 u : "A" | "B" = "A"; + +let /* @@ label */a = []; + +/* @@@ label Error TypeError: Cannot infer type for a because class composite needs an explicit target type */ \ No newline at end of file