From 97c7450379fe44e79b2e33da27fd27596db09933 Mon Sep 17 00:00:00 2001 From: Istvan Romai Date: Fri, 13 Jun 2025 14:00:38 +0200 Subject: [PATCH] Compiler diagnostic improvement arkts-no-ctor-signatures-iface Modified the parser to give error message when using 'new' keyword in interface body Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICFK7P Internal issue: #23519 Change-Id: I5a9fb1bf1d609afe2f9a1c762e38f773803bc788 Signed-off-by: Istvan Romai --- ets2panda/parser/ETSparserClasses.cpp | 11 +++--- .../ets/interface-constructor-signatures.ets | 36 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 4 +++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/interface-constructor-signatures.ets diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index 5f850708be..ac0f510869 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -1016,14 +1016,17 @@ ir::ModifierFlags ETSParser::ParseInterfaceMethodModifiers() LogError(diagnostic::LOCAL_CLASS_ACCESS_MOD, {}, Lexer()->GetToken().Start()); } } - const auto keywordType = Lexer()->GetToken().KeywordType(); const bool isPrivate = (keywordType == lexer::TokenType::KEYW_PRIVATE); const bool isDefaultInAmbient = (keywordType == lexer::TokenType::KEYW_DEFAULT) && InAmbientContext(); - if (!isPrivate && !isDefaultInAmbient) { - LogError(diagnostic::UNEXPECTED_TOKEN_PRIVATE_ID); + if (!isPrivate) { + if (!isDefaultInAmbient) { + LogError(diagnostic::UNEXPECTED_TOKEN_PRIVATE_ID); + } + if (keywordType == lexer::TokenType::KEYW_NEW) { + LogError(diagnostic::ERROR_ARKTS_NO_INTERFACE_CONSTRUCTOR_SIGNATURES); + } } - Lexer()->NextToken(); return isDefaultInAmbient ? ir::ModifierFlags::DEFAULT : ir::ModifierFlags::PRIVATE; } diff --git a/ets2panda/test/ast/compiler/ets/interface-constructor-signatures.ets b/ets2panda/test/ast/compiler/ets/interface-constructor-signatures.ets new file mode 100644 index 0000000000..9d2fef2044 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/interface-constructor-signatures.ets @@ -0,0 +1,36 @@ +/* + * 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. + */ + +//arkts-no-ctor-signatures-iface + +interface I { + new (s: string): I +} + +function fn(i: I) { + return new i("hello") +} + +/* @@? 19:5 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 19:5 Error SyntaxError: Constructor signatures are not supported in interfaces, use methods instead! */ +/* @@? 19:10 Error TypeError: Cannot find type 's'. */ +/* @@? 19:11 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 19:11 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 19:19 Error SyntaxError: Interface fields must have type annotation. */ +/* @@? 19:20 Error SyntaxError: Invalid Type. */ +/* @@? 19:20 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 19:20 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 20:1 Error SyntaxError: Invalid Type. */ +/* @@? 23:16 Error TypeError: Cannot find type 'i'. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index be4c8ab393..998434c0d9 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -222,6 +222,10 @@ syntax: id: 313 message: "Import assertion is not supported, please use the ordinary import syntax instead!" +- name: ERROR_ARKTS_NO_INTERFACE_CONSTRUCTOR_SIGNATURES + id: 81515 + message: "Constructor signatures are not supported in interfaces, use methods instead!" + - name: ERROR_ARKTS_NO_PRIVATE_IDENTIFIERS id: 298 message: "Use 'private' keyword to declare an identifier as private." -- Gitee