diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index b215a47e80c574e68c599f9d7163f878bb44299e..cce74b9c04be563459ceb589f07999e923ee90d4 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -1194,7 +1194,8 @@ static ir::ClassDeclaration *CreateLambdaClass(public_lib::Context *ctx, checker return classDeclaration; } -static ir::ETSNewClassInstanceExpression *CreateConstructorCall(public_lib::Context *ctx, ir::AstNode *lambdaOrFuncRef, +static ir::ETSNewClassInstanceExpression *CreateConstructorCall(public_lib::Context *ctx, + ir::TypedAstNode *lambdaOrFuncRef, ir::ClassDeclaration *lambdaClass, LambdaInfo const *info) { @@ -1235,6 +1236,9 @@ static ir::ETSNewClassInstanceExpression *CreateConstructorCall(public_lib::Cont objectType->AsETSObjectType()); auto scopeCtx = checker::ScopeContext(ctx->GetChecker(), nearestScope); newExpr->Check(checker); + // NOTE: We need to set back the TsType, which is ETSFunctionType, to ensure to insert correct invoke calls. + ES2PANDA_ASSERT(lambdaOrFuncRef->TsType()->IsETSFunctionType()); + newExpr->SetTsType(lambdaOrFuncRef->TsType()); return newExpr; } @@ -1474,9 +1478,7 @@ static ir::AstNode *InsertInvokeCall(public_lib::Context *ctx, ir::CallExpressio auto *oldType = checker->GetApparentType(oldCallee->TsType()); ES2PANDA_ASSERT(oldType != nullptr); size_t arity = call->Arguments().size(); - auto *ifaceType = oldType->IsETSObjectType() - ? oldType->AsETSObjectType() - : oldType->AsETSFunctionType()->ArrowToFunctionalInterfaceDesiredArity(checker, arity); + auto *ifaceType = oldType->AsETSFunctionType()->ArrowToFunctionalInterfaceDesiredArity(checker, arity); ES2PANDA_ASSERT(ifaceType != nullptr); bool hasRestParam = (oldType->IsETSFunctionType() && oldType->AsETSFunctionType()->ArrowSignature()->HasRestParameter()) || @@ -1499,9 +1501,7 @@ static ir::AstNode *InsertInvokeCall(public_lib::Context *ctx, ir::CallExpressio newCallee->SetObjectType(ifaceType); /* Pull out substituted call signature */ - auto *funcIface = - ifaceType->HasObjectFlag(checker::ETSObjectFlags::INTERFACE) ? ifaceType : ifaceType->Interfaces()[0]; - checker::Signature *callSig = funcIface->GetFunctionalInterfaceInvokeType()->CallSignatures()[0]; + checker::Signature *callSig = ifaceType->GetFunctionalInterfaceInvokeType()->CallSignatures()[0]; ES2PANDA_ASSERT(callSig != nullptr); call->SetCallee(newCallee); diff --git a/ets2panda/test/runtime/ets/lambda_default.ets b/ets2panda/test/runtime/ets/lambda_default.ets new file mode 100644 index 0000000000000000000000000000000000000000..6bc41bd941a491522062dcf393954a33a0a54c77 --- /dev/null +++ b/ets2panda/test/runtime/ets/lambda_default.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 main() { + arktest.assertEQ((param?:string) => { return param }("Hihi"), "Hihi") + arktest.assertEQ((param?:string) => { return param }(), undefined) +}