diff --git a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp index d87fc777fd5283328aa9c22b3f21b944ac3cc7c2..b525f3c4f84375b4ab1a608f7b6265805aa8961b 100644 --- a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp @@ -16,6 +16,8 @@ #include "defaultParametersLowering.h" #include "compiler/lowering/util.h" +#include + namespace ark::es2panda::compiler { static ir::Statement *TransformInitializer(ArenaAllocator *allocator, parser::ETSParser *parser, @@ -38,6 +40,20 @@ static ir::Statement *TransformInitializer(ArenaAllocator *allocator, parser::ET param->Ident()->Name(), init, typeAnnotation->Clone(allocator, nullptr)); } +static void ValidateDefaultParamInDeclare(public_lib::Context *ctx, ir::ScriptFunction *function, + std::vector ¶ms) +{ + for (auto param : params) { + if (param->Initializer() == nullptr) { + continue; + } + param->SetInitializer(nullptr); + if ((function->Flags() & ir::ScriptFunctionFlags::EXTERNAL) != 0U) { + ctx->checker->AsETSChecker()->LogError(diagnostic::DEFAULT_PARAM_IN_DECLARE, param->Start()); + } + } +} + static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *function) { auto const ¶ms = function->Params(); @@ -62,7 +78,7 @@ static void TransformFunction(public_lib::Context *ctx, ir::ScriptFunction *func return; } if (!function->HasBody()) { // #23134 - ES2PANDA_ASSERT(ctx->diagnosticEngine->IsAnyError()); + ValidateDefaultParamInDeclare(ctx, function, defaultParams); return; } auto const body = function->Body()->AsBlockStatement(); diff --git a/ets2panda/test/ast/compiler/ets/default_param_declare.ets b/ets2panda/test/ast/compiler/ets/default_param_declare.ets new file mode 100644 index 0000000000000000000000000000000000000000..491faa15e8776f434ca3093296d3895001dfa6aa --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/default_param_declare.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +declare class A{ + foo(/* @@ label1 */a:number = 1, /* @@ label2 */b:int = 2):void +} + +/* @@@ label1 Error SyntaxError: A parameter initializer is only allowed in a function or constructor implementation. */ +/* @@@ label2 Error SyntaxError: A parameter initializer is only allowed in a function or constructor implementation. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 465347e084fe629eda60dd36511d7b93fbb3b906..2dd18d400b7f2574ed0cf18f6d6cdd353e9bc545 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1195,3 +1195,7 @@ syntax: - name: DEEP_NESTING id: 306 message: "Maximum allowed nesting level exceeded." + +- name: DEFAULT_PARAM_IN_DECLARE + id: 382 + message: "A parameter initializer is only allowed in a function or constructor implementation." \ No newline at end of file