diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 7c2edff7e0be2f127766b8700cc49d3a2b66606b..0ef99faa78adc6609efef8e6f0d734ca29d3a6a4 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -982,17 +982,17 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const } expr->SetPreferredType(preferredType); - - if (!ValidArrayExprSizeForTupleSize(checker, preferredType, expr) || - !CheckArrayExpressionElements(checker, expr)) { - return checker->InvalidateType(expr); - } } if (preferredType == nullptr) { return checker->TypeError(expr, diagnostic::UNRESOLVABLE_ARRAY, expr->Start()); } + if (!ValidArrayExprSizeForTupleSize(checker, preferredType, expr) || + (!expr->Elements().empty() && !CheckArrayExpressionElements(checker, expr))) { + return checker->InvalidateType(expr); + } + expr->SetTsType(preferredType); if (!preferredType->IsETSResizableArrayType() && !preferredType->IsETSTupleType()) { ES2PANDA_ASSERT(preferredType->IsETSArrayType()); diff --git a/ets2panda/test/ast/compiler/ets/empty_tuple_assignmemt.ets b/ets2panda/test/ast/compiler/ets/empty_tuple_assignmemt.ets new file mode 100644 index 0000000000000000000000000000000000000000..80163182816856e82a7c3e58a536d9d9e106ee05 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/empty_tuple_assignmemt.ets @@ -0,0 +1,19 @@ +/* + * 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 tuple: [string,number] = /* @@ label */[] + +/* @@@ label Error TypeError: Initializer has 0 elements, but tuple requires 2 */ diff --git a/ets2panda/test/ast/parser/ets/readonly-parameter-test/Readonly-with-ArrayType-test2.ets b/ets2panda/test/ast/parser/ets/readonly-parameter-test/Readonly-with-ArrayType-test2.ets index 7656872082259533c237005afc004ffbbcc3a816..e6e6e1759a06268cdcf5e6961f14950d7523c259 100644 --- a/ets2panda/test/ast/parser/ets/readonly-parameter-test/Readonly-with-ArrayType-test2.ets +++ b/ets2panda/test/ast/parser/ets/readonly-parameter-test/Readonly-with-ArrayType-test2.ets @@ -16,8 +16,9 @@ function foo (p: [int, string]) { } -let x: Readonly<[int, string]> = [] +let x: Readonly<[int, string]> = /* @@ label2 */[] /* @@ label */foo(/* @@ label1 */x) /* @@@ label1 Error TypeError: Type 'readonly [Int, String]' is not compatible with type '[Int, String]' at index 1 */ /* @@@ label Error TypeError: No matching call signature for foo(readonly [Int, String]) */ +/* @@@ label2 Error TypeError: Initializer has 0 elements, but tuple requires 2 */ diff --git a/ets2panda/test/ast/parser/ets/readonly-parameter-test/readonly-parameter-test5.ets b/ets2panda/test/ast/parser/ets/readonly-parameter-test/readonly-parameter-test5.ets index c070f2aba93e0138a6a8cce31b73688f519f8c67..1434abd64149fe606b64f3a8b78fe5129bd571d2 100644 --- a/ets2panda/test/ast/parser/ets/readonly-parameter-test/readonly-parameter-test5.ets +++ b/ets2panda/test/ast/parser/ets/readonly-parameter-test/readonly-parameter-test5.ets @@ -16,8 +16,9 @@ function foo (p: [int, string]) { } -let x: readonly [int, string] = [] +let x: readonly [int, string] = /* @@ label2 */[] /* @@ label */foo(/* @@ label1 */x) /* @@@ label Error TypeError: No matching call signature for foo(readonly [Int, String]) */ /* @@@ label1 Error TypeError: Type 'readonly [Int, String]' is not compatible with type '[Int, String]' at index 1 */ +/* @@@ label2 Error TypeError: Initializer has 0 elements, but tuple requires 2 */