From 1cf721fe2a9fad8d1420205bb7b4088237c822fd Mon Sep 17 00:00:00 2001 From: Tamas Toth Date: Thu, 19 Jun 2025 13:19:49 +0200 Subject: [PATCH] Fix lambda type inference Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICGG85 Fixes #26798 internal issue Change-Id: Ic702ad7fa27a7365a842cad05ef44a7264adb6b0 Signed-off-by: Tamas Toth --- ets2panda/checker/ets/function.cpp | 30 +++++++++++-------- .../ets/rest_param_lambda_type_interface.ets | 18 +++++++++++ ets2panda/util/diagnostic/semantic.yaml | 8 +++-- 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/rest_param_lambda_type_interface.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index d2423c24ca..10b3334d26 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1611,21 +1611,27 @@ SignatureInfo *ETSChecker::ComposeSignatureInfo(ir::TSTypeParameterDeclaration * } } - if (!params.empty()) { - if (auto param = params.back()->AsETSParameterExpression(); param->IsRestParameter()) { - if (param->TypeAnnotation() == nullptr) { // #23134 - ES2PANDA_ASSERT(IsAnyError()); - return nullptr; - } - auto restParamType = param->RestParameter()->TypeAnnotation()->GetType(this); - if (!restParamType->IsETSTupleType() && !restParamType->IsETSArrayType() && - !restParamType->IsETSResizableArrayType()) { - LogError(diagnostic::ONLY_ARRAY_OR_TUPLE_FOR_REST, {}, param->Start()); + if (params.empty()) { + return signatureInfo; + } + + if (auto param = params.back()->AsETSParameterExpression(); param->IsRestParameter()) { + if (param->TypeAnnotation() == nullptr) { // #23134 + if (param->RestParameter() != nullptr) { + LogError(diagnostic::REST_PARAM_MUST_HAVE_TYPE_ANNOTATION, {param->Name()}, param->Start()); return nullptr; } - signatureInfo->restVar = SetupSignatureParameter(param, param->TypeAnnotation()->GetType(this)); - ES2PANDA_ASSERT(signatureInfo->restVar != nullptr); + ES2PANDA_ASSERT(IsAnyError()); + return nullptr; + } + auto restParamType = param->RestParameter()->TypeAnnotation()->GetType(this); + if (!restParamType->IsETSTupleType() && !restParamType->IsETSArrayType() && + !restParamType->IsETSResizableArrayType()) { + LogError(diagnostic::ONLY_ARRAY_OR_TUPLE_FOR_REST, {}, param->Start()); + return nullptr; } + signatureInfo->restVar = SetupSignatureParameter(param, param->TypeAnnotation()->GetType(this)); + ES2PANDA_ASSERT(signatureInfo->restVar != nullptr); } return signatureInfo; diff --git a/ets2panda/test/ast/parser/ets/rest_param_lambda_type_interface.ets b/ets2panda/test/ast/parser/ets/rest_param_lambda_type_interface.ets new file mode 100644 index 0000000000..11c6c291b3 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/rest_param_lambda_type_interface.ets @@ -0,0 +1,18 @@ +/* +* 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 low 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. +*/ + +(...arg) => 102; + +/* @@? 16:2 Error TypeError: Rest parameter 'arg' must have an explicit type annotation. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index db1355f996..e4464ee0d4 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1557,5 +1557,9 @@ semantic: message: "overload declaration cannot contain abstract methods." - name: INVALID_RECORD_PROPERTY - id: 383 - message: "Invalid record property" \ No newline at end of file + id: 393 + message: "Invalid record property" + +- name: REST_PARAM_MUST_HAVE_TYPE_ANNOTATION + id: 394 + message: "Rest parameter '{}' must have an explicit type annotation." -- Gitee