From 99984689a09f7abd80eb5351845cc4e40e630c58 Mon Sep 17 00:00:00 2001 From: l00799755 Date: Tue, 23 Apr 2024 20:53:28 +0800 Subject: [PATCH 1/3] Fix seg fault of overloading generics Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I9I2BC Internal issue: #16332,#16664 Reason When passing no type argument and there is no deault type parameter set meanwhile, there should be CTE but we get segment fault instead. Description When checking number of type arguments, the return statement has been removed when type argument is nullptr, allowing for further checks for type argument. Tests ninja es2panda_tests bash ets_testtunner.sh --cts -r -rt All required pre-merge tests passed. Results are available in the ggwatcher. Signed-off-by: l00799755 --- ets2panda/checker/ets/helpers.cpp | 4 - ...rics_type_param_constraint_11-expected.txt | 1115 +++++++++ .../ets/generics_type_param_constraint_11.ets | 20 + ...rics_type_param_constraint_12-expected.txt | 2044 +++++++++++++++++ .../ets/generics_type_param_constraint_12.ets | 41 + ..._param_no_typeargs_no_default-expected.txt | 1004 ++++++++ ...rics_type_param_no_typeargs_no_default.ets | 22 + ...aram_no_typeargs_no_default_2-expected.txt | 1933 ++++++++++++++++ ...cs_type_param_no_typeargs_no_default_2.ets | 41 + 9 files changed, 6220 insertions(+), 4 deletions(-) create mode 100644 ets2panda/test/parser/ets/generics_type_param_constraint_11-expected.txt create mode 100644 ets2panda/test/parser/ets/generics_type_param_constraint_11.ets create mode 100644 ets2panda/test/parser/ets/generics_type_param_constraint_12-expected.txt create mode 100644 ets2panda/test/parser/ets/generics_type_param_constraint_12.ets create mode 100644 ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default-expected.txt create mode 100644 ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default.ets create mode 100644 ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2-expected.txt create mode 100644 ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 374564cbe9..a5ee1b3bb6 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1921,10 +1921,6 @@ void ETSChecker::CheckNumberOfTypeArguments(ETSObjectType *const type, ir::TSTyp return; } - if (typeArgs == nullptr) { - return; - } - size_t minimumTypeArgs = std::count_if(typeParams.begin(), typeParams.end(), [](Type *param) { return param->AsETSTypeParameter()->GetDefaultType() == nullptr; }); diff --git a/ets2panda/test/parser/ets/generics_type_param_constraint_11-expected.txt b/ets2panda/test/parser/ets/generics_type_param_constraint_11-expected.txt new file mode 100644 index 0000000000..b10518c48f --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_constraint_11-expected.txt @@ -0,0 +1,1115 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 7 + }, + "end": { + "line": 15, + "column": 11 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 12 + }, + "end": { + "line": 15, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 12 + }, + "end": { + "line": 15, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 11 + }, + "end": { + "line": 15, + "column": 14 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 2 + }, + "end": { + "line": 16, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 2 + }, + "end": { + "line": 16, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 19 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 22 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 2 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 15 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 27 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 28 + }, + "end": { + "line": 20, + "column": 35 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 23 + }, + "end": { + "line": 20, + "column": 37 + } + } + }, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 38 + }, + "end": { + "line": 20, + "column": 38 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 21, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/generics_type_param_constraint_11.ets b/ets2panda/test/parser/ets/generics_type_param_constraint_11.ets new file mode 100644 index 0000000000..6879b3a3d8 --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_constraint_11.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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 Base { + foo (p : T) {} + foo (p : Base) {} + foo (p : Derived) {} +} +class Derived extends Base {} diff --git a/ets2panda/test/parser/ets/generics_type_param_constraint_12-expected.txt b/ets2panda/test/parser/ets/generics_type_param_constraint_12-expected.txt new file mode 100644 index 0000000000..24ecb5ee1c --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_constraint_12-expected.txt @@ -0,0 +1,2044 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Unpackable", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 11 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Unpackable", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 37 + }, + "end": { + "line": 20, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 37 + }, + "end": { + "line": 20, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 37 + }, + "end": { + "line": 20, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 36 + }, + "end": { + "line": 20, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 26 + }, + "end": { + "line": 20, + "column": 41 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 11 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 27 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "right": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 23 + }, + "end": { + "line": 24, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 20 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 25 + }, + "end": { + "line": 27, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 27 + }, + "end": { + "line": 29, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 20 + }, + "end": { + "line": 29, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 20 + }, + "end": { + "line": 29, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 29, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 40 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 10 + }, + "end": { + "line": 32, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 10 + }, + "end": { + "line": 32, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 33 + }, + "end": { + "line": 32, + "column": 37 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 38 + }, + "end": { + "line": 32, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 38 + }, + "end": { + "line": 32, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 38 + }, + "end": { + "line": 32, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 37 + }, + "end": { + "line": 32, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 33 + }, + "end": { + "line": 32, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 33 + }, + "end": { + "line": 32, + "column": 41 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 41 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 26 + }, + "end": { + "line": 32, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 26 + }, + "end": { + "line": 32, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 32, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 13 + }, + "end": { + "line": 33, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "Value is ", + "loc": { + "start": { + "line": 33, + "column": 17 + }, + "end": { + "line": 33, + "column": 28 + } + } + }, + { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 30 + }, + "end": { + "line": 33, + "column": 31 + } + } + }, + "property": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 33, + "column": 32 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 30 + }, + "end": { + "line": 33, + "column": 40 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 30 + }, + "end": { + "line": 33, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 32, + "column": 42 + }, + "end": { + "line": 34, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 34, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 34, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 34, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "doSomethingUseful", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 10 + }, + "end": { + "line": 36, + "column": 27 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "doSomethingUseful", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 10 + }, + "end": { + "line": 36, + "column": 27 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "box1", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 9 + }, + "end": { + "line": 37, + "column": 13 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 20 + }, + "end": { + "line": 37, + "column": 24 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 25 + }, + "end": { + "line": 37, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 25 + }, + "end": { + "line": 37, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 25 + }, + "end": { + "line": 37, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 37, + "column": 24 + }, + "end": { + "line": 37, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 20 + }, + "end": { + "line": 37, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 20 + }, + "end": { + "line": 37, + "column": 33 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 123, + "loc": { + "start": { + "line": 37, + "column": 33 + }, + "end": { + "line": 37, + "column": 36 + } + } + } + ], + "loc": { + "start": { + "line": 37, + "column": 16 + }, + "end": { + "line": 38, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 9 + }, + "end": { + "line": 38, + "column": 20 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 38, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "box1", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 21 + }, + "end": { + "line": 38, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 26 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "box2", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 9 + }, + "end": { + "line": 39, + "column": 13 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 20 + }, + "end": { + "line": 39, + "column": 24 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 25 + }, + "end": { + "line": 39, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 25 + }, + "end": { + "line": 39, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 25 + }, + "end": { + "line": 39, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 39, + "column": 24 + }, + "end": { + "line": 39, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 20 + }, + "end": { + "line": 39, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 20 + }, + "end": { + "line": 39, + "column": 33 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "hello", + "loc": { + "start": { + "line": 39, + "column": 33 + }, + "end": { + "line": 39, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 39, + "column": 16 + }, + "end": { + "line": 40, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 9 + }, + "end": { + "line": 40, + "column": 20 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 39, + "column": 5 + }, + "end": { + "line": 40, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 40, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "box2", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 21 + }, + "end": { + "line": 40, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 40, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 40, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 36, + "column": 30 + }, + "end": { + "line": 41, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 27 + }, + "end": { + "line": 41, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 27 + }, + "end": { + "line": 41, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 1 + }, + "end": { + "line": 41, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 42, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/generics_type_param_constraint_12.ets b/ets2panda/test/parser/ets/generics_type_param_constraint_12.ets new file mode 100644 index 0000000000..305863f3ba --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_constraint_12.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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. + */ + +interface Unpackable { + getValue() : T; +} + +class Boxx implements Unpackable { + value_ : T; + + constructor(value : T) { + this.value_ = value; + } + + public getValue() : T { + return this.value_; + } +} + +function printBoxedValue(x : Boxx) { + console.log("Value is ", x.getValue()) +} + +function doSomethingUseful() { + let box1 = new Boxx(123) + printBoxedValue(box1) + let box2 = new Boxx("hello") + printBoxedValue(box2) +} diff --git a/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default-expected.txt b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default-expected.txt new file mode 100644 index 0000000000..803f93e47a --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default-expected.txt @@ -0,0 +1,1004 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 13 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 16 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 22 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 2 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 2 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Derived", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Base", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 23 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 23 + }, + "end": { + "line": 22, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 23 + }, + "end": { + "line": 22, + "column": 29 + } + } + }, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 30 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 23, + "column": 1 + } + } +} +TypeError: Type 'Base' is generic but type argument were not provided. [generics_type_param_no_typeargs_no_default.ets:18:11] diff --git a/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default.ets b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default.ets new file mode 100644 index 0000000000..b96214c412 --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 Base { + foo (p : T) {} + foo (p : Base) {} + foo (p : Derived) {} +} + +class Derived extends Base {} diff --git a/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2-expected.txt b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2-expected.txt new file mode 100644 index 0000000000..4b7116207c --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2-expected.txt @@ -0,0 +1,1933 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 20 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 5 + }, + "end": { + "line": 16, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 25 + }, + "end": { + "line": 17, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Unpackable", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 11 + }, + "end": { + "line": 15, + "column": 21 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 15, + "column": 22 + }, + "end": { + "line": 15, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 22 + }, + "end": { + "line": 15, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 15, + "column": 21 + }, + "end": { + "line": 15, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 15, + "column": 1 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Unpackable", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 36 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 36 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 41 + } + } + } + ], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 11 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 27 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "right": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 23 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 20 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 25 + }, + "end": { + "line": 26, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 25 + }, + "end": { + "line": 26, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 25 + }, + "end": { + "line": 26, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 20 + } + } + }, + "property": { + "type": "Identifier", + "name": "value_", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 21 + }, + "end": { + "line": 27, + "column": 27 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 27, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 9 + }, + "end": { + "line": 27, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 27 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 28, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 28, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 40 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 25 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 33 + }, + "end": { + "line": 31, + "column": 37 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 38 + }, + "end": { + "line": 31, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 38 + }, + "end": { + "line": 31, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 38 + }, + "end": { + "line": 31, + "column": 40 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 37 + }, + "end": { + "line": 31, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 33 + }, + "end": { + "line": 31, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 33 + }, + "end": { + "line": 31, + "column": 41 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 29 + }, + "end": { + "line": 31, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 29 + }, + "end": { + "line": 31, + "column": 41 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 26 + }, + "end": { + "line": 31, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 26 + }, + "end": { + "line": 31, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 25 + }, + "end": { + "line": 31, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 13 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "Value is ", + "loc": { + "start": { + "line": 32, + "column": 17 + }, + "end": { + "line": 32, + "column": 28 + } + } + }, + { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 30 + }, + "end": { + "line": 32, + "column": 31 + } + } + }, + "property": { + "type": "Identifier", + "name": "getValue", + "decorators": [], + "loc": { + "start": { + "line": 32, + "column": 32 + }, + "end": { + "line": 32, + "column": 40 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 32, + "column": 30 + }, + "end": { + "line": 32, + "column": 40 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 32, + "column": 30 + }, + "end": { + "line": 32, + "column": 42 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 42 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 25 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 25 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "doSomethingUseful", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 10 + }, + "end": { + "line": 35, + "column": 27 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "doSomethingUseful", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 10 + }, + "end": { + "line": 35, + "column": 27 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "box1", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 13 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 20 + }, + "end": { + "line": 36, + "column": 25 + } + } + }, + "arguments": [ + { + "type": "NumberLiteral", + "value": 123, + "loc": { + "start": { + "line": 36, + "column": 25 + }, + "end": { + "line": 36, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 36, + "column": 16 + }, + "end": { + "line": 37, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 37, + "column": 20 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 37, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "box1", + "decorators": [], + "loc": { + "start": { + "line": 37, + "column": 21 + }, + "end": { + "line": 37, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 37, + "column": 5 + }, + "end": { + "line": 37, + "column": 26 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "box2", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 38, + "column": 13 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Boxx", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 20 + }, + "end": { + "line": 38, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 20 + }, + "end": { + "line": 38, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 20 + }, + "end": { + "line": 38, + "column": 25 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "hello", + "loc": { + "start": { + "line": 38, + "column": 25 + }, + "end": { + "line": 38, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 38, + "column": 16 + }, + "end": { + "line": 39, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 39, + "column": 20 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 39, + "column": 20 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "printBoxedValue", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 5 + }, + "end": { + "line": 39, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "box2", + "decorators": [], + "loc": { + "start": { + "line": 39, + "column": 21 + }, + "end": { + "line": 39, + "column": 25 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 39, + "column": 5 + }, + "end": { + "line": 39, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 39, + "column": 5 + }, + "end": { + "line": 39, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 35, + "column": 30 + }, + "end": { + "line": 40, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 27 + }, + "end": { + "line": 40, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 27 + }, + "end": { + "line": 40, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 1 + }, + "end": { + "line": 40, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 42, + "column": 1 + } + } +} +TypeError: Type 'Boxx' is generic but type argument were not provided. [generics_type_param_no_typeargs_no_default_2.ets:36:20] diff --git a/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2.ets b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2.ets new file mode 100644 index 0000000000..9b637ed794 --- /dev/null +++ b/ets2panda/test/parser/ets/generics_type_param_no_typeargs_no_default_2.ets @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 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. + */ +interface Unpackable { + getValue() : T; +} + +class Boxx implements Unpackable { + value_ : T; + + constructor(value : T) { + this.value_ = value; + } + + public getValue() : T { + return this.value_; + } +} + +function printBoxedValue(x : Boxx) { + console.log("Value is ", x.getValue()) +} + +function doSomethingUseful() { + let box1 = new Boxx(123) + printBoxedValue(box1) + let box2 = new Boxx("hello") + printBoxedValue(box2) +} + -- Gitee From b063054065f2b80183dccb362122dc6134d04683 Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Mon, 29 Jan 2024 14:40:45 +0300 Subject: [PATCH 2/3] Add ACC_VARARGS flag for rest parameters Issue: #I8ZQQX Testing: CI tests passed Signed-off-by: Vyacheslav Cherkashin Signed-off-by: churkinaleksey --- ets2panda/checker/types/signature.h | 5 + ets2panda/compiler/core/ETSemitter.cpp | 8 +- ets2panda/ir/base/scriptFunction.h | 6 + ets2panda/test/CMakeLists.txt | 11 + .../test/unit/rest_parameter_flag_test.cpp | 411 ++++++++++++++++++ 5 files changed, 439 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/unit/rest_parameter_flag_test.cpp diff --git a/ets2panda/checker/types/signature.h b/ets2panda/checker/types/signature.h index e0ea0292e5..6f613bbee1 100644 --- a/ets2panda/checker/types/signature.h +++ b/ets2panda/checker/types/signature.h @@ -239,6 +239,11 @@ public: ss << compiler::Signatures::MANGLE_SEPARATOR; } + if (signatureInfo_->restVar != nullptr) { + MaybeBoxedType(context->Checker(), signatureInfo_->restVar)->ToAssemblerTypeWithRank(ss); + ss << compiler::Signatures::MANGLE_SEPARATOR; + } + returnType_->ToAssemblerTypeWithRank(ss); ss << compiler::Signatures::MANGLE_SEPARATOR; } diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index a35a088569..ba49cbcf2f 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -111,7 +111,6 @@ static pandasm::Function GenScriptFunction(CompilerContext const *context, const auto *paramScope = funcScope->ParamScope(); auto func = pandasm::Function(funcScope->InternalName().Mutf8(), EXTENSION); - func.params.reserve(paramScope->Params().size()); for (const auto *var : paramScope->Params()) { @@ -125,10 +124,15 @@ static pandasm::Function GenScriptFunction(CompilerContext const *context, const func.returnType = PandasmTypeWithRank(scriptFunc->Signature()->ReturnType()); } + uint32_t accessFlags = 0; if (!scriptFunc->IsStaticBlock()) { const auto *methodDef = util::Helpers::GetContainingClassMethodDefinition(scriptFunc); - func.metadata->SetAccessFlags(TranslateModifierFlags(methodDef->Modifiers())); + accessFlags |= TranslateModifierFlags(methodDef->Modifiers()); + } + if (scriptFunc->HasRestParameter()) { + accessFlags |= ACC_VARARGS; } + func.metadata->SetAccessFlags(accessFlags); return func; } diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index 2ca76dd107..7511ae3a26 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -17,6 +17,7 @@ #define ES2PANDA_PARSER_INCLUDE_AST_SCRIPT_FUNCTION_H #include "ir/statements/returnStatement.h" +#include "checker/types/signature.h" #include "ir/astNode.h" #include "varbinder/scope.h" #include "util/enumbitops.h" @@ -214,6 +215,11 @@ public: return body_ != nullptr; } + [[nodiscard]] bool HasRestParameter() const noexcept + { + return signature_->RestVar() != nullptr; + } + [[nodiscard]] bool IsThrowing() const noexcept { return (funcFlags_ & ir::ScriptFunctionFlags::THROWS) != 0; diff --git a/ets2panda/test/CMakeLists.txt b/ets2panda/test/CMakeLists.txt index 5e81e2016a..d82d4a9aa6 100644 --- a/ets2panda/test/CMakeLists.txt +++ b/ets2panda/test/CMakeLists.txt @@ -162,6 +162,17 @@ if(PANDA_REGRESSION_TESTS) ${PANDA_SANITIZERS_LIST} ) + # NOTE: es2panda_rest_parameter_flag test runs a lot of time on qemu, so let's disable it + if (NOT PANDA_QEMU_BUILD) + panda_add_gtest( + NAME es2panda_rest_parameter_flag + SOURCES unit/rest_parameter_flag_test.cpp + LIBRARIES es2panda-public es2panda-lib arkassembler arkbytecodeopt + INCLUDE_DIRS ${ES2PANDA_PATH} ${ES2PANDA_BINARY_ROOT} + SANITIZERS ${PANDA_SANITIZERS_LIST} + ) + endif() + add_subdirectory(tsconfig) endif() diff --git a/ets2panda/test/unit/rest_parameter_flag_test.cpp b/ets2panda/test/unit/rest_parameter_flag_test.cpp new file mode 100644 index 0000000000..c78f7f6e86 --- /dev/null +++ b/ets2panda/test/unit/rest_parameter_flag_test.cpp @@ -0,0 +1,411 @@ +/** + * Copyright (c) 2024 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. + */ + +#include + +#include "assembler/assembly-program.h" +#include "es2panda.h" +#include "generated/signatures.h" +#include "libpandabase/mem/mem.h" +#include "macros.h" +#include "mem/pool_manager.h" +#include "util/options.h" + +namespace ark::es2panda::compiler::test { + +class RestParameterTest : public testing::Test { +public: + RestParameterTest() + { + constexpr auto COMPILER_SIZE = 268435456; + + mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); + PoolManager::Initialize(PoolType::MMAP); + } + ~RestParameterTest() override + { + PoolManager::Finalize(); + mem::MemConfig::Finalize(); + } + + void SetCurrentProgram(std::string_view src) + { + int argc = 1; + const char *argv = "../../../bin/es2panda"; // NOLINT(modernize-avoid-c-arrays) + static constexpr std::string_view FILE_NAME = "dummy.ets"; + + program_ = GetProgram(argc, &argv, FILE_NAME, src); + ASSERT_NE(program_.get(), nullptr); + } + + void CheckRestParameterFlag(std::string_view functionName) + { + pandasm::Function *fn = GetFunction(functionName); + ASSERT_TRUE(fn != nullptr) << "Function '" << functionName << "' not found"; + ASSERT_TRUE(HasRestParameterFlag(fn)) << "Function '" << fn->name << "' doesn't have ACC_VARARGS flag"; + } + + void CheckNoRestParameterFlag(std::string_view functionName) + { + pandasm::Function *fn = GetFunction(functionName); + ASSERT_TRUE(fn != nullptr) << "Function '" << functionName << "' not found"; + ASSERT_FALSE(HasRestParameterFlag(fn)) << "Function '" << fn->name << "' has ACC_VARARGS flag"; + } + +private: + bool HasRestParameterFlag(pandasm::Function *fn) + { + return (fn->metadata->GetAccessFlags() & ACC_VARARGS) != 0; + } + + NO_COPY_SEMANTIC(RestParameterTest); + NO_MOVE_SEMANTIC(RestParameterTest); + + static std::unique_ptr GetProgram(int argc, const char **argv, std::string_view fileName, + std::string_view src) + { + auto options = std::make_unique(); + if (!options->Parse(argc, argv)) { + std::cerr << options->ErrorMsg() << std::endl; + return nullptr; + } + + Logger::ComponentMask mask {}; + mask.set(Logger::Component::ES2PANDA); + Logger::InitializeStdLogging(Logger::LevelFromString(options->LogLevel()), mask); + + es2panda::Compiler compiler(options->Extension(), options->ThreadCount()); + es2panda::SourceFile input(fileName, src, options->ParseModule()); + + return std::unique_ptr(compiler.Compile(input, options->CompilerOptions())); + } + + pandasm::Function *GetFunction(std::string_view functionName) + { + auto it = program_->functionTable.find(functionName.data()); + if (it == program_->functionTable.end()) { + return nullptr; + } + return &it->second; + } + +private: + std::unique_ptr program_ {}; +}; + +// === Function === +TEST_F(RestParameterTest, function_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + function fn(): void { + } + )"); + CheckNoRestParameterFlag("ETSGLOBAL.fn:void;"); +} + +TEST_F(RestParameterTest, function_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + function fn(args: int[]): void { + } + )"); + CheckNoRestParameterFlag("ETSGLOBAL.fn:i32[];void;"); +} + +TEST_F(RestParameterTest, function_without_rest_parameters_2) +{ + SetCurrentProgram(R"( + function fn(arg0: int, args: String[]): void { + } + )"); + CheckNoRestParameterFlag("ETSGLOBAL.fn:i32;std.core.String[];void;"); +} + +TEST_F(RestParameterTest, function_with_rest_parameter_0) +{ + SetCurrentProgram(R"( + function fn(...args: String[]): void { + } + )"); + CheckRestParameterFlag("ETSGLOBAL.fn:std.core.String[];void;"); +} + +TEST_F(RestParameterTest, function_with_rest_parameter_1) +{ + SetCurrentProgram(R"( + function fn(o: Object, ...args: int[]): void { + } + )"); + CheckRestParameterFlag("ETSGLOBAL.fn:std.core.Object;i32[];void;"); +} + +// === Method of class === +TEST_F(RestParameterTest, class_method_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + fn() {}; + } + )"); + CheckNoRestParameterFlag("A.fn:void;"); +} + +TEST_F(RestParameterTest, class_method_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + class A { + fn(arg0: int) {}; + } + )"); + CheckNoRestParameterFlag("A.fn:i32;void;"); +} + +TEST_F(RestParameterTest, class_method_with_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + fn(...args: int[]) {}; + } + )"); + CheckRestParameterFlag("A.fn:i32[];void;"); +} + +// === Static method of class === +TEST_F(RestParameterTest, static_class_method_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + static fn() {}; + } + )"); + CheckNoRestParameterFlag("A.fn:void;"); +} + +TEST_F(RestParameterTest, static_class_method_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + class A { + static fn(arg0: int) {}; + } + )"); + CheckNoRestParameterFlag("A.fn:i32;void;"); +} + +TEST_F(RestParameterTest, static_class_method_with_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + static fn(...args: int[]) {}; + } + )"); + CheckRestParameterFlag("A.fn:i32[];void;"); +} + +TEST_F(RestParameterTest, static_class_method_with_rest_parameters_1) +{ + SetCurrentProgram(R"( + class A { + static fn(a: String[], ...args: int[]) {}; + } + )"); + CheckRestParameterFlag("A.fn:std.core.String[];i32[];void;"); +} + +// === Constructor of class === +TEST_F(RestParameterTest, class_constructor_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + constructor() {}; + } + )"); + CheckNoRestParameterFlag("A.:void;"); +} + +TEST_F(RestParameterTest, class_constructor_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + class A { + constructor(args: String[]) {}; + } + )"); + CheckNoRestParameterFlag("A.:std.core.String[];void;"); +} + +TEST_F(RestParameterTest, class_constructor_with_rest_parameters_0) +{ + SetCurrentProgram(R"( + class A { + constructor(...args: int[]) {}; + } + )"); + CheckRestParameterFlag("A.:i32[];void;"); +} + +TEST_F(RestParameterTest, class_constructor_with_rest_parameters_1) +{ + SetCurrentProgram(R"( + class A { + constructor(v0: long, ...args: String[]) {}; + } + )"); + CheckRestParameterFlag("A.:i64;std.core.String[];void;"); +} + +// === Method of interface === +TEST_F(RestParameterTest, interface_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + interface A { + fn() {}; + } + )"); + CheckNoRestParameterFlag("A.fn:void;"); +} + +TEST_F(RestParameterTest, interface_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + interface A { + fn(args: String[]) {}; + } + )"); + CheckNoRestParameterFlag("A.fn:std.core.String[];void;"); +} + +TEST_F(RestParameterTest, interface_with_rest_parameters_0) +{ + SetCurrentProgram(R"( + interface A { + fn(...args: Object[]) {}; + } + )"); + CheckRestParameterFlag("A.fn:std.core.Object[];void;"); +} + +TEST_F(RestParameterTest, interface_with_rest_parameters_1) +{ + SetCurrentProgram(R"( + interface A { + fn(o: Object, ...args: String[]) {}; + } + )"); + CheckRestParameterFlag("A.fn:std.core.Object;std.core.String[];void;"); +} + +// === Lambda method === +TEST_F(RestParameterTest, lambda_without_rest_parameters_0) +{ + SetCurrentProgram(R"( + let fn: ()=>int = (): int => { + return 1; + } + )"); + CheckNoRestParameterFlag("LambdaObject-ETSGLOBAL-lambda$invoke$0-i32-0.invoke:i32;"); +} + +TEST_F(RestParameterTest, lambda_without_rest_parameters_1) +{ + SetCurrentProgram(R"( + let fn: (args: long[])=>int = (args: long[]): int => { + return 1; + } + )"); + CheckNoRestParameterFlag("LambdaObject-ETSGLOBAL-lambda$invoke$0-i64[]-i32-0.invoke:i64[];i32;"); +} + +// NOTE(aleksisch): lambda with rest broken, throws CTE +// TEST_F(RestParameterTest, lambda_with_rest_parameters_0) +//{ +// SetCurrentProgram(R"( +// let fn = (...args: long[]): int => { +// return 1; +// } +// )"); +// CheckRestParameterFlag("FunctionalInterface-i64[]-i32-0.invoke:i64[];i32;"); +//} + +// TEST_F(RestParameterTest, lambda_with_rest_parameters_1) +//{ +// SetCurrentProgram(R"( +// class A { +// fn = (...args: long[]): int => { +// return 1; +// } +// } +// +// let fn = (o: Object, ...args: long[]): int => { +// return 1; +// } +// )"); +// CheckRestParameterFlag("FunctionalInterface-i64[]-i32-0.invoke:i64[];i32;"); +// CheckRestParameterFlag("FunctionalInterface-std-core-Object-i64[]-i32-0.invoke:std.core.Object;i64[];i32;"); +// } + +// === Abstract method of abstract class === +TEST_F(RestParameterTest, abstract_function_without_rest_parameter_0) +{ + SetCurrentProgram(R"( + abstract class A { + abstract fn(): void + } + )"); + CheckNoRestParameterFlag("A.fn:void;"); +} + +TEST_F(RestParameterTest, abstract_function_without_rest_parameter_1) +{ + SetCurrentProgram(R"( + abstract class A { + abstract fn(args: String[]): void + } + )"); + CheckNoRestParameterFlag("A.fn:std.core.String[];void;"); +} + +TEST_F(RestParameterTest, abstract_function_with_rest_parameter_0) +{ + SetCurrentProgram(R"( + abstract class A { + abstract fn(...args: String[]): void + } + )"); + CheckRestParameterFlag("A.fn:std.core.String[];void;"); +} + +TEST_F(RestParameterTest, abstract_function_with_rest_parameter_1) +{ + SetCurrentProgram(R"( + abstract class A { + abstract fn(v: int, ...args: String[]): void + } + )"); + CheckRestParameterFlag("A.fn:i32;std.core.String[];void;"); +} + +// === External methods === +TEST_F(RestParameterTest, external_function_with_rest_parameter_0) +{ + SetCurrentProgram(""); + CheckRestParameterFlag("std.core.LambdaValue.invoke:std.core.Object[];std.core.Object;"); +} + +TEST_F(RestParameterTest, external_function_with_rest_parameter_1) +{ + SetCurrentProgram(""); + CheckRestParameterFlag("escompat.Math.max:f64[];f64;"); +} + +} // namespace ark::es2panda::compiler::test -- Gitee From f58598081ddb800382ccde33acdc15c60ba822dd Mon Sep 17 00:00:00 2001 From: churkinaleksey Date: Thu, 4 Apr 2024 13:37:29 +0300 Subject: [PATCH 3/3] fix namespace alias import for dynamic Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I9EYR5 Signed-off-by: churkinaleksey --- ets2panda/checker/ets/helpers.cpp | 2 +- ets2panda/checker/ets/validateHelpers.cpp | 5 + .../lowering/scopesInit/scopesInitPhase.cpp | 10 + .../lowering/scopesInit/scopesInitPhase.h | 2 +- ets2panda/ir/ets/etsImportDeclaration.h | 4 +- ets2panda/ir/module/importDeclaration.cpp | 1 + ets2panda/ir/module/importDeclaration.h | 4 +- ets2panda/parser/ETSparser.cpp | 2 +- ets2panda/test/CMakeLists.txt | 83 +---- .../import_all_alias_neg-expected.txt | 349 ++++++++++++++++++ .../ets/import_tests/import_all_alias_neg.ets | 18 + .../test/unit/dynamic/dynamic_call_test.cpp | 13 +- .../test/unit/rest_parameter_flag_test.cpp | 32 +- ets2panda/varbinder/ETSBinder.cpp | 33 +- 14 files changed, 418 insertions(+), 140 deletions(-) create mode 100644 ets2panda/test/parser/ets/import_tests/import_all_alias_neg-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_all_alias_neg.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index a5ee1b3bb6..73642b7ce9 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -253,13 +253,13 @@ checker::Type *ETSChecker::ResolveIdentifier(ir::Identifier *const ident) resolved = FindVariableInGlobal(ident); } + ident->SetVariable(resolved); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) ValidateResolvedIdentifier(ident, resolved); ValidatePropertyAccess(resolved, Context().ContainingClass(), ident->Start()); SaveCapturedVariable(resolved, ident); - ident->SetVariable(resolved); return GetTypeOfVariable(resolved); } diff --git a/ets2panda/checker/ets/validateHelpers.cpp b/ets2panda/checker/ets/validateHelpers.cpp index 93a4ed518b..f78c1f608b 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -88,6 +88,11 @@ void ETSChecker::ValidateCallExpressionIdentifier(ir::Identifier *const ident, T if (ident->Parent()->AsCallExpression()->Callee() != ident) { return; } + if (ident->Variable() != nullptr && // It should always be true! + ident->Variable()->Declaration()->Node() != nullptr && + ident->Variable()->Declaration()->Node()->IsImportNamespaceSpecifier()) { + ThrowTypeError({"Namespace style identifier ", ident->Name(), " is not callable."}, ident->Start()); + } if (type->IsETSFunctionType() || type->IsETSDynamicType() || (type->IsETSObjectType() && type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL))) { return; diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp index be8ee46864..66028b32b4 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp @@ -740,6 +740,16 @@ void InitScopesPhaseETS::VisitImportNamespaceSpecifier(ir::ImportNamespaceSpecif Iterate(importSpec); } +void InitScopesPhaseETS::VisitImportSpecifier(ir::ImportSpecifier *importSpec) +{ + if (importSpec->Parent()->AsETSImportDeclaration()->IsPureDynamic()) { + auto [decl, var] = + VarBinder()->NewVarDecl(importSpec->Start(), importSpec->Local()->Name(), importSpec); + var->AddFlag(varbinder::VariableFlags::INITIALIZED); + } + Iterate(importSpec); +} + // Auxiliary method to avoid extra nested levels and too large function size void AddOverload(ir::MethodDefinition *overload, varbinder::Variable *variable) noexcept { diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h index 29bed5afae..77dc1c45d6 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.h @@ -337,7 +337,7 @@ private: void VisitClassStaticBlock(ir::ClassStaticBlock *staticBlock) override; void VisitBlockExpression(ir::BlockExpression *blockExpr) override; void VisitImportNamespaceSpecifier(ir::ImportNamespaceSpecifier *importSpec) override; - void VisitImportSpecifier([[maybe_unused]] ir::ImportSpecifier *importSpec) override {}; + void VisitImportSpecifier([[maybe_unused]] ir::ImportSpecifier *importSpec) override; void VisitImportDefaultSpecifier([[maybe_unused]] ir::ImportDefaultSpecifier *importSpec) override {}; void VisitETSReExportDeclaration(ir::ETSReExportDeclaration *reExport) override; void VisitETSParameterExpression(ir::ETSParameterExpression *paramExpr) override; diff --git a/ets2panda/ir/ets/etsImportDeclaration.h b/ets2panda/ir/ets/etsImportDeclaration.h index b46d39d893..f316a22b46 100644 --- a/ets2panda/ir/ets/etsImportDeclaration.h +++ b/ets2panda/ir/ets/etsImportDeclaration.h @@ -26,8 +26,8 @@ class StringLiteral; class ETSImportDeclaration : public ImportDeclaration { public: - explicit ETSImportDeclaration(ImportSource *source, const ArenaVector &specifiers) - : ImportDeclaration(source->Source(), specifiers), source_(source) + explicit ETSImportDeclaration(ImportSource *source, ArenaVector &&specifiers) + : ImportDeclaration(source->Source(), std::move(specifiers)), source_(source) { SetType(AstNodeType::ETS_IMPORT_DECLARATION); } diff --git a/ets2panda/ir/module/importDeclaration.cpp b/ets2panda/ir/module/importDeclaration.cpp index aa25c7a4f7..f2c577a806 100644 --- a/ets2panda/ir/module/importDeclaration.cpp +++ b/ets2panda/ir/module/importDeclaration.cpp @@ -22,6 +22,7 @@ #include "ir/srcDump.h" namespace ark::es2panda::ir { + void ImportDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) { if (auto *transformedNode = cb(source_); source_ != transformedNode) { diff --git a/ets2panda/ir/module/importDeclaration.h b/ets2panda/ir/module/importDeclaration.h index c9096366dc..13b6bde669 100644 --- a/ets2panda/ir/module/importDeclaration.h +++ b/ets2panda/ir/module/importDeclaration.h @@ -23,8 +23,8 @@ class StringLiteral; class ImportDeclaration : public Statement { public: - explicit ImportDeclaration(StringLiteral *source, ArenaVector const &specifiers) - : Statement(AstNodeType::IMPORT_DECLARATION), source_(source), specifiers_(specifiers) + explicit ImportDeclaration(StringLiteral *source, ArenaVector &&specifiers) + : Statement(AstNodeType::IMPORT_DECLARATION), source_(source), specifiers_(std::move(specifiers)) { } diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 9afd321c59..b637c2c65e 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -2332,7 +2332,7 @@ ir::Statement *ETSParser::ParseExport(lexer::SourcePosition startLoc, ir::Modifi ir::ImportSource *reExportSource = ParseSourceFromClause(true); lexer::SourcePosition endLoc = reExportSource->Source()->End(); - auto *reExportDeclaration = AllocNode(reExportSource, specifiers); + auto *reExportDeclaration = AllocNode(reExportSource, std::move(specifiers)); reExportDeclaration->SetRange({startLoc, endLoc}); ConsumeSemicolon(reExportDeclaration); diff --git a/ets2panda/test/CMakeLists.txt b/ets2panda/test/CMakeLists.txt index d82d4a9aa6..83235a3e2e 100644 --- a/ets2panda/test/CMakeLists.txt +++ b/ets2panda/test/CMakeLists.txt @@ -46,12 +46,12 @@ function(ets2panda_add_gtest TARGET) NAME ${TARGET} SOURCES ${ARG_CPP_SOURCES} LIBRARIES - es2panda-public es2panda-lib + es2panda-public es2panda-lib arkassembler arkbytecodeopt INCLUDE_DIRS - ${ES2PANDA_PATH} - ${ES2PANDA_BINARY_ROOT} + ${ES2PANDA_PATH} + ${ES2PANDA_BINARY_ROOT} SANITIZERS - ${PANDA_SANITIZERS_LIST} + ${PANDA_SANITIZERS_LIST} ) endfunction(ets2panda_add_gtest) @@ -92,75 +92,12 @@ if(PANDA_REGRESSION_TESTS) add_dependencies(es2panda-plugin-test es2panda e2p_test_plugin) add_dependencies(es2panda_tests es2panda-plugin-test) - ets2panda_add_gtest(es2panda_dynamic_call_test - CPP_SOURCES unit/dynamic/dynamic_call_test.cpp - ) - - panda_add_gtest( - NAME es2panda_astverifier_tests - SOURCES - unit/public/ast_verifier_test.cpp - LIBRARIES - es2panda-public es2panda-lib - INCLUDE_DIRS - ${ES2PANDA_PATH} - ${ES2PANDA_BINARY_ROOT} - SANITIZERS - ${PANDA_SANITIZERS_LIST} - ) - - if(PANDA_WITH_ETS) - panda_add_gtest( - NAME scopes_initialization_test - SOURCES - unit/lowerings/scopes_initialization.cpp - LIBRARIES - es2panda-lib es2panda-public arkassembler arkbytecodeopt - INCLUDE_DIRS - ${ES2PANDA_PATH} - ${OUTPUT_DIR} - - SANITIZERS - ${PANDA_SANITIZERS_LIST} - ) - panda_add_gtest( - NAME es2panda_checker_tests - SOURCES - unit/checker_test.cpp - LIBRARIES - es2panda-public es2panda-lib - INCLUDE_DIRS - ${ES2PANDA_ROOT} - SANITIZERS - ${PANDA_SANITIZERS_LIST} - ) - - endif() - - panda_add_gtest( - NAME es2panda_astdumper_tests - SOURCES - unit/ast_dumper_test.cpp - LIBRARIES - es2panda-public es2panda-lib arkassembler arkbytecodeopt - INCLUDE_DIRS - ${ES2PANDA_PATH} - SANITIZERS - ${PANDA_SANITIZERS_LIST} - ) - - panda_add_gtest( - NAME es2panda_union_normalization_tests - SOURCES - unit/union_normalization_test.cpp - LIBRARIES - es2panda-public es2panda-lib arkassembler arkbytecodeopt - INCLUDE_DIRS - ${ES2PANDA_PATH} - ${ES2PANDA_BINARY_ROOT} - SANITIZERS - ${PANDA_SANITIZERS_LIST} - ) + ets2panda_add_gtest(es2panda_dynamic_call_test CPP_SOURCES unit/dynamic/dynamic_call_test.cpp) + ets2panda_add_gtest(es2panda_astverifier_tests CPP_SOURCES unit/public/ast_verifier_test.cpp) + ets2panda_add_gtest(scopes_initialization_test CPP_SOURCES unit/lowerings/scopes_initialization.cpp) + ets2panda_add_gtest(es2panda_checker_tests CPP_SOURCES unit/checker_test.cpp) + ets2panda_add_gtest(es2panda_astdumper_tests CPP_SOURCES unit/ast_dumper_test.cpp) + ets2panda_add_gtest(es2panda_union_normalization_tests CPP_SOURCES unit/union_normalization_test.cpp) # NOTE: es2panda_rest_parameter_flag test runs a lot of time on qemu, so let's disable it if (NOT PANDA_QEMU_BUILD) diff --git a/ets2panda/test/parser/ets/import_tests/import_all_alias_neg-expected.txt b/ets2panda/test/parser/ets/import_tests/import_all_alias_neg-expected.txt new file mode 100644 index 0000000000..feb58d3f2f --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_all_alias_neg-expected.txt @@ -0,0 +1,349 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages", + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 16, + "column": 46 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "Test", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "Test", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 27 + } + } +} +TypeError: Namespace style identifier Test is not callable. [import_all_alias_neg.ets:18:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_all_alias_neg.ets b/ets2panda/test/parser/ets/import_tests/import_all_alias_neg.ets new file mode 100644 index 0000000000..d5aa69dbb2 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_all_alias_neg.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022-2024 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. + */ + +import * as Test from "import_tests/packages"; + +function main() { Test() } \ No newline at end of file diff --git a/ets2panda/test/unit/dynamic/dynamic_call_test.cpp b/ets2panda/test/unit/dynamic/dynamic_call_test.cpp index ad7a3fa7fd..bc293db96b 100644 --- a/ets2panda/test/unit/dynamic/dynamic_call_test.cpp +++ b/ets2panda/test/unit/dynamic/dynamic_call_test.cpp @@ -23,6 +23,8 @@ #include "ir/ets/etsTypeReferencePart.h" #include "ir/ets/etsTypeReference.h" #include "ir/ts/tsQualifiedName.h" +#include "util/helpers.h" +#include "compiler/lowering/scopesInit/scopesInitPhase.h" #include "util/language.h" #include "parser/ETSparser.h" @@ -77,13 +79,16 @@ public: void AddDynImport(const char *specifierName, varbinder::ETSBinder *varbinder, ir::Identifier *node) { auto aIdent = Allocator()->New(specifierName, Allocator()); + ArenaVector specifiers {Allocator()->Adapter()}; auto specifier = Allocator()->New(aIdent, aIdent); - auto importSrc = Allocator()->New(Allocator()->New(), + specifiers.emplace_back(specifier); + auto importSrc = Allocator()->New(Allocator()->New("/tmp"), Allocator()->New(), - Language::FromString("ets").value(), false); + Language::FromString("js").value(), false); auto importDecl = - Allocator()->New(importSrc, ArenaVector {Allocator()->Adapter()}); - varbinder->AddDynamicSpecifiersToTopBindings(specifier, importDecl); + util::NodeAllocator::Alloc(Allocator(), importSrc, std::move(specifiers)); + compiler::InitScopesPhaseETS::RunExternalNode(importDecl, varbinder); + varbinder->BuildImportDeclaration(importDecl); auto var = varbinder->TopScope()->Find(specifierName); node->SetVariable(var.variable); } diff --git a/ets2panda/test/unit/rest_parameter_flag_test.cpp b/ets2panda/test/unit/rest_parameter_flag_test.cpp index c78f7f6e86..62b3f40159 100644 --- a/ets2panda/test/unit/rest_parameter_flag_test.cpp +++ b/ets2panda/test/unit/rest_parameter_flag_test.cpp @@ -29,9 +29,9 @@ class RestParameterTest : public testing::Test { public: RestParameterTest() { - constexpr auto COMPILER_SIZE = 268435456; + const auto compilerSize = 268435456; - mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0); + mem::MemConfig::Initialize(0, 0, compilerSize, 0, 0, 0); PoolManager::Initialize(PoolType::MMAP); } ~RestParameterTest() override @@ -326,34 +326,6 @@ TEST_F(RestParameterTest, lambda_without_rest_parameters_1) CheckNoRestParameterFlag("LambdaObject-ETSGLOBAL-lambda$invoke$0-i64[]-i32-0.invoke:i64[];i32;"); } -// NOTE(aleksisch): lambda with rest broken, throws CTE -// TEST_F(RestParameterTest, lambda_with_rest_parameters_0) -//{ -// SetCurrentProgram(R"( -// let fn = (...args: long[]): int => { -// return 1; -// } -// )"); -// CheckRestParameterFlag("FunctionalInterface-i64[]-i32-0.invoke:i64[];i32;"); -//} - -// TEST_F(RestParameterTest, lambda_with_rest_parameters_1) -//{ -// SetCurrentProgram(R"( -// class A { -// fn = (...args: long[]): int => { -// return 1; -// } -// } -// -// let fn = (o: Object, ...args: long[]): int => { -// return 1; -// } -// )"); -// CheckRestParameterFlag("FunctionalInterface-i64[]-i32-0.invoke:i64[];i32;"); -// CheckRestParameterFlag("FunctionalInterface-std-core-Object-i64[]-i32-0.invoke:std.core.Object;i64[];i32;"); -// } - // === Abstract method of abstract class === TEST_F(RestParameterTest, abstract_function_without_rest_parameter_0) { diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 8fcb688088..2693683162 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -438,13 +438,9 @@ void ETSBinder::AddDynamicSpecifiersToTopBindings(ir::AstNode *const specifier, return specifier->AsImportSpecifier()->Local()->Name(); }(); - auto *const decl = Allocator()->New(name, specifier); - auto *const var = Allocator()->New(decl, varbinder::VariableFlags::STATIC); - var->AddFlag(VariableFlags::INITIALIZED); - - dynamicImportVars_.emplace(var, DynamicImportData {import, specifier, var}); - - TopScope()->InsertDynamicBinding(name, var); + ASSERT(GetScope()->Find(name, ResolveBindingOptions::DECLARATION).variable != nullptr); + auto specDecl = GetScope()->Find(name, ResolveBindingOptions::DECLARATION); + dynamicImportVars_.emplace(specDecl.variable, DynamicImportData {import, specifier, specDecl.variable}); } void ETSBinder::InsertForeignBinding(ir::AstNode *const specifier, const ir::ETSImportDeclaration *const import, @@ -512,7 +508,7 @@ bool ETSBinder::AddImportNamespaceSpecifiersToTopBindings(ir::AstNode *const spe } for (auto it : item->GetETSImportDeclarations()->Specifiers()) { - if (it->IsImportNamespaceSpecifier() && !specifier->AsImportNamespaceSpecifier()->Local()->Name().Empty()) { + if (it->IsImportNamespaceSpecifier() && !namespaceSpecifier->Local()->Name().Empty()) { continue; } @@ -604,13 +600,6 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(ir::AstNode *const specifier, return false; } const ir::StringLiteral *const importPath = import->Source(); - auto insertForeignBinding = [this, specifier, import](const util::StringView &name, Variable *var) { - if (import->Language().IsDynamic()) { - dynamicImportVars_.emplace(var, DynamicImportData {import, specifier, var}); - } - - TopScope()->InsertForeignBinding(name, var); - }; const auto *const importSpecifier = specifier->AsImportSpecifier(); @@ -664,7 +653,7 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(ir::AstNode *const specifier, ThrowError(importPath->Start(), "Imported element not exported '" + var->Declaration()->Name().Mutf8() + "'"); } - insertForeignBinding(localName, var); + InsertForeignBinding(specifier, import, localName, var); return true; } @@ -716,14 +705,6 @@ void ETSBinder::AddSpecifiersToTopBindings(ir::AstNode *const specifier, const i const auto *const importGlobalScope = importProgram->GlobalScope(); const auto &globalBindings = importGlobalScope->Bindings(); - auto insertForeignBinding = [this, specifier, import](const util::StringView &name, Variable *var) { - if (import->Language().IsDynamic()) { - dynamicImportVars_.emplace(var, DynamicImportData {import, specifier, var}); - } - - TopScope()->InsertForeignBinding(name, var); - }; - if (AddImportNamespaceSpecifiersToTopBindings(specifier, globalBindings, importProgram, importGlobalScope, import)) { return; @@ -738,12 +719,12 @@ void ETSBinder::AddSpecifiersToTopBindings(ir::AstNode *const specifier, const i auto item = std::find_if(globalBindings.begin(), globalBindings.end(), predicateFunc); if (item == globalBindings.end()) { - insertForeignBinding(specifier->AsImportDefaultSpecifier()->Local()->Name(), + InsertForeignBinding(specifier, import, specifier->AsImportDefaultSpecifier()->Local()->Name(), FindStaticBinding(record, importPath)); return; } - insertForeignBinding(specifier->AsImportDefaultSpecifier()->Local()->Name(), item->second); + InsertForeignBinding(specifier, import, specifier->AsImportDefaultSpecifier()->Local()->Name(), item->second); } void ETSBinder::HandleCustomNodes(ir::AstNode *childNode) -- Gitee