From f267a72bace01931df0c0ce104080f5a0174a2d9 Mon Sep 17 00:00:00 2001 From: MuSilk Date: Mon, 1 Sep 2025 14:24:58 +0800 Subject: [PATCH] Fix array type as function or typeof argument bug Issue: [Bug]: array type used as function or typeof argument https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICVQJL Signed-off-by: MuSilk --- ets2panda/parser/ETSparserExpressions.cpp | 8 ++++- .../ast/compiler/ets/array_type_as_param.ets | 26 +++++++++++++++ .../ast/compiler/ets/array_type_as_value.ets | 33 +++++++++++++++++++ .../test/ast/compiler/ets/type_as_value.ets | 2 +- .../ast/parser/ets/MultipleParserErrors.ets | 3 +- .../ets/readonlyFunctionTypeAnnotation.ets | 2 +- .../ast/parser/ets/unexpected_token_38.ets | 1 - 7 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/array_type_as_param.ets create mode 100644 ets2panda/test/ast/compiler/ets/array_type_as_value.ets diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 9b7dc49103..5ad55ecac7 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -311,8 +311,14 @@ ir::Expression *ETSParser::ParseDefaultPrimaryExpression(ExpressionParseFlags fl ir::TypeNode *potentialType = ParseTypeAnnotation(&options); if (potentialType != nullptr) { + // #29702 remove special handling for TSArray. if (potentialType->IsTSArrayType()) { - return potentialType; + auto currentSavedPos = Lexer()->Save(); + Lexer()->Rewind(savedPos); + const auto tokenNow = Lexer()->GetToken(); + LogUnexpectedToken(tokenNow); + Lexer()->Rewind(currentSavedPos); + return AllocBrokenExpression(tokenNow.Loc()); } if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_PERIOD) { diff --git a/ets2panda/test/ast/compiler/ets/array_type_as_param.ets b/ets2panda/test/ast/compiler/ets/array_type_as_param.ets new file mode 100644 index 0000000000..b8f173ad20 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/array_type_as_param.ets @@ -0,0 +1,26 @@ +/* + * 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:number[]){ + +} + +function main(){ + foo(typeof number[]) + foo(number[]) +} + +/* @@? 21:16 Error SyntaxError: Unexpected token 'number'. */ +/* @@? 22:9 Error SyntaxError: Unexpected token 'number'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/array_type_as_value.ets b/ets2panda/test/ast/compiler/ets/array_type_as_value.ets new file mode 100644 index 0000000000..20dbca68ad --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/array_type_as_value.ets @@ -0,0 +1,33 @@ +/* + * 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 A {a0 : number[] = []} + +function main(){ + let v1 = [number[]] + let v2 : A = {a0: number[]} + if(Int[]){} + while(number[]){} + let v3 = int[] && true + let v4 = long[] || false +} + +/* @@? 19:15 Error SyntaxError: Unexpected token 'number'. */ +/* @@? 20:23 Error SyntaxError: Unexpected token 'number'. */ +/* @@? 21:8 Error SyntaxError: Unexpected token 'Int'. */ +/* @@? 22:11 Error SyntaxError: Unexpected token 'number'. */ +/* @@? 23:5 Error SyntaxError: Missing condition in while statement */ +/* @@? 23:14 Error SyntaxError: Unexpected token 'int'. */ +/* @@? 24:14 Error SyntaxError: Unexpected token 'long'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/type_as_value.ets b/ets2panda/test/ast/compiler/ets/type_as_value.ets index 2fdb848aae..fb69b217cc 100644 --- a/ets2panda/test/ast/compiler/ets/type_as_value.ets +++ b/ets2panda/test/ast/compiler/ets/type_as_value.ets @@ -15,4 +15,4 @@ let a = number[][] -/* @@? 16:17 Error TypeError: 'Array>' only refers to a type, but is being used as a value here. */ \ No newline at end of file +/* @@? 16:9 Error SyntaxError: Unexpected token 'number'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets b/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets index 257ff2bd18..6e6cf94a8e 100644 --- a/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets +++ b/ets2panda/test/ast/parser/ets/MultipleParserErrors.ets @@ -198,6 +198,7 @@ function main(): void { /* @@? 51:25 Error SyntaxError: Rest parameter must be the last formal parameter. */ /* @@? 51:27 Error SyntaxError: Unexpected token '...'. */ /* @@? 51:30 Error SyntaxError: Unexpected token 'p'. */ +/* @@? 51:33 Error SyntaxError: Unexpected token 'int'. */ /* @@? 51:33 Error SyntaxError: Label must be followed by a loop statement. */ /* @@? 51:38 Error SyntaxError: Unexpected token ')'. */ /* @@? 51:39 Error SyntaxError: Unexpected token ':'. */ @@ -288,4 +289,4 @@ function main(): void { /* @@? 165:5 Error TypeError: This expression is not callable. */ /* @@? 166:5 Error TypeError: No matching call signature */ /* @@? 166:5 Error TypeError: Expected 1 arguments, got 0. */ -/* @@? 292:1 Error SyntaxError: Expected '}', got 'end of stream'. */ +/* @@? 293:1 Error SyntaxError: Expected '}', got 'end of stream'. */ diff --git a/ets2panda/test/ast/parser/ets/readonlyFunctionTypeAnnotation.ets b/ets2panda/test/ast/parser/ets/readonlyFunctionTypeAnnotation.ets index 686f313ee0..5b7703cef0 100644 --- a/ets2panda/test/ast/parser/ets/readonlyFunctionTypeAnnotation.ets +++ b/ets2panda/test/ast/parser/ets/readonlyFunctionTypeAnnotation.ets @@ -22,6 +22,6 @@ func: readonly (Y : object | long [] ) => [ ] /* @@? 17:20 Error SyntaxError: Unexpected token ':'. */ /* @@? 17:22 Error SyntaxError: Unexpected token 'object'. */ /* @@? 17:22 Error TypeError: Type name 'object' used in the wrong context */ -/* @@? 17:22 Error TypeError: Bad operand type, the types of the operands must be numeric type. */ +/* @@? 17:31 Error SyntaxError: Unexpected token 'long'. */ /* @@? 17:39 Error SyntaxError: Unexpected token ')'. */ /* @@? 17:42 Error SyntaxError: Unexpected token '=>'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_38.ets b/ets2panda/test/ast/parser/ets/unexpected_token_38.ets index 780e05d962..cf3bc0a1d1 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_38.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_38.ets @@ -28,4 +28,3 @@ let func3: (f: (a: number, b: string) => number[]): number[] => (a: number, b: b /* @@? 22:51 Error SyntaxError: Unexpected token '=>'. */ /* @@? 22:53 Error SyntaxError: Unexpected token 'number'. */ /* @@? 22:62 Error SyntaxError: Unexpected token '=>'. */ -/* @@? 22:62 Error SyntaxError: Unexpected token. */ -- Gitee