From 4313fbabfae0858a2ba923bf753e87feea854ce3 Mon Sep 17 00:00:00 2001 From: ekkoruse Date: Sat, 5 Jul 2025 17:13:10 +0800 Subject: [PATCH] log syntaxerror for default value in declare Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICK7TT?from=project-issue Signed-off-by: ekkoruse --- .../ets/defaultParametersLowering.cpp | 18 +++++++++++++++- .../compiler/ets/default_param_declare.ets | 21 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 4 ++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/default_param_declare.ets diff --git a/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersLowering.cpp index d87fc777fd..b525f3c4f8 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 0000000000..491faa15e8 --- /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 465347e084..2dd18d400b 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 -- Gitee