From cde8f691d7015f7b288cb17aac5554321210e2a0 Mon Sep 17 00:00:00 2001 From: zmw Date: Tue, 12 Aug 2025 17:28:23 +0800 Subject: [PATCH] Fix lambda less param infer fail Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSLYD Description: Fix lambda less param infer fail Signed-off-by: zmw --- ets2panda/checker/ets/helpers.cpp | 4 ++-- .../test/runtime/ets/lambdaParamsInfer4.ets | 19 ++++++++++++++++++ .../test/runtime/ets/lambdaParamsInfer5.ets | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/lambdaParamsInfer4.ets create mode 100644 ets2panda/test/runtime/ets/lambdaParamsInfer5.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index d6db109207..c51f8fad4d 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2584,14 +2584,14 @@ bool ETSChecker::CheckLambdaAssignableUnion(ir::AstNode *typeAnn, ir::ScriptFunc bool assignable = false; for (auto *type : typeAnn->AsETSUnionType()->Types()) { if (type->IsETSFunctionType()) { - assignable |= lambda->Params().size() == type->AsETSFunctionType()->Params().size(); + assignable |= lambda->Params().size() <= type->AsETSFunctionType()->Params().size(); continue; } if (type->IsETSTypeReference()) { auto aliasType = util::Helpers::DerefETSTypeReference(type); assignable |= aliasType->IsETSFunctionType() && - lambda->Params().size() == aliasType->AsETSFunctionType()->Params().size(); + lambda->Params().size() <= aliasType->AsETSFunctionType()->Params().size(); } } diff --git a/ets2panda/test/runtime/ets/lambdaParamsInfer4.ets b/ets2panda/test/runtime/ets/lambdaParamsInfer4.ets new file mode 100644 index 0000000000..1e12277b35 --- /dev/null +++ b/ets2panda/test/runtime/ets/lambdaParamsInfer4.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. + */ + +function foo(fun: ((a: number) => Int) | undefined) { + return 1; +} +arktest.assertEQ(foo((() => { return 1 })), 1); diff --git a/ets2panda/test/runtime/ets/lambdaParamsInfer5.ets b/ets2panda/test/runtime/ets/lambdaParamsInfer5.ets new file mode 100644 index 0000000000..b5dc3e873a --- /dev/null +++ b/ets2panda/test/runtime/ets/lambdaParamsInfer5.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +type callback = (a: number) => Int; +function foo(fun: callback | undefined) { + return 1; +} +arktest.assertEQ(foo((() => { return 1 })), 1); -- Gitee