From db0c1849f5efdffb3687ff7a7fdf112c4d35b845 Mon Sep 17 00:00:00 2001 From: Boglarka Haag Date: Thu, 7 Mar 2024 12:47:05 +0100 Subject: [PATCH] [es2panda] Anonymous lambda call segfault Anonymous lambda call fixed. The return type can be predicted correctly. Fixes #15935 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I96QS0 Signed-off-by: Haag Boglarka --- ets2panda/checker/ETSAnalyzerHelpers.cpp | 11 +++++++++- .../runtime/ets/anonymous_lambda_call.ets | 18 +++++++++++++++++ .../runtime/ets/anonymous_lambda_call_2.ets | 19 ++++++++++++++++++ .../runtime/ets/anonymous_lambda_call_3.ets | 20 +++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/anonymous_lambda_call.ets create mode 100644 ets2panda/test/runtime/ets/anonymous_lambda_call_2.ets create mode 100644 ets2panda/test/runtime/ets/anonymous_lambda_call_3.ets diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index bc4ac968a0..eb75655aec 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -189,18 +189,27 @@ checker::Type *InitAnonymousLambdaCallee(checker::ETSChecker *checker, ir::Expre ArenaVector params {checker->Allocator()->Adapter()}; checker->CopyParams(arrowFunc->Params(), params); + checker::Type *funcReturnType = nullptr; auto *typeAnnotation = arrowFunc->ReturnTypeAnnotation(); if (typeAnnotation != nullptr) { typeAnnotation = typeAnnotation->Clone(checker->Allocator(), nullptr); typeAnnotation->SetTsType(arrowFunc->ReturnTypeAnnotation()->TsType()); + } else { + if ((arrowFunc->Flags() & ir::ScriptFunctionFlags::HAS_RETURN) != 0) { + InferReturnType(checker, arrowFunc, funcReturnType, callee); + } else if (arrowFunc->Signature()->ReturnType() != nullptr) { + auto newTypeAnnotation = callee->AsArrowFunctionExpression()->CreateTypeAnnotation(checker); + typeAnnotation = arrowFunc->ReturnTypeAnnotation(); + funcReturnType = newTypeAnnotation->GetType(checker); + } } auto signature = ir::FunctionSignature(nullptr, std::move(params), typeAnnotation); auto *funcType = checker->AllocNode(std::move(signature), ir::ScriptFunctionFlags::NONE); funcType->SetScope(arrowFunc->Scope()->AsFunctionScope()->ParamScope()); - auto *const funcIface = funcType->Check(checker); + auto *const funcIface = typeAnnotation != nullptr ? funcType->Check(checker) : funcReturnType; checker->Relation()->SetNode(callee); checker->Relation()->IsAssignableTo(calleeType, funcIface); return funcIface; diff --git a/ets2panda/test/runtime/ets/anonymous_lambda_call.ets b/ets2panda/test/runtime/ets/anonymous_lambda_call.ets new file mode 100644 index 0000000000..51410084c4 --- /dev/null +++ b/ets2panda/test/runtime/ets/anonymous_lambda_call.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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 main() { + assert (() => 3)() == 3 +} diff --git a/ets2panda/test/runtime/ets/anonymous_lambda_call_2.ets b/ets2panda/test/runtime/ets/anonymous_lambda_call_2.ets new file mode 100644 index 0000000000..531f0bf754 --- /dev/null +++ b/ets2panda/test/runtime/ets/anonymous_lambda_call_2.ets @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024 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 main() { + let ret: number = ((x: number) => x)(5) + assert ret == 5 +} diff --git a/ets2panda/test/runtime/ets/anonymous_lambda_call_3.ets b/ets2panda/test/runtime/ets/anonymous_lambda_call_3.ets new file mode 100644 index 0000000000..1ce766edbe --- /dev/null +++ b/ets2panda/test/runtime/ets/anonymous_lambda_call_3.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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 main() { + let res : boolean = false; + (() => { res = true; })() + assert res; +} -- Gitee