diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 73ab94b616b1e7374ef3ad0cbca3debb0c08d65c..bdd4bb1bfb8e168d60ea7c3f0654435b01a14e14 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1762,8 +1762,11 @@ ir::Expression *ETSParser::ParseFunctionParameter() typeAnnotation->SetParent(paramIdent); paramIdent->SetTsTypeAnnotation(typeAnnotation); paramIdent->SetEnd(typeAnnotation->End()); - } else if (!isArrow && !isOptional) { + } + + if ((!isArrow || isOptional) && paramIdent->TypeAnnotation() == nullptr) { LogError(diagnostic::EXPLICIT_PARAM_TYPE); + paramIdent->SetTsTypeAnnotation(AllocBrokenType(Lexer()->GetToken().Loc())); } return ParseFunctionParameterExpression(paramIdent, isOptional); diff --git a/ets2panda/test/ast/compiler/ets/invalid_param_pack.ets b/ets2panda/test/ast/compiler/ets/invalid_param_pack.ets index 448d15b445d902cabb2ddbfbd16521844d70bb4f..47fe5706e0c2d9e4fbbf985b201d83bbb2a10a1d 100644 --- a/ets2panda/test/ast/compiler/ets/invalid_param_pack.ets +++ b/ets2panda/test/ast/compiler/ets/invalid_param_pack.ets @@ -38,10 +38,8 @@ class Derived2 extends Base2{ } /* @@? 17:17 Error SyntaxError: Rest parameter should be either array or tuple type. */ -/* @@? 17:17 Error TypeError: The type of parameter 'args' cannot be inferred */ /* @@? 17:24 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 24:3 Error TypeError: No matching call signature */ /* @@? 29:13 Error SyntaxError: Rest parameter should be either array or tuple type. */ -/* @@? 29:13 Error TypeError: The type of parameter 'args' cannot be inferred */ /* @@? 29:20 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 36:3 Error TypeError: Unresolved reference my_func */ diff --git a/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets b/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets index e23f26a1e73d09f55811c7adb00e3db7161d0a8f..71e76536db95195f451ec2584e8ae947aa24a29d 100644 --- a/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets +++ b/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets @@ -20,5 +20,4 @@ interface inter { function foo(i: Partial>): void {} -/* @@? 18:15 Error TypeError: The type of parameter 'A' cannot be inferred */ /* @@? 18:17 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/test/ast/compiler/ets/unexpected_param_01.ets b/ets2panda/test/ast/compiler/ets/unexpected_param_01.ets new file mode 100644 index 0000000000000000000000000000000000000000..5e15bcbf7dada134279d2b9f12f5c685e0b3a4ab --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/unexpected_param_01.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +let byteP: Promise = byteFunc() +let pthen = byteP.then((value ? Object): Object => { + return value + +}); +await byteP; + +/* @@? 16:30 Error TypeError: Unresolved reference byteFunc */ +/* @@? 17:41 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 17:41 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 17:41 Error SyntaxError: Expected '=>', got 'identification literal'. */ +/* @@? 17:41 Error SyntaxError: Class cannot be used as object. */ +/* @@? 17:48 Error SyntaxError: Unexpected token ':'. */ +/* @@? 17:50 Error SyntaxError: Unexpected token 'Object'. */ +/* @@? 17:50 Error TypeError: The type of parameter 'Object' cannot be inferred */ +/* @@? 18:12 Error TypeError: Unresolved reference value */ +/* @@? 20:2 Error SyntaxError: Unexpected token ')'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/unexpected_param_02.ets b/ets2panda/test/ast/compiler/ets/unexpected_param_02.ets new file mode 100644 index 0000000000000000000000000000000000000000..c5bb53c7ebb55422acc3f0b24ff8b109b185e907 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/unexpected_param_02.ets @@ -0,0 +1,18 @@ +/* + * 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 foo(a?){} + +/* @@? 16:16 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/FixedArray/MultipleParserErrors.ets b/ets2panda/test/ast/parser/ets/FixedArray/MultipleParserErrors.ets index 7fa2468144f7fe14e9182bdb19ef92e8bc7ab075..bf3c5717b906f081bc34692dda911d86fb0597a8 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/MultipleParserErrors.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/MultipleParserErrors.ets @@ -172,8 +172,8 @@ function main(): void { /* @@? 37:33 Error SyntaxError: Unexpected token 'rethrows'. */ /* @@? 37:33 Error TypeError: Unresolved reference rethrows */ /* @@? 39:14 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ -/* @@? 39:14 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 39:14 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 39:14 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 39:24 Error SyntaxError: Unexpected token 'int'. */ /* @@? 39:27 Error SyntaxError: Unexpected token ')'. */ /* @@? 39:29 Error SyntaxError: Unexpected token '{'. */ @@ -189,12 +189,11 @@ function main(): void { /* @@? 43:11 Error SyntaxError: Invalid left-hand side in assignment expression. */ /* @@? 43:13 Error SyntaxError: Unexpected token 'byte'. */ /* @@? 45:6 Error SyntaxError: Unexpected token 'this'. */ -/* @@? 45:6 Error TypeError: Invalid left-hand side of assignment expression */ /* @@? 45:6 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 45:6 Error TypeError: Invalid left-hand side of assignment expression */ /* @@? 45:11 Error SyntaxError: Invalid left-hand side in assignment expression. */ /* @@? 47:8 Error SyntaxError: Variable must be initialized or it's type must be declared. */ /* @@? 47:8 Error TypeError: Missing initializer in const declaration */ -/* @@? 49:12 Error TypeError: The type of parameter 'a' cannot be inferred */ /* @@? 49:13 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 51:35 Error SyntaxError: Rest parameter must be the last formal parameter. */ /* @@? 51:37 Error SyntaxError: Unexpected token '...'. */ @@ -241,10 +240,10 @@ function main(): void { /* @@? 115:38 Error SyntaxError: Unexpected token '{'. */ /* @@? 116:5 Error SyntaxError: return keyword should be used in function body. */ /* @@? 116:12 Error TypeError: All return statements in the function should be empty or have a value. */ +/* @@? 119:26 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 119:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ -/* @@? 119:26 Error SyntaxError: Unexpected token 'case'. */ /* @@? 119:26 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 119:26 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 119:26 Error SyntaxError: Unexpected token 'case'. */ /* @@? 119:30 Error SyntaxError: Unexpected token ':'. */ /* @@? 119:32 Error SyntaxError: Unexpected token 'U'. */ /* @@? 119:33 Error SyntaxError: Unexpected token ')'. */ @@ -254,8 +253,8 @@ function main(): void { /* @@? 120:5 Error SyntaxError: return keyword should be used in function body. */ /* @@? 120:12 Error TypeError: All return statements in the function should be empty or have a value. */ /* @@? 123:5 Error SyntaxError: Identifier expected, got ','. */ -/* @@? 123:6 Error SyntaxError: Variable must be initialized or it's type must be declared. */ /* @@? 123:6 Error SyntaxError: Unexpected token 'abc'. */ +/* @@? 123:6 Error SyntaxError: Variable must be initialized or it's type must be declared. */ /* @@? 123:6 Error TypeError: Unresolved reference abc */ /* @@? 127:1 Error SyntaxError: The modifier async cannot be used in an ambient context. */ /* @@? 132:14 Error SyntaxError: Unexpected token, expected an identifier. */ @@ -285,4 +284,4 @@ function main(): void { /* @@? 165:5 Error TypeError: This expression is not callable. */ /* @@? 166:5 Error TypeError: Expected 1 arguments, got 0. */ /* @@? 166:5 Error TypeError: No matching call signature */ -/* @@? 289:1 Error SyntaxError: Expected '}', got 'end of stream'. */ +/* @@? 288:1 Error SyntaxError: Expected '}', got 'end of stream'. */ diff --git a/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets b/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets index 804e16d1ad836a82c4596faf50287174b5ecbf9f..134fd0804fc8e36b16d4e607ab019596474b96ac 100644 --- a/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets +++ b/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets @@ -172,8 +172,8 @@ function main(): void { /* @@? 37:33 Error SyntaxError: Unexpected token 'rethrows'. */ /* @@? 37:33 Error TypeError: Unresolved reference rethrows */ /* @@? 39:14 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ -/* @@? 39:14 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 39:14 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 39:14 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 39:24 Error SyntaxError: Unexpected token 'int'. */ /* @@? 39:27 Error SyntaxError: Unexpected token ')'. */ /* @@? 39:29 Error SyntaxError: Unexpected token '{'. */ @@ -189,12 +189,11 @@ function main(): void { /* @@? 43:11 Error SyntaxError: Invalid left-hand side in assignment expression. */ /* @@? 43:13 Error SyntaxError: Unexpected token 'byte'. */ /* @@? 45:6 Error SyntaxError: Unexpected token 'this'. */ -/* @@? 45:6 Error TypeError: Invalid left-hand side of assignment expression */ /* @@? 45:6 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 45:6 Error TypeError: Invalid left-hand side of assignment expression */ /* @@? 45:11 Error SyntaxError: Invalid left-hand side in assignment expression. */ /* @@? 47:8 Error SyntaxError: Variable must be initialized or it's type must be declared. */ /* @@? 47:8 Error TypeError: Missing initializer in const declaration */ -/* @@? 49:12 Error TypeError: The type of parameter 'a' cannot be inferred */ /* @@? 49:13 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 51:25 Error SyntaxError: Rest parameter must be the last formal parameter. */ /* @@? 51:27 Error SyntaxError: Unexpected token '...'. */ @@ -225,10 +224,10 @@ function main(): void { /* @@? 92:7 Error TypeError: Variable 'A' has already been declared. */ /* @@? 92:7 Error TypeError: Merging declarations is not supported, please keep all definitions of classes, interfaces and enums compact in the codebase! */ /* @@? 93:3 Error SyntaxError: Unexpected token. A constructor, method, accessor, or property was expected. */ -/* @@? 115:26 Error SyntaxError: Unexpected token, expected an identifier. */ -/* @@? 115:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ -/* @@? 115:26 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 115:26 Error SyntaxError: Unexpected token 'case'. */ +/* @@? 115:26 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 115:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 115:26 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 115:30 Error SyntaxError: Unexpected token ':'. */ /* @@? 115:32 Error SyntaxError: Unexpected token 'U'. */ /* @@? 115:32 Error TypeError: Unresolved reference U */ @@ -239,10 +238,10 @@ function main(): void { /* @@? 115:38 Error SyntaxError: Unexpected token '{'. */ /* @@? 116:5 Error SyntaxError: return keyword should be used in function body. */ /* @@? 116:12 Error TypeError: All return statements in the function should be empty or have a value. */ -/* @@? 119:26 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 119:26 Error SyntaxError: Unexpected token 'case'. */ /* @@? 119:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 119:26 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 119:26 Error SyntaxError: Unexpected token 'case'. */ +/* @@? 119:26 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 119:30 Error SyntaxError: Unexpected token ':'. */ /* @@? 119:32 Error SyntaxError: Unexpected token 'U'. */ /* @@? 119:33 Error SyntaxError: Unexpected token ')'. */ @@ -257,10 +256,10 @@ function main(): void { /* @@? 123:6 Error TypeError: Unresolved reference abc */ /* @@? 127:1 Error SyntaxError: The modifier async cannot be used in an ambient context. */ /* @@? 132:14 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 137:16 Error SyntaxError: Unexpected token '='. */ /* @@? 137:16 Error SyntaxError: Expected '{', got '='. */ /* @@? 137:16 Error SyntaxError: Expected ')', got '='. */ /* @@? 137:16 Error SyntaxError: Catch clause variable cannot have an initializer. */ -/* @@? 137:16 Error SyntaxError: Unexpected token '='. */ /* @@? 137:18 Error SyntaxError: Unexpected token '0'. */ /* @@? 137:19 Error SyntaxError: Unexpected token ')'. */ /* @@? 137:21 Error SyntaxError: Unexpected token '{'. */ @@ -283,4 +282,4 @@ function main(): void { /* @@? 165:5 Error TypeError: This expression is not callable. */ /* @@? 166:5 Error TypeError: Expected 1 arguments, got 0. */ /* @@? 166:5 Error TypeError: No matching call signature */ -/* @@? 287:1 Error SyntaxError: Expected '}', got 'end of stream'. */ +/* @@? 286:1 Error SyntaxError: Expected '}', got 'end of stream'. */ diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets index 439897e7a608a875f61fb5e501d5ad24cda8c003..d5077544840f5baec9e408280c5c401c4a4c1374 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_bad_param07.ets @@ -19,9 +19,7 @@ } -function foo(@MyAnno ...a/* @@ label */){} - -/* @@@ label Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +function foo(@MyAnno ...a){} /* @@? 22:15 Error TypeError: The required field 'testProperty1' must be specified. Fields without default values cannot be omitted. */ -/* @@? 22:22 Error TypeError: The type of parameter 'a' cannot be inferred */ /* @@? 22:22 Error SyntaxError: Rest parameter should be either array or tuple type. */ +/* @@? 22:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_missing_AT_for_function_param.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_missing_AT_for_function_param.ets index a87e83104e6429bc3e96a54ea7f42dcc4735732d..96304af4406e85870489c188070c4065893f4f47 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_missing_AT_for_function_param.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationUsage_missing_AT_for_function_param.ets @@ -26,17 +26,15 @@ function foo(MyAnno({testProperty1: ""}) x: int, MyAnno({testProperty1: ""}) y: } /* @@? 19:10 Error TypeError: Only abstract or native methods can't have body. */ -/* @@? 19:14 Error TypeError: The type of parameter 'MyAnno' cannot be inferred */ /* @@? 19:20 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 19:20 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 19:20 Error TypeError: need to specify target type for class composite */ /* @@? 19:42 Error SyntaxError: Unexpected token 'x'. */ -/* @@? 19:45 Error SyntaxError: Unexpected token 'int'. */ /* @@? 19:45 Error SyntaxError: Label must be followed by a loop statement. */ +/* @@? 19:45 Error SyntaxError: Unexpected token 'int'. */ /* @@? 19:48 Error SyntaxError: Unexpected token ')'. */ /* @@? 19:50 Error SyntaxError: Unexpected token '{'. */ /* @@? 22:1 Error TypeError: Function foo is already declared. */ -/* @@? 22:14 Error TypeError: The type of parameter 'MyAnno' cannot be inferred */ /* @@? 22:20 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 22:20 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 22:20 Error TypeError: need to specify target type for class composite */ @@ -53,7 +51,6 @@ function foo(MyAnno({testProperty1: ""}) x: int, MyAnno({testProperty1: ""}) y: /* @@? 22:87 Error SyntaxError: Unexpected token ')'. */ /* @@? 22:89 Error SyntaxError: Unexpected token '{'. */ /* @@? 25:1 Error TypeError: Function foo is already declared. */ -/* @@? 25:14 Error TypeError: The type of parameter 'MyAnno' cannot be inferred */ /* @@? 25:20 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 25:20 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 25:20 Error TypeError: need to specify target type for class composite */ @@ -68,4 +65,4 @@ function foo(MyAnno({testProperty1: ""}) x: int, MyAnno({testProperty1: ""}) y: /* @@? 25:81 Error SyntaxError: Label must be followed by a loop statement. */ /* @@? 25:81 Error TypeError: Type name 'string' used in the wrong context */ /* @@? 25:87 Error SyntaxError: Unexpected token ')'. */ -/* @@? 25:89 Error SyntaxError: Unexpected token '{'. */ +/* @@? 25:89 Error SyntaxError: Unexpected token '{'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/async_function_bad.ets b/ets2panda/test/ast/parser/ets/async_function_bad.ets index b55c2ffaf805c847953bbbdc5b6a313d38d8bde5..d7c4a852b25b8e90f91677213358c186e36d53bd 100644 --- a/ets2panda/test/ast/parser/ets/async_function_bad.ets +++ b/ets2panda/test/ast/parser/ets/async_function_bad.ets @@ -19,7 +19,6 @@ async native function foo(): Promise; /* @@? 16:14 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 16:14 Error SyntaxError: Unexpected token, expected '('. */ /* @@? 16:14 Error TypeError: Only abstract or native methods can't have body. */ -/* @@? 16:23 Error TypeError: The type of parameter 'foo' cannot be inferred */ /* @@? 16:26 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ /* @@? 16:26 Error SyntaxError: Unexpected token, expected ',' or ')'. */ /* @@? 16:27 Error SyntaxError: Unexpected token ')'. */ 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 e085a3efaffb4ed91f7fcca39ce993d05794b0f6..dab191d5b7e27239944032f95c54f80efa65ebaa 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 @@ -25,3 +25,5 @@ /* @@? 20:1 Error TypeError: Can't resolve array type */ /* @@? 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. */ + +