From 6da091a7335c50f3ee17aae9fd3543b9a284ac84 Mon Sep 17 00:00:00 2001 From: Zhelyapov Aleksey Date: Thu, 3 Jul 2025 09:04:49 +0300 Subject: [PATCH] Implemented lambda spread param type inference Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJPDR Signed-off-by: Zhelyapov Aleksey --- ets2panda/checker/ets/function.cpp | 12 ++++++--- .../compiler/lowering/ets/lambdaLowering.cpp | 10 +++++--- .../ast/compiler/ets/restvar_type_infer.ets | 2 ++ .../ets/FixedArray/unexpected_token_31.ets | 1 + .../annotationUsage_bad_param07.ets | 1 + .../ast/parser/ets/unexpected_token_31.ets | 1 + .../ets/lambdaRestParamTypeInference.ets | 25 +++++++++++++++++++ 7 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/runtime/ets/lambdaRestParamTypeInference.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 432e97432f..94cbc1c8e1 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1435,17 +1435,21 @@ SignatureInfo *ETSChecker::ComposeSignatureInfo(ir::TSTypeParameterDeclaration * if (!params.empty()) { if (auto param = params.back()->AsETSParameterExpression(); param->IsRestParameter()) { - if (param->TypeAnnotation() == nullptr) { // #23134 - ES2PANDA_ASSERT(IsAnyError()); + checker::Type *restParamType = nullptr; + if (param->TypeAnnotation() != nullptr) { + restParamType = param->RestParameter()->TypeAnnotation()->GetType(this); + } else if (param->Ident()->TsType() != nullptr) { + restParamType = param->Ident()->TsType(); + } else { + ES2PANDA_ASSERT(IsAnyError()); // #23134 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)); + signatureInfo->restVar = SetupSignatureParameter(param, restParamType); ES2PANDA_ASSERT(signatureInfo->restVar != nullptr); } } diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index c513801d30..8231a17c35 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -190,15 +190,19 @@ ParamsAndVarMap CreateLambdaCalleeParameters(public_lib::Context *ctx, ir::Arrow newParam->SetOptional(false); newParamType = checker->CreateETSUnionType({newParamType, checker->GlobalETSUndefinedType()}); } - newParam->Ident()->SetTsTypeAnnotation(allocator->New(newParamType, allocator)); - newParam->Ident()->TypeAnnotation()->SetParent(newParam->Ident()); - newParam->Ident()->SetVariable(nullptr); // Remove the cloned variable. + newParam->SetTypeAnnotation(allocator->New(newParamType, allocator)); auto *var = varBinder->AddParamDecl(newParam); var->SetTsType(newParamType); var->SetScope(paramScope); newParam->SetVariable(var); newParam->SetTsType(newParamType); newParam->Ident()->SetTsType(newParamType); + if (newParam->IsRestParameter()) { + newParam->TypeAnnotation()->SetParent(newParam->RestParameter()); + newParam->RestParameter()->SetTsType(newParamType); + } else { + newParam->TypeAnnotation()->SetParent(newParam->Ident()); + } resParams.push_back(newParam); varMap[oldParam->AsETSParameterExpression()->Variable()] = var; diff --git a/ets2panda/test/ast/compiler/ets/restvar_type_infer.ets b/ets2panda/test/ast/compiler/ets/restvar_type_infer.ets index 578567a54a..42f7062c07 100644 --- a/ets2panda/test/ast/compiler/ets/restvar_type_infer.ets +++ b/ets2panda/test/ast/compiler/ets/restvar_type_infer.ets @@ -19,4 +19,6 @@ function main() { } /* @@? 17:16 Error TypeError: The type of parameter 'args' cannot be inferred */ +/* @@? 17:16 Error SyntaxError: Rest parameter should be either array or tuple type. */ /* @@? 18:26 Error TypeError: The type of parameter 'args' cannot be inferred */ +/* @@? 18:26 Error SyntaxError: Rest parameter should be either array or tuple type. */ diff --git a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets index e53b749a07..37de6fe998 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_31.ets @@ -18,6 +18,7 @@ function foo(...^number: FixedArray): int { } /* @@? 16:10 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 16:14 Error SyntaxError: Rest parameter should be either array or tuple type. */ /* @@? 16:17 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 16:18 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 16:18 Error SyntaxError: Rest parameter must be the last formal parameter. */ diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets index 93b781dc7b..439897e7a6 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets @@ -24,3 +24,4 @@ function foo(@MyAnno ...a/* @@ label */){} /* @@@ label Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 22:15 Error TypeError: The required field 'testProperty1' must be specified. Fields without default values cannot be omitted. */ /* @@? 22:22 Error TypeError: The type of parameter 'a' cannot be inferred */ +/* @@? 22:22 Error SyntaxError: Rest parameter should be either array or tuple type. */ diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_31.ets b/ets2panda/test/ast/parser/ets/unexpected_token_31.ets index 13151518a2..01374410c3 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_31.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_31.ets @@ -18,6 +18,7 @@ function foo(...^number: int[]): int { } /* @@? 16:10 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 16:14 Error SyntaxError: Rest parameter should be either array or tuple type. */ /* @@? 16:17 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 16:18 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 16:18 Error SyntaxError: Rest parameter must be the last formal parameter. */ diff --git a/ets2panda/test/runtime/ets/lambdaRestParamTypeInference.ets b/ets2panda/test/runtime/ets/lambdaRestParamTypeInference.ets new file mode 100644 index 0000000000..5d9adf75e2 --- /dev/null +++ b/ets2panda/test/runtime/ets/lambdaRestParamTypeInference.ets @@ -0,0 +1,25 @@ +/* + * 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. + */ + +const sum1: ((...numbers: number[]) => number) = ((...numbers) => { + return numbers.reduce((a, b) => a + b, 0) +}) + +const sum2: ((n: int, ...numbers: number[]) => number) = ((n, ...numbers) => { + return numbers.reduce((a, b) => a + b, 0) +}) + +arktest.assertEQ(sum1(1,2,3,4), 10) +arktest.assertEQ(sum2(1,2,3,4), 9) -- Gitee