From 87ec4d04c8f5f170fd17cbc53b760f8961c43e52 Mon Sep 17 00:00:00 2001 From: MuSilk Date: Wed, 16 Jul 2025 15:00:23 +0800 Subject: [PATCH] Fix empty tuple assignment bug Issue: [Bug]: ES2panda crash with empty tuple https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMP5F Signed-off-by: MuSilk --- ets2panda/checker/ETSAnalyzer.cpp | 10 +++++----- .../compiler/ets/empty_tuple_assignmemt.ets | 19 +++++++++++++++++++ .../Readonly-with-ArrayType-test2.ets | 3 ++- .../readonly-parameter-test5.ets | 3 ++- 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/empty_tuple_assignmemt.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 7c2edff7e0..0ef99faa78 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 0000000000..8016318281 --- /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 7656872082..e6e6e1759a 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 c070f2aba9..1434abd641 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 */ -- Gitee