From b43c2ffd433ce8894441979eae039847ce7f4b6b Mon Sep 17 00:00:00 2001 From: fcc Date: Thu, 24 Jul 2025 10:39:17 +0800 Subject: [PATCH] fix type checking empty ArrayExpression Type check ArrayExpression when its preferred type is tuple. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOG1F Signed-off-by: fcc --- ets2panda/checker/ETSAnalyzer.cpp | 5 ++++ .../ast/compiler/ets/tuple_types_13_neg.ets | 23 +++++++++++++++++++ .../Readonly-with-ArrayType-test2.ets | 2 +- .../readonly-parameter-test5.ets | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/tuple_types_13_neg.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 74631ae976..bdc374e028 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -985,6 +985,11 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const return checker->TypeError(expr, diagnostic::UNRESOLVABLE_ARRAY, expr->Start()); } + if (preferredType->IsETSTupleType()) { + if (!checker->IsArrayExprSizeValidForTuple(expr, preferredType->AsETSTupleType())) { + return checker->InvalidateType(expr); + } + } expr->SetTsType(preferredType); if (!preferredType->IsETSResizableArrayType() && !preferredType->IsETSTupleType()) { ES2PANDA_ASSERT(preferredType->IsETSArrayType()); diff --git a/ets2panda/test/ast/compiler/ets/tuple_types_13_neg.ets b/ets2panda/test/ast/compiler/ets/tuple_types_13_neg.ets new file mode 100644 index 0000000000..ad1ae6a543 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/tuple_types_13_neg.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +function getFloatTuple(): [float, float, float, float, float] { + return []; +} + +let x: [float, float, float, float, float] = [] + +/* @@? 17:12 Error TypeError: Initializer has 0 elements, but tuple requires 5 */ +/* @@? 20:46 Error TypeError: Initializer has 0 elements, but tuple requires 5 */ 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..a5d161c013 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,7 +16,7 @@ function foo (p: [int, string]) { } -let x: Readonly<[int, string]> = [] +let x: Readonly<[int, string]> = [1, 'a'] /* @@ label */foo(/* @@ label1 */x) /* @@@ label1 Error TypeError: Type 'readonly [Int, String]' is not compatible with type '[Int, String]' at index 1 */ 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..b8f77f76e3 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,7 +16,7 @@ function foo (p: [int, string]) { } -let x: readonly [int, string] = [] +let x: readonly [int, string] = [1, 'a'] /* @@ label */foo(/* @@ label1 */x) /* @@@ label Error TypeError: No matching call signature for foo(readonly [Int, String]) */ -- Gitee