diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 2a7ea152f1d0c42814411b6665fbd1e30fd2b4d0..09542e5e65f4bd1247a6289e08c7f10b3aaeb614 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1630,6 +1630,16 @@ bool ETSParser::IsFixedArrayTypeNode(ir::AstNode *node) ir::Expression *ETSParser::ParseFunctionParameter() { + switch (Lexer()->GetToken().Type()) { + case lexer::TokenType::KEYW_PRIVATE: + case lexer::TokenType::KEYW_PUBLIC: + case lexer::TokenType::KEYW_PROTECTED: + LogError(diagnostic::FIELD_IN_PARAM); + Lexer()->NextToken(); + default: + break; + } + if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_AT) { return ParseFunctionParameterAnnotations(); } diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 7a0cb42540017e338534f2b12a247320c7db9296..bff2cbec61e8ca9a642255a35da2c38a699fa4e7 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -359,9 +359,13 @@ ir::Expression *ETSParser::ParsePrimaryExpression(ExpressionParseFlags flags) } case lexer::TokenType::KEYW_TYPE: { LogError(diagnostic::TYPE_ALIAS_ONLY_TOP_LEVEL); - const auto &rangeToken = Lexer()->GetToken().Loc(); ParseTypeAliasDeclaration(); // Try to parse type alias and drop the result. - return AllocBrokenExpression(rangeToken); + return AllocBrokenExpression(Lexer()->GetToken().Loc()); + } + case lexer::TokenType::KEYW_FUNCTION: { + LogError(diagnostic::FUNC_EXPR); + ParseFunctionDeclaration(true, ir::ModifierFlags::NONE); + return AllocBrokenExpression(Lexer()->GetToken().Loc()); } case lexer::TokenType::PUNCTUATOR_FORMAT: { return ParseExpressionFormatPlaceholder(); diff --git a/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets b/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets index 62d52f75f5cb2155a2d76209841e60a88aca9c41..f4850751e54e1f9d72894560bd436e6a932569d9 100644 --- a/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets +++ b/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets @@ -20,5 +20,6 @@ const b = a.map(function (e) { return e * 2; }); -/* @@? 16:2 Error SyntaxError: Unexpected token 'function'. */ -/* @@? 19:17 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 16:2 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:17 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:28 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/test/ast/parser/ets/decl_in_param.ets b/ets2panda/test/ast/parser/ets/decl_in_param.ets new file mode 100644 index 0000000000000000000000000000000000000000..39e7ac9b8f22cb6f5796d6ff0e57cd8a085c4823 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/decl_in_param.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +function f(public x: number){ +} +class C{ + constructor(public m:number) {} + m(public x:number) {} +} +/* @@? 16:12 Error SyntaxError: Declaring fields in parameter list is not supported */ +/* @@? 19:17 Error SyntaxError: Declaring fields in parameter list is not supported */ +/* @@? 20:7 Error SyntaxError: Declaring fields in parameter list is not supported */ diff --git a/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets b/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets index 0d648f4ea72508aa1f13ee99f58bafac51877434..e085a3efaffb4ed91f7fcca39ce993d05794b0f6 100644 --- a/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets +++ b/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets @@ -21,6 +21,7 @@ return x+1; }); -/* @@? 16:2 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 16:2 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ /* @@? 20:1 Error TypeError: Can't resolve array type */ -/* @@? 20:8 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 20:8 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 20:18 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 8a1a9bafa9cc0a3418103f5191e12558fdcde633..c576b1fe9a0dc899183473b15436d0a13946b007 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1267,3 +1267,11 @@ syntax: - name: GENERATOR_FUNCTION id: 314 message: "Generator functions are not supported, please use async/await mechanism for multitasking" + +- name: FIELD_IN_PARAM + id: 315 + message: "Declaring fields in parameter list is not supported" + +- name: FUNC_EXPR + id: 316 + message: "Function expressions are not supported, use arrow functions instead"