diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index bc4ac968a0b6d7ab69d3d169fc245beb543726b4..eb75655aecc1ea2fd2697e89a897e104f82df241 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 0000000000000000000000000000000000000000..51410084c42e10e3f527c27115dd52cda84e1b66 --- /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 0000000000000000000000000000000000000000..531f0bf754affd43e30121693daa7d27246d0540 --- /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 0000000000000000000000000000000000000000..1ce766edbe2b407fab7525b27d2cb6b2b9484d86 --- /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; +}