From ca9c39b545d0296064ee5de30e086f4ebc8fa42a Mon Sep 17 00:00:00 2001 From: panpan <2873081892@qq.com> Date: Fri, 25 Jul 2025 10:25:41 +0800 Subject: [PATCH] Fix the issue of recognize rest parameter type Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOQNR Signed-off-by: panpan --- ets2panda/checker/ets/function.cpp | 16 +++++++++- .../ast/compiler/ets/restparam_crash_test.ets | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/restparam_crash_test.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index ff31328770..9da8a5e207 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -569,7 +569,21 @@ bool ETSChecker::IsValidRestArgument(ir::Expression *const argument, Signature * { auto *restParamType = substitutedSig->RestVar()->TsType(); if (restParamType->IsETSTupleType()) { - return false; + auto *tupleType = restParamType->AsETSTupleType(); + const size_t tupleSize = tupleType->GetTupleSize(); + size_t totalRestArgs = 0; + + if (argument->Parent()->IsCallExpression()) { + totalRestArgs = argument->Parent()->AsCallExpression()->Arguments().size(); + } else if (argument->Parent()->IsETSNewClassInstanceExpression()) { + totalRestArgs = argument->Parent()->AsETSNewClassInstanceExpression()->GetArguments().size(); + } else { + return false; + } + + return !( + totalRestArgs != tupleSize || + (index < tupleSize && argument->Check(this)->TypeFlags() != tupleType->GetTypeAtIndex(index)->TypeFlags())); } if (argument->IsObjectExpression()) { argument->SetPreferredType(GetElementTypeOfArray(restParamType)); diff --git a/ets2panda/test/ast/compiler/ets/restparam_crash_test.ets b/ets2panda/test/ast/compiler/ets/restparam_crash_test.ets new file mode 100644 index 0000000000..d5413b9dad --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/restparam_crash_test.ets @@ -0,0 +1,31 @@ +/* + * 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 processItems(...items: [{ key: string }]): void { + for (const item of items) { + console.log(`Item: ${item.key}`); + } +} + +processItems( + { key: "value1" }, + { key: "value2" }, + { key: "value3" } +); + +/* @@? 16:23 Error SyntaxError: Rest parameter should be either array or tuple type. */ +/* @@? 16:34 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ +/* @@? 17:22 Error TypeError: 'For-of' statement source expression is not of iterable type. */ +/* @@? 22:1 Error TypeError: This expression is not callable. */ -- Gitee