diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 409e4ef4f6243cd88770cfcd40abca7e363f13dd..304958e47c1f324bffeff6bd7cd0df9c1b49e84c 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 0000000000000000000000000000000000000000..9f5695c28969865c29577d9c4d346fd685bc60ab --- /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 0000000000000000000000000000000000000000..ff1194075b9aad7afca50984b7a91e611b1e07b0 --- /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 67d7a48464199ebb03ddf3328a46bac9afb687b6..c7b077b0b6d2ae67d8c467c912f2edf81e0ec8e9 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 bfc4c833fee12b73b0133ec6f9476fbc1b61fa0b..82705a71d7c72c55a686a7616db3dc19b0f25bd9 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -145,6 +145,10 @@ syntax: id: 127 message: "Conflicting modifiers '!' and '?' on field." +- 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." @@ -227,8 +231,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 @@ -252,8 +256,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 @@ -453,8 +457,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 @@ -486,8 +490,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 @@ -571,8 +575,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 @@ -740,8 +744,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 @@ -1226,8 +1230,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