From 6ce4ce25423da24db10400760caf0a38a7b24345 Mon Sep 17 00:00:00 2001 From: nadolskyanton Date: Thu, 10 Jul 2025 16:03:51 +0300 Subject: [PATCH] Parser: forbid typed ctor Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICLIXS Description: Forbid typed constructor Signed-off-by: nadolskyanton --- ets2panda/parser/parserImpl.cpp | 5 +++ .../ets/constructor_no_type_parameters.ets | 22 +++++++++++++ .../ets/constructor_type_parameters.ets | 20 ++++++++++++ .../test/ast/parser/ets/type_variance2.ets | 1 + ets2panda/util/diagnostic/syntax.yaml | 32 +++++++++++-------- 5 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/constructor_no_type_parameters.ets create mode 100644 ets2panda/test/ast/parser/ets/constructor_type_parameters.ets diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index b9be71ce60..7b7c10dc59 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -964,6 +964,11 @@ FunctionSignature ParserImpl::ParseFunctionSignature(ParserStatus status) { ir::TSTypeParameterDeclaration *typeParamDecl = ParseFunctionTypeParameters(); + // Check if constructor has type parameters + if ((status & ParserStatus::CONSTRUCTOR_FUNCTION) != 0 && typeParamDecl != nullptr) { + LogError(diagnostic::CONSTRUCTOR_TYPE_PARAMETERS); + } + if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS) { auto parameter = (status & ParserStatus::ARROW_FUNCTION) != 0 ? ParseFunctionParameter() : nullptr; if (parameter != nullptr) { diff --git a/ets2panda/test/ast/parser/ets/constructor_no_type_parameters.ets b/ets2panda/test/ast/parser/ets/constructor_no_type_parameters.ets new file mode 100644 index 0000000000..9f5695c289 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/constructor_no_type_parameters.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +class C { + constructor(x: int) {} +} + +class D { + constructor(x: T) {} +} diff --git a/ets2panda/test/ast/parser/ets/constructor_type_parameters.ets b/ets2panda/test/ast/parser/ets/constructor_type_parameters.ets new file mode 100644 index 0000000000..ff1194075b --- /dev/null +++ b/ets2panda/test/ast/parser/ets/constructor_type_parameters.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +class C { + constructor(x: T) {} +} + +/* @@? 17:17 Error SyntaxError: Constructor should not have type parameters. */ diff --git a/ets2panda/test/ast/parser/ets/type_variance2.ets b/ets2panda/test/ast/parser/ets/type_variance2.ets index 67d7a48464..c7b077b0b6 100644 --- a/ets2panda/test/ast/parser/ets/type_variance2.ets +++ b/ets2panda/test/ast/parser/ets/type_variance2.ets @@ -18,3 +18,4 @@ class C { } /* @@@ label Error SyntaxError: Variance modifier is not allowed here. */ +/* @@? 17:35 Error SyntaxError: Constructor should not have type parameters. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index d8a218abd8..cf118ac29c 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -149,6 +149,10 @@ syntax: id: 93489 message: "Constructor function types are not supported." +- name: CONSTRUCTOR_TYPE_PARAMETERS + id: 330 + message: "Constructor should not have type parameters." + - name: DECALRE_IN_AMBIENT_CONTEXT id: 104 message: "A 'declare' modifier cannot be used in an already ambient context." @@ -231,8 +235,8 @@ syntax: - name: ERROR_ARKTS_NO_FOR_IN_LOOP id: 6987 - message: "'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays - and iterable objects." + message: "'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays\ + \ and iterable objects." - name: ERROR_ARKTS_NO_IMPORT_ASSERTIONS id: 313 @@ -260,8 +264,8 @@ syntax: - name: ERROR_ARKTS_NO_SIDE_EFFECT_IMPORT id: 73297 - message: "Importing for side-effect only is prohibited! Please provide objects to be imported explicitly or use * to import - all objects declared in the module!" + message: "Importing for side-effect only is prohibited! Please provide objects to be imported explicitly or use * to import\ + \ all objects declared in the module!" - name: ERROR_ARKTS_NO_UMD id: 114179 @@ -466,8 +470,8 @@ syntax: - name: IMPORT_TOP_LEVEL id: 226 - message: "Import declarations can only be used on the top level and before any other declaration, top level statement or - directive." + message: "Import declarations can only be used on the top level and before any other declaration, top level statement or\ + \ directive." - name: IMPROPER_NESTING_CLASS id: 233 @@ -503,8 +507,8 @@ syntax: - name: INIT_MODULE_DECLARATION_POSITION id: 322 - message: "initModule() must only be called immediately after the import statement, and before any other declarations or - statements." + message: "initModule() must only be called immediately after the import statement, and before any other declarations or\ + \ statements." - name: INSERT_NODE_ABSENT id: 10 @@ -588,8 +592,8 @@ syntax: - name: INVALID_DECORATOR_CONSTRUCTOR id: 201 - message: "The modifier for a constructor should be limited to access modifiers (private, internal, protected, public), and - 'native' modifiers." + message: "The modifier for a constructor should be limited to access modifiers (private, internal, protected, public), and\ + \ 'native' modifiers." - name: INVALID_DESTRUCTURING_TARGET id: 49 @@ -757,8 +761,8 @@ syntax: - name: LITERAL_VALUE_IDENT id: 315 - message: "Number, string or computed value property name '{}' is not allowed, use classes to access data by property names - that are identifiers" + message: "Number, string or computed value property name '{}' is not allowed, use classes to access data by property names\ + \ that are identifiers" - name: LOCAL_CLASS_ACCESS_MOD id: 30 @@ -1243,8 +1247,8 @@ syntax: - name: UNSUPPORTED_ENUM_TYPE id: 327 - message: "Unsupported enum type annotation. Supported enum types are: int, long or double. String is allowed for literal - types, not annotations." + message: "Unsupported enum type annotation. Supported enum types are: int, long or double. String is allowed for literal\ + \ types, not annotations." - name: UNTERMINATED_MULTI_LINE_COMMENT id: 245 -- Gitee