diff --git a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp index e6bffbeee360a7424ff93c0a5bad1660de6ddeed..5dec64b8fee4b190eaf6c16d665d4f11695fa9f9 100644 --- a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp @@ -42,9 +42,12 @@ static void TransformDefaultParameters(public_lib::Context *ctx, ir::ScriptFunct const std::vector ¶ms, bool isInterfaceFunction) { + auto const allocator = ctx->allocator; + auto const parser = ctx->parser->AsETSParser(); + if (isInterfaceFunction) { for (const auto param : params) { - TransformInitializer(ctx->allocator, ctx->parser->AsETSParser(), param); + TransformInitializer(allocator, parser, param); } return; } @@ -55,8 +58,7 @@ static void TransformDefaultParameters(public_lib::Context *ctx, ir::ScriptFunct } auto const body = function->Body()->AsBlockStatement(); - auto const allocator = ctx->allocator; - auto const parser = ctx->parser->AsETSParser(); + auto &bodyStmt = body->StatementsForUpdates(); bodyStmt.insert(bodyStmt.begin(), params.size(), nullptr); @@ -69,7 +71,7 @@ static void TransformDefaultParameters(public_lib::Context *ctx, ir::ScriptFunct } } -static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *function) +static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *function, bool isArrow) { auto const ¶ms = function->Params(); std::vector defaultParams; @@ -79,14 +81,21 @@ static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *func ES2PANDA_ASSERT(ctx->diagnosticEngine->IsAnyError()); continue; } - if (param->AsETSParameterExpression()->Initializer() == nullptr) { + auto etsParam = param->AsETSParameterExpression(); + if (etsParam->Initializer() == nullptr) { continue; } - if (param->AsETSParameterExpression()->TypeAnnotation() == nullptr) { // #23134 - ES2PANDA_ASSERT(ctx->diagnosticEngine->IsAnyError()); - continue; + if ((etsParam->TypeAnnotation() == nullptr)) { // #23134 + if (isArrow) { + etsParam->SetTypeAnnotation(ctx->allocator->New( + ctx->GetChecker()->AsETSChecker()->GlobalETSAnyType(), ctx->allocator)); + } else { + ES2PANDA_ASSERT(ctx->diagnosticEngine->IsAnyError()); + continue; + } } - defaultParams.push_back(param->AsETSParameterExpression()); + + defaultParams.push_back(etsParam); } if (defaultParams.empty()) { @@ -114,7 +123,7 @@ bool DefaultParametersLowering::PerformForModule(public_lib::Context *ctx, parse // CC-OFFNXT(G.FMT.14-CPP) project code style [ctx](ir::AstNode *const node) -> ir::AstNode * { if (node->IsScriptFunction()) { - TransformFunction(ctx, node->AsScriptFunction()); + TransformFunction(ctx, node->AsScriptFunction(), node->Parent()->IsArrowFunctionExpression()); } return node; }, diff --git a/ets2panda/test/runtime/ets/lambda_with_default_implicit.ets b/ets2panda/test/runtime/ets/lambda_with_default_implicit.ets new file mode 100644 index 0000000000000000000000000000000000000000..5c112409241542165c15cf3c659104c7720ebacf --- /dev/null +++ b/ets2panda/test/runtime/ets/lambda_with_default_implicit.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() { + let b5 = (m = "string") => { return "hello"; }; + arktest.assertTrue(b5() == "hello") +}