diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index 5f850708be761f4d726f8bc6dc64567d341dc5b2..ac0f51086922cda75a8d898b7edff97c7595f9be 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 0000000000000000000000000000000000000000..9d2fef2044422f716fa5e9d5d606f9ae3f04f37f --- /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 be4c8ab3932e7ca4a9abe8f129da3b8eecbfb2ca..998434c0d987e8d93f02c32defce9aa9cc3544e7 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."