From 999d252eb2f95aaa7ce786af81e9786549175fa4 Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Tue, 16 Jan 2024 12:06:28 +0100 Subject: [PATCH 01/21] [ArkTS frontend] overriding methods with optional parameters Overriding a method with optional parameter causes error. Fixes internal issue #15203 Change-Id: If45a44317c73c535b22505f5d87708b00b27470e Signed-off-by: Vivien Voros --- ets2panda/checker/ets/function.cpp | 2 +- .../test/parser/ets/override-expected.txt | 2543 +++++++++++++++++ ets2panda/test/parser/ets/override.ets | 25 + ets2panda/test/runtime/ets/Override-3.ets | 42 + 4 files changed, 2611 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/parser/ets/override-expected.txt create mode 100644 ets2panda/test/parser/ets/override.ets create mode 100644 ets2panda/test/runtime/ets/Override-3.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 5c9d2164773..e6f781ed573 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1255,7 +1255,7 @@ void ETSChecker::CheckOverride(Signature *signature) iter = iter->SuperType(); } - if (!isOverriding && signature->Function()->IsOverride()) { + if (!isOverriding && signature->Function()->IsOverride() && !(signature->Function()->IsDefaultParamProxy())) { ThrowTypeError({"Method ", signature->Function()->Id()->Name(), signature, " in ", signature->Owner(), " not overriding any method"}, signature->Function()->Start()); diff --git a/ets2panda/test/parser/ets/override-expected.txt b/ets2panda/test/parser/ets/override-expected.txt new file mode 100644 index 00000000000..6fb7ad94956 --- /dev/null +++ b/ets2panda/test/parser/ets/override-expected.txt @@ -0,0 +1,2543 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "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": 3 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "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 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + { + "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": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "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": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 29 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 35 + }, + "end": { + "line": 20, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + { + "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": 21, + "column": 2 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "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": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 24, + "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": 26, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/override.ets b/ets2panda/test/parser/ets/override.ets new file mode 100644 index 00000000000..90460dc7007 --- /dev/null +++ b/ets2panda/test/parser/ets/override.ets @@ -0,0 +1,25 @@ +/* + * 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 B { + foo(a: T, op?: T): T { return a } +} +class A extends B { + override foo(a: T, op?: T): T { return a } +} +function main(){ + +} + diff --git a/ets2panda/test/runtime/ets/Override-3.ets b/ets2panda/test/runtime/ets/Override-3.ets new file mode 100644 index 00000000000..f3294dc0664 --- /dev/null +++ b/ets2panda/test/runtime/ets/Override-3.ets @@ -0,0 +1,42 @@ +/* + * 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 B { + foo(a: int, op?: int): int { return a } +} +class A extends B { + override foo(a: int, op?: int): int { return a } +} + +class C { + foo(a: T, op?: T): T { return a } +} +class D extends C { + override foo(a: T, op?: T): T { return a } +} + +function main(){ + let a = new A(); + let b = new B(); + let c = new C(); + let d = new D(); + + assert(b.foo(5) == a.foo(5)); + assert(b.foo(6) != a.foo(5)); + assert(b.foo(5) != a.foo(6)); + assert(c.foo("x") == d.foo("x")); + assert(c.foo("x") != d.foo("y")); + assert(c.foo("y") != d.foo("x")); +} -- Gitee From 552e504e654bf6a0b89084cb828c4ed7957fb3b3 Mon Sep 17 00:00:00 2001 From: Anna Antipina Date: Thu, 11 Jan 2024 18:12:52 +0300 Subject: [PATCH 02/21] Title: Implement string constructor for NullishType Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8VHWY Description: enable the ability to create 'new String(NullishType)' Test: ${ARK_SOURCE_DIR}/tests/tests-u-runner/runner.sh ${ARK_SOURCE_DIR} --ets-runtime --build-dir="${ARK_BUILD_DIR}" --heap-verifier="fail_on_verification:pre:into:before_g1_concurrent:post" --timeout=30 --force-generate --test-file string_from_nullish.ets Signed-off-by: Anna Antipina --- ets2panda/compiler/core/ETSGen.h | 56 ++++++++++++++++++- ets2panda/compiler/scripts/signatures.yaml | 12 ++++ .../test/runtime/ets/string_from_nullish.ets | 38 +++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/string_from_nullish.ets diff --git a/ets2panda/compiler/core/ETSGen.h b/ets2panda/compiler/core/ETSGen.h index 456d8c01f51..b1c59073517 100644 --- a/ets2panda/compiler/core/ETSGen.h +++ b/ets2panda/compiler/core/ETSGen.h @@ -983,13 +983,67 @@ private: } } + template + bool ResolveStringFromNullishBuiltin(const ir::AstNode *node, checker::Signature *signature, + const ArenaVector &arguments) + { + if (signature->InternalName() != Signatures::BUILTIN_STRING_FROM_NULLISH_CTOR) { + return false; + } + auto argExpr = arguments[0]; + if (argExpr->IsExpression()) { + if (argExpr->AsExpression()->IsNullLiteral()) { + LoadAccumulatorString(node, "null"); + return true; + } + if (argExpr->AsExpression()->IsUndefinedLiteral()) { + LoadAccumulatorString(node, "undefined"); + return true; + } + } + + Label *isNull = AllocLabel(); + Label *end = AllocLabel(); +#ifdef PANDA_WITH_ETS + Label *isUndefined = AllocLabel(); +#endif + COMPILE_ARG(0); + LoadAccumulator(node, arg0); + if (argExpr->TsType()->IsNullish()) { + BranchIfNull(node, isNull); +#ifdef PANDA_WITH_ETS + Sa().Emit(node); + BranchIfTrue(node, isUndefined); +#endif + } + LoadAccumulator(node, arg0); + CastToString(node); + StoreAccumulator(node, arg0); + Ra().Emit(node, Signatures::BUILTIN_STRING_FROM_STRING_CTOR, arg0, dummyReg_); + JumpTo(node, end); + if (argExpr->TsType()->IsNullish()) { + SetLabel(node, isNull); + LoadAccumulatorString(node, "null"); +#ifdef PANDA_WITH_ETS + JumpTo(node, end); + SetLabel(node, isUndefined); + LoadAccumulatorString(node, "undefined"); +#endif + } + SetLabel(node, end); + return true; + } + template void CallImpl(const ir::AstNode *node, checker::Signature *signature, const ArenaVector &arguments) { RegScope rs(this); - const auto name = signature->InternalName(); + if (ResolveStringFromNullishBuiltin(node, signature, arguments)) { + return; + } + const auto name = signature->InternalName(); switch (arguments.size()) { case 0U: { Ra().Emit(node, name, dummyReg_, dummyReg_); diff --git a/ets2panda/compiler/scripts/signatures.yaml b/ets2panda/compiler/scripts/signatures.yaml index 37f0738757c..c0797950622 100644 --- a/ets2panda/compiler/scripts/signatures.yaml +++ b/ets2panda/compiler/scripts/signatures.yaml @@ -304,6 +304,18 @@ signatures: return_type: PRIMITIVE_VOID ref: BUILTIN_OBJECT_CTOR + - callee: BUILTIN_STRING + method_name: $CTOR + params: [BUILTIN_STRING] + return_type: PRIMITIVE_VOID + ref: BUILTIN_STRING_FROM_STRING_CTOR + + - callee: BUILTIN_STRING + method_name: $CTOR + params: [BUILTIN_OBJECT] + return_type: PRIMITIVE_VOID + ref: BUILTIN_STRING_FROM_NULLISH_CTOR + - callee: BUILTIN_OBJECT method_name: toString params: [] diff --git a/ets2panda/test/runtime/ets/string_from_nullish.ets b/ets2panda/test/runtime/ets/string_from_nullish.ets new file mode 100644 index 00000000000..b2aae3aa507 --- /dev/null +++ b/ets2panda/test/runtime/ets/string_from_nullish.ets @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-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. + */ + +function main(): int { + let z: NullishType = new Number(1); + let z1 = undefined; + let z2 = null; + let z3: Object = new String("0"); + let z4: Object = new Number(1); + let x = new String(z) + let x1 = new String(z1) + let x2 = new String(z2) + let x3 = new String(z3) + let x4 = new String(z4) + let x5 = new String(null) + let x6 = new String(undefined) + let actual: String[] = [new String(z), new String(z1), new String(z2), new String(z3), new String(z4), new String(null), new String(undefined)] + let expected: String[] = ["1", "undefined", "null", "0", "1", "null", "undefined"] + for (let i = 0; i < expected.length; i++) { + if (actual[i] != expected[i]) { + console.log("Failed: actual is \"" + actual[i] + "\" expected is \"" + expected[i] + "\"") + return 1; + } + } + return 0; +} -- Gitee From f4d0e3844776fd363b4e9b457df772fcc34162e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20Horv=C3=A1th?= Date: Wed, 17 Jan 2024 16:22:37 +0100 Subject: [PATCH 03/21] Fix the instantiation of tuple types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes internal issue #15207 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8XAME Testing: All required pre-merge tests passed. Results are available in the ggwatcher. Signed-off-by: Ferenc Horváth Change-Id: I07666818f493ea8f61f75b04bb95cdbc47a68e32 --- ets2panda/checker/types/ets/etsTupleType.cpp | 3 +- .../compiler/ets/nullableTuple-expected.txt | 797 ++++++++++++++++++ ets2panda/test/compiler/ets/nullableTuple.ets | 24 + 3 files changed, 823 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/nullableTuple-expected.txt create mode 100644 ets2panda/test/compiler/ets/nullableTuple.ets diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index 99afd7bf641..aaaf104a049 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -185,7 +185,8 @@ void ETSTupleType::Cast(TypeRelation *const relation, Type *const target) Type *ETSTupleType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation, [[maybe_unused]] GlobalTypesHolder *globalTypes) { - return this; + return allocator->New(GetTupleTypesList(), + ElementType()->Instantiate(allocator, relation, globalTypes), GetSpreadType()); } } // namespace panda::es2panda::checker diff --git a/ets2panda/test/compiler/ets/nullableTuple-expected.txt b/ets2panda/test/compiler/ets/nullableTuple-expected.txt new file mode 100644 index 00000000000..6cd2174f4a8 --- /dev/null +++ b/ets2panda/test/compiler/ets/nullableTuple-expected.txt @@ -0,0 +1,797 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 42 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 16, + "column": 26 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "TTupleUnion", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + { + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "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": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 12, + "loc": { + "start": { + "line": 20, + "column": 32 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + { + "type": "StringLiteral", + "value": "Hello World", + "loc": { + "start": { + "line": 20, + "column": 38 + }, + "end": { + "line": 20, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 4 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TTupleUnion", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "init": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 4 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 12 + } + } + }, + "property": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "computed": true, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 12, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "second": null, + "loc": { + "start": { + "line": 23, + "column": 4 + }, + "end": { + "line": 23, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/nullableTuple.ets b/ets2panda/test/compiler/ets/nullableTuple.ets new file mode 100644 index 00000000000..7891b82c56a --- /dev/null +++ b/ets2panda/test/compiler/ets/nullableTuple.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +type TNumberStringPair = [number, string]; +type TTupleUnion = TNumberStringPair|undefined; + +function main(): void { + let x: TNumberStringPair = [12.0, "Hello World"]; + let y: TTupleUnion = undefined; + + assert x[0] == 12.0; +} -- Gitee From 83417a1e83850f3d527d4b82adb51466648c6972 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 5 Jan 2024 15:53:39 +0100 Subject: [PATCH 04/21] Get enum value, when used with '&' operator in binary expression Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8UVCM Internal issue: #15017 Test: build, new runtime test Signed-off-by: Martin Sajti --- ets2panda/checker/ets/arithmetic.cpp | 13 ++++++++ ets2panda/compiler/core/ETSGen.cpp | 7 ++++ ets2panda/ir/astNode.h | 2 +- ets2panda/ir/astNodeFlags.h | 1 + ets2panda/test/runtime/ets/Enum6.ets | 50 ++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/Enum6.ets diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index 66b26f35397..46b8207153e 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -291,6 +291,19 @@ checker::Type *ETSChecker::CheckBinaryOperatorBitwise(ir::Expression *left, ir:: bool isEqualOp, checker::Type *const leftType, checker::Type *const rightType, Type *unboxedL, Type *unboxedR) { + // NOTE (mmartin): These need to be done for other binary expressions, but currently it's not defined precisely when + // to apply this conversion + + if (leftType->IsETSEnumType()) { + left->AddAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + unboxedL = GlobalIntType(); + } + + if (rightType->IsETSEnumType()) { + right->AddAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + unboxedR = GlobalIntType(); + } + if (leftType->IsETSUnionType() || rightType->IsETSUnionType()) { ThrowTypeError("Bad operand type, unions are not allowed in binary expressions except equality.", pos); } diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 1949eabc510..e222381e663 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -982,6 +982,13 @@ void ETSGen::ApplyConversion(const ir::AstNode *node, const checker::Type *targe { auto ttctx = TargetTypeContext(this, targetType); + if (node->HasAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE)) { + Ra().Emit( + node, node->AsExpression()->TsType()->AsETSEnumType()->GetValueMethod().globalSignature->InternalName(), + dummyReg_, 0); + node->RemoveAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + } + if ((node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U) { ApplyBoxingConversion(node); return; diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index f6eedfb88e7..4b20f93db34 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -450,7 +450,7 @@ public: { \ return ((member_name)&flag) != 0U; \ } \ - void Remove##flag_type(flag_type flag) noexcept \ + void Remove##flag_type(flag_type flag) const noexcept \ { \ (member_name) &= ~flag; \ } diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index f6420bb1311..fb74be021bc 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -22,6 +22,7 @@ namespace panda::es2panda::ir { enum class AstNodeFlags { NO_OPTS = 0, CHECKCAST = 1U << 0U, + ENUM_GET_VALUE = 1U << 1U, }; enum class ModifierFlags : uint32_t { diff --git a/ets2panda/test/runtime/ets/Enum6.ets b/ets2panda/test/runtime/ets/Enum6.ets new file mode 100644 index 00000000000..39b8f0c5dbc --- /dev/null +++ b/ets2panda/test/runtime/ets/Enum6.ets @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 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. + */ + +enum Access { + READ = 1, + WRITE = 2, + READWRITE = 3 +} + +enum TestEnum { + A = 144, + B, + C = 795 +} + +function main(): void { + test_access_enum_bitwise(Access.WRITE); + assert (Access.WRITE & Access.READWRITE) == 2; + + test_test_enum_bitwise(TestEnum.C); + assert (TestEnum.B & TestEnum.A) == 144 + +} + +function test_access_enum_bitwise(enum_val: Access): void { + assert (enum_val & Access.READ) == 0 + assert (enum_val & Access.WRITE) == 2 + assert (enum_val | Access.READ) == 3 + assert (enum_val | Access.READWRITE) == 3 + assert (enum_val & Access.READWRITE) == 2 +} + +function test_test_enum_bitwise(enum_val: TestEnum): void { + assert (enum_val & TestEnum.A) == 16 + assert (enum_val | TestEnum.A) == 923 + assert (enum_val & TestEnum.B) == 17 + assert (enum_val | TestEnum.B) == 923 +} -- Gitee From 1af38e08ae4ba1f8577bfeb95bc39da302226e5e Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Wed, 10 Jan 2024 14:13:19 +0100 Subject: [PATCH 05/21] Tuple instantiation and LUB for type parameters Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8VHL5 Internal issue: #15174, #15184 Test: build, new checker test, new runtime test Signed-off-by: Martin Sajti --- ets2panda/checker/ETSAnalyzer.cpp | 2 +- ets2panda/checker/types/ets/etsObjectType.cpp | 51 +- ets2panda/checker/types/ets/etsObjectType.h | 1 + ets2panda/checker/types/ets/etsTupleType.cpp | 17 + ets2panda/checker/types/ets/etsTupleType.h | 1 + ets2panda/ir/ets/etsTuple.cpp | 84 +- ets2panda/ir/ets/etsTuple.h | 6 +- ets2panda/ir/expressions/memberExpression.cpp | 7 - .../compiler/ets/tuple_types_19-expected.txt | 2271 +++++++++++++++++ .../test/compiler/ets/tuple_types_19.ets | 31 + .../test/runtime/ets/tuple_types_runtime.ets | 11 + 11 files changed, 2410 insertions(+), 72 deletions(-) create mode 100644 ets2panda/test/compiler/ets/tuple_types_19-expected.txt create mode 100644 ets2panda/test/compiler/ets/tuple_types_19.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 653bcf2c2e6..77a8d79564a 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -725,7 +725,7 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const currentElement->Start()); } - const checker::CastingContext cast( + checker::AssignmentContext( checker->Relation(), currentElement, elementType, compareType, currentElement->Start(), {"Array initializer's type is not assignable to tuple type at index: ", idx}); diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 4dfa0dba6fe..17cbff5b6f9 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -604,46 +604,57 @@ void ETSObjectType::Cast(TypeRelation *const relation, Type *const target) conversion::Forbidden(relation); } -void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) +bool ETSObjectType::DefaultObjectTypeChecks(const ETSChecker *const etsChecker, TypeRelation *const relation, + Type *const source) { - relation->Result(false); - auto *const etsChecker = relation->GetChecker()->AsETSChecker(); - - if (source->IsETSUnionType()) { - bool res = std::all_of(source->AsETSUnionType()->ConstituentTypes().begin(), - source->AsETSUnionType()->ConstituentTypes().end(), [this, relation](Type *ct) { - relation->Result(false); - IsSupertypeOf(relation, ct); - return relation->IsTrue(); - }); - relation->Result(res); - return; - } - // 3.8.3 Subtyping among Array Types auto const *const base = GetConstOriginalBaseType(); if (base == etsChecker->GlobalETSObjectType() && source->IsETSArrayType()) { relation->Result(true); - return; + return true; + } + + if (source->IsETSTypeParameter()) { + IsSupertypeOf(relation, source->AsETSTypeParameter()->GetConstraintType()); + return true; } if (!source->IsETSObjectType() || !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE | ETSObjectFlags::NULL_TYPE)) { - return; + return true; } if ((!ContainsNull() && source->ContainsNull()) || (!ContainsUndefined() && source->ContainsUndefined())) { - return; + return true; } // All classes and interfaces are subtypes of Object if (base == etsChecker->GlobalETSObjectType() || base == etsChecker->GlobalETSNullishObjectType()) { relation->Result(true); - return; + return true; } IdenticalUptoNullability(relation, source); - if (relation->IsTrue()) { + return relation->IsTrue(); +} + +void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) +{ + relation->Result(false); + auto *const etsChecker = relation->GetChecker()->AsETSChecker(); + + if (source->IsETSUnionType()) { + bool res = std::all_of(source->AsETSUnionType()->ConstituentTypes().begin(), + source->AsETSUnionType()->ConstituentTypes().end(), [this, relation](Type *ct) { + relation->Result(false); + IsSupertypeOf(relation, ct); + return relation->IsTrue(); + }); + relation->Result(res); + return; + } + + if (DefaultObjectTypeChecks(etsChecker, relation, source)) { return; } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 548bacce30e..464754a587b 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -482,6 +482,7 @@ public: Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; bool CastNumericObject(TypeRelation *relation, Type *target); + bool DefaultObjectTypeChecks(const ETSChecker *etsChecker, TypeRelation *relation, Type *source); void IsSupertypeOf(TypeRelation *relation, Type *source) override; Type *AsSuper(Checker *checker, varbinder::Variable *sourceVar) override; diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index aaaf104a049..08e74f9828b 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -14,7 +14,10 @@ */ #include "etsTupleType.h" + +#include "checker/ETSchecker.h" #include "checker/ets/conversion.h" +#include "ir/ets/etsTuple.h" namespace panda::es2panda::checker { void ETSTupleType::ToString(std::stringstream &ss) const @@ -129,6 +132,20 @@ void ETSTupleType::AssignmentTarget(TypeRelation *const relation, Type *const so relation->Result(true); } +Type *ETSTupleType::Substitute(TypeRelation *relation, const Substitution *substitution) +{ + auto *const checker = relation->GetChecker()->AsETSChecker(); + ArenaVector newTypeList(checker->Allocator()->Adapter()); + + for (auto *const tupleTypeListElement : GetTupleTypesList()) { + newTypeList.emplace_back(tupleTypeListElement->Substitute(relation, substitution)); + } + + auto *newSpreadType = spreadType_ == nullptr ? nullptr : spreadType_->Substitute(relation, substitution); + auto *newElementType = ir::ETSTuple::CalculateLUBForTuple(checker, newTypeList, newSpreadType); + return checker->Allocator()->New(std::move(newTypeList), newElementType, newSpreadType); +} + void ETSTupleType::Cast(TypeRelation *const relation, Type *const target) { // NOTE(mmartin): Might be not the correct casting rules, as these aren't defined yet diff --git a/ets2panda/checker/types/ets/etsTupleType.h b/ets2panda/checker/types/ets/etsTupleType.h index 7e84e5f87aa..249dbe58cfb 100644 --- a/ets2panda/checker/types/ets/etsTupleType.h +++ b/ets2panda/checker/types/ets/etsTupleType.h @@ -85,6 +85,7 @@ public: void Identical(TypeRelation *relation, Type *other) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; bool AssignmentSource(TypeRelation *relation, Type *target) override; + Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; diff --git a/ets2panda/ir/ets/etsTuple.cpp b/ets2panda/ir/ets/etsTuple.cpp index dcb984b54b5..929fce73a10 100644 --- a/ets2panda/ir/ets/etsTuple.cpp +++ b/ets2panda/ir/ets/etsTuple.cpp @@ -15,7 +15,6 @@ #include "etsTuple.h" -#include "checker/ETSchecker.h" #include "checker/types/ets/etsTupleType.h" #include "ir/astDump.h" @@ -79,7 +78,36 @@ checker::Type *ETSTuple::Check([[maybe_unused]] checker::ETSChecker *const check return GetType(checker); } -void ETSTuple::SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *const type) +checker::Type *ETSTuple::GetType(checker::ETSChecker *const checker) +{ + if (TsType() != nullptr) { + return TsType(); + } + + ArenaVector typeList(checker->Allocator()->Adapter()); + + for (auto *const typeAnnotation : GetTupleTypeAnnotationsList()) { + auto *const checkedType = checker->GetTypeFromTypeAnnotation(typeAnnotation); + typeList.emplace_back(checkedType); + } + + if (HasSpreadType()) { + ASSERT(spreadType_->IsTSArrayType()); + auto *const arrayType = spreadType_->GetType(checker); + ASSERT(arrayType->IsETSArrayType()); + spreadType_->SetTsType(arrayType->AsETSArrayType()->ElementType()); + } + + auto *const spreadElementType = spreadType_ != nullptr ? spreadType_->TsType() : nullptr; + + auto *const tupleType = checker->Allocator()->New( + typeList, CalculateLUBForTuple(checker, typeList, spreadElementType), spreadElementType); + + SetTsType(tupleType); + return TsType(); +} + +static void SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *const type) { if (type->HasTypeFlag(checker::TypeFlag::NULLISH)) { containsNullOrUndefined.first = true; @@ -101,7 +129,7 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker bool allElementsAreSame = std::all_of(typeList.begin(), typeList.end(), - [this, &checker, &typeList, &containsNullOrUndefined](checker::Type *const element) { + [&checker, &typeList, &containsNullOrUndefined](checker::Type *const element) { SetNullUndefinedFlags(containsNullOrUndefined, element); return checker->Relation()->IsIdenticalTo(typeList[0], element); }); @@ -113,16 +141,13 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker // If only one type present in the tuple, that will be the holder array type. If any two not identical types // present, primitives will be boxed, and LUB is calculated for all of them. - // That makes it possible to assign eg. `[int, int, ...int[]]` tuple type to `int[]` array type. Because a `short[]` - // array already isn't assignable to `int[]` array, that preserve that the `[int, short, ...int[]]` tuple type's - // element type will be calculated to `Object[]`, which is not assignable to `int[]` array either. + // That makes it possible to assign eg. `[int, int, ...int[]]` tuple type to `int[]` array type. Because a + // `short[]` array already isn't assignable to `int[]` array, that preserve that the `[int, short, ...int[]]` + // tuple type's element type will be calculated to `Object[]`, which is not assignable to `int[]` array either. if (allElementsAreSame) { return typeList[0]; } - auto *const savedRelationNode = checker->Relation()->GetNode(); - checker->Relation()->SetNode(this); - auto getBoxedTypeOrType = [&checker](checker::Type *const type) { auto *const boxedType = checker->PrimitiveTypeAsETSBuiltinType(type); return boxedType == nullptr ? type : boxedType; @@ -131,11 +156,19 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker checker::Type *lubType = getBoxedTypeOrType(typeList[0]); for (std::size_t idx = 1; idx < typeList.size(); ++idx) { + if (typeList[idx]->IsETSTypeParameter()) { + lubType = typeList[idx]->AsETSTypeParameter()->GetConstraintType(); + continue; + } lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(typeList[idx])); } if (spreadType != nullptr) { - lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(spreadType)); + if (spreadType->IsETSTypeParameter()) { + lubType = spreadType->AsETSTypeParameter()->GetConstraintType(); + } else { + lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(spreadType)); + } } const auto nullishUndefinedFlags = @@ -148,38 +181,7 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker checker->GetGlobalTypesHolder()); } - checker->Relation()->SetNode(savedRelationNode); - return lubType; } -checker::Type *ETSTuple::GetType(checker::ETSChecker *const checker) -{ - if (TsType() != nullptr) { - return TsType(); - } - - ArenaVector typeList(checker->Allocator()->Adapter()); - - for (auto *const typeAnnotation : GetTupleTypeAnnotationsList()) { - auto *const checkedType = checker->GetTypeFromTypeAnnotation(typeAnnotation); - typeList.emplace_back(checkedType); - } - - if (HasSpreadType()) { - ASSERT(spreadType_->IsTSArrayType()); - auto *const arrayType = spreadType_->GetType(checker); - ASSERT(arrayType->IsETSArrayType()); - spreadType_->SetTsType(arrayType->AsETSArrayType()->ElementType()); - } - - auto *const spreadElementType = spreadType_ != nullptr ? spreadType_->TsType() : nullptr; - - auto *const tupleType = checker->Allocator()->New( - typeList, CalculateLUBForTuple(checker, typeList, spreadElementType), spreadElementType); - - SetTsType(tupleType); - return TsType(); -} - } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsTuple.h b/ets2panda/ir/ets/etsTuple.h index ab4eff12f4a..43379d92130 100644 --- a/ets2panda/ir/ets/etsTuple.h +++ b/ets2panda/ir/ets/etsTuple.h @@ -16,6 +16,7 @@ #ifndef ES2PANDA_IR_ETS_TUPLE_H #define ES2PANDA_IR_ETS_TUPLE_H +#include "checker/ETSchecker.h" #include "ir/typeNode.h" namespace panda::es2panda::ir { @@ -75,9 +76,8 @@ public: checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; - void SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *type); - checker::Type *CalculateLUBForTuple(checker::ETSChecker *checker, ArenaVector &typeList, - checker::Type *spreadType); + static checker::Type *CalculateLUBForTuple(checker::ETSChecker *checker, ArenaVector &typeList, + checker::Type *spreadType); void Accept(ASTVisitorT *v) override { diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 9f2c98d44fc..c705d084eb3 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -330,14 +330,7 @@ checker::Type *MemberExpression::CheckTupleAccessMethod(checker::ETSChecker *che // NOTE(mmartin): this can be replaced with the general type mapper, once implemented if ((GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG) != 0U) { - auto *const savedNode = checker->Relation()->GetNode(); - if (savedNode == nullptr) { - checker->Relation()->SetNode(this); - } - SetTupleConvertedType(checker->PrimitiveTypeAsETSBuiltinType(tupleTypeAtIdx)); - - checker->Relation()->SetNode(savedNode); } if (tupleTypeAtIdx->IsETSObjectType() && baseType->AsETSArrayType()->ElementType()->IsETSObjectType()) { diff --git a/ets2panda/test/compiler/ets/tuple_types_19-expected.txt b/ets2panda/test/compiler/ets/tuple_types_19-expected.txt new file mode 100644 index 00000000000..c1e89ef573f --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_19-expected.txt @@ -0,0 +1,2271 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "TuplePair", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "one", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "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 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "provide", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "provide", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 29 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "property": { + "type": "Identifier", + "name": "one", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 44 + }, + "end": { + "line": 18, + "column": 47 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + { + "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": 16, + "column": 23 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 17 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + { + "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": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "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 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 30 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 39 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "right": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 46 + }, + "end": { + "line": 23, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 52 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 29 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 24, + "column": 23 + }, + "end": { + "line": 24, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 24, + "column": 39 + }, + "end": { + "line": 24, + "column": 43 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 44 + }, + "end": { + "line": 24, + "column": 48 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 39 + }, + "end": { + "line": 24, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 32 + }, + "end": { + "line": 24, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 30 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 19 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 19 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 25, + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "number_tup", + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 29 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 37 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 39 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 19 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 28, + "column": 41 + }, + "end": { + "line": 28, + "column": 45 + } + } + }, + { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 28, + "column": 47 + }, + "end": { + "line": 28, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 40 + }, + "end": { + "line": 28, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 53 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "tn", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 26 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 11 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 54 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 61 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 62 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 54 + }, + "end": { + "line": 29, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 63 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "number_tup", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 63 + }, + "end": { + "line": 29, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 37 + }, + "end": { + "line": 29, + "column": 75 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 75 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 75 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "result", + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 25 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 33 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 30, + "column": 17 + }, + "end": { + "line": 30, + "column": 35 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 15 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "tn", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 38 + } + } + }, + "property": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 39 + }, + "end": { + "line": 30, + "column": 46 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 46 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 48 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 31, + "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": 32, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/tuple_types_19.ets b/ets2panda/test/compiler/ets/tuple_types_19.ets new file mode 100644 index 00000000000..cb744db81f9 --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_19.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 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 TuplePair { + one: [X, Y] + public provide(): [X, Y] { return this.one; } +} + +class TupleProvider { + memb: [X, X]; + constructor(value: [X, X]) { this.memb = value;} + public publish(): [X, X] { return this.memb; } +} + +function foo() { + let number_tup: [Double, Double] = [42.0, 43.0]; + let tn: TupleProvider = new TupleProvider(number_tup); + let result: [Number, Number] = tn.publish(); +} diff --git a/ets2panda/test/runtime/ets/tuple_types_runtime.ets b/ets2panda/test/runtime/ets/tuple_types_runtime.ets index 5f57797cab2..046ad690bbe 100644 --- a/ets2panda/test/runtime/ets/tuple_types_runtime.ets +++ b/ets2panda/test/runtime/ets/tuple_types_runtime.ets @@ -28,6 +28,12 @@ class B extends A { } } +class TupleProvider { + memb: [X, X]; + constructor(value: [X, X]) { this.memb = value;} + public publish(): [X, X] { return this.memb; } +} + function main(): void { const tup_1: [string, number] = ["a", 12]; @@ -126,4 +132,9 @@ function main(): void { assert(a_b_tup[0].foo() == 1); assert(a_b_tup[1].foo() == 2); assert(a_b_tup[2].foo() == 2); + + let number_tup: [Double, Double] = [42.0, 43.0]; + let tn: TupleProvider = new TupleProvider(number_tup); + let result: [Number, Number] = tn.publish(); + assert result[0] == 42.0; } -- Gitee From 7d1cbaaabec4c108486f71ceca492530dcc71728 Mon Sep 17 00:00:00 2001 From: Soma Simon Date: Tue, 16 Jan 2024 11:32:09 +0100 Subject: [PATCH 06/21] [ArkTS frontend] Fix cannot reference 'this' in this context Save context at proxy methods to fix the problem with this expression. Create a better check if a proxy method has return type. Fixes internal issue #14623 Change-Id: I03f41a1e90a6bde387ab504ce886694a69f8a40f Signed-off-by: Soma Simon --- ets2panda/checker/ETSAnalyzer.cpp | 1 + ets2panda/parser/ETSparser.cpp | 2 +- ..._implicitly_typed_return_void-expected.txt | 4 +- .../test/parser/ets/proxy_method-expected.txt | 1440 +++++++++++++++++ ets2panda/test/parser/ets/proxy_method.ets | 24 + .../test/runtime/ets/InferProxymethodType.ets | 37 + 6 files changed, 1505 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/parser/ets/proxy_method-expected.txt create mode 100644 ets2panda/test/parser/ets/proxy_method.ets create mode 100644 ets2panda/test/runtime/ets/InferProxymethodType.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 77a8d79564a..f2d00c77dbb 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1145,6 +1145,7 @@ checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const } if (expr->Signature()->HasSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE)) { + checker::SavedCheckerContext savedCtx(checker, checker->Context().Status(), expr->Signature()->Owner()); expr->Signature()->OwnerVar()->Declaration()->Node()->Check(checker); returnType = expr->Signature()->ReturnType(); // NOTE(vpukhov): #14902 substituted signature is not updated diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index b6d884410d2..8bacd3eba68 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -2299,7 +2299,7 @@ ir::MethodDefinition *ETSParser::CreateProxyMethodDefinition(ir::MethodDefinitio } proxyMethod += ' '; - if (returnType != "void") { + if ((function->AsScriptFunction()->Flags() & ir::ScriptFunctionFlags::HAS_RETURN) != 0) { proxyMethod += "return "; } diff --git a/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt b/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt index 1e351257677..bb716f90ae0 100644 --- a/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt +++ b/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt @@ -1100,8 +1100,8 @@ } }, { - "type": "ReturnStatement", - "argument": { + "type": "ExpressionStatement", + "expression": { "type": "CallExpression", "callee": { "type": "Identifier", diff --git a/ets2panda/test/parser/ets/proxy_method-expected.txt b/ets2panda/test/parser/ets/proxy_method-expected.txt new file mode 100644 index 00000000000..fa34056abf1 --- /dev/null +++ b/ets2panda/test/parser/ets/proxy_method-expected.txt @@ -0,0 +1,1440 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "s", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "q", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "StringLiteral", + "value": "met ", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "right": { + "type": "Identifier", + "name": "s", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 31 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 31 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "met_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "met_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "s", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "q", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "q", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "s", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "q", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "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": 9 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "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": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "S", + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 20 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/proxy_method.ets b/ets2panda/test/parser/ets/proxy_method.ets new file mode 100644 index 00000000000..d058f714957 --- /dev/null +++ b/ets2panda/test/parser/ets/proxy_method.ets @@ -0,0 +1,24 @@ +/* + * 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 A { + met(s: string, q?: string){ + console.log("met " + s) + } +} + +function main() { + new A().met('S') +} diff --git a/ets2panda/test/runtime/ets/InferProxymethodType.ets b/ets2panda/test/runtime/ets/InferProxymethodType.ets new file mode 100644 index 00000000000..334bb0f6ecc --- /dev/null +++ b/ets2panda/test/runtime/ets/InferProxymethodType.ets @@ -0,0 +1,37 @@ +/* + * 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 A { + met(s: string, q?: string){ + return true; + } + + mat2(){ + return this.met("cat","a"); + } +} + +function main() { + let newA = new A(); + let a1 = new A().met('S'); + let a2 = new A().met('S',"a"); + let a3 = newA.met('S', 'a'); + let b = new A().mat2(); + + assert a1; + assert a2; + assert a3; + assert b; +} -- Gitee From 06d656264c26ea8c4d6d1034e63f65f94c4e6bd6 Mon Sep 17 00:00:00 2001 From: ozerovnikita Date: Thu, 21 Dec 2023 19:28:37 +0300 Subject: [PATCH 07/21] fix CTE in assignment non-functional non-functional variable to a functional type Description: replaced ThrowError() call that returns Unresolved reference with ThrowTypeError() call with error description Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8S702 Signed-off-by: ozerovnikita --- ets2panda/checker/ets/helpers.cpp | 3 +- ...l_variable_to_functional_type-expected.txt | 894 ++++++++++++++++++ ...functional_variable_to_functional_type.ets | 26 + ...variable_to_functional_type_1-expected.txt | 449 +++++++++ ...nctional_variable_to_functional_type_1.ets | 21 + 5 files changed, 1392 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index e720f7b96fa..77aa9046324 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -586,7 +586,8 @@ void ETSChecker::ValidateAssignmentIdentifier(ir::Identifier *const ident, varbi if (targetType->IsETSObjectType() && targetType->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) { if (!type->IsETSFunctionType() && !(type->IsETSObjectType() && type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL))) { - ThrowError(ident); + ThrowTypeError({"Assigning a non-functional variable \"", ident->Name(), "\" to a functional type"}, + ident->Start()); } return; diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt new file mode 100644 index 00000000000..6ca5ac47b09 --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt @@ -0,0 +1,894 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "__Observable__", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 30 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "__memo_state_observable_handler__", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + { + "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": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "installOn", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "installOn", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "observable", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 60 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "__Observable__", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "property": { + "type": "Identifier", + "name": "__memo_state_observable_handler__", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 68 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 68 + } + } + }, + "right": { + "type": "Identifier", + "name": "observable", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 81 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 81 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 81 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + { + "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": 26, + "column": 2 + }, + "end": { + "line": 26, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 26, + "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 + } + } + } + ], + "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": 27, + "column": 1 + } + } +} +TypeError: Assigning a non-functional variable "observable" to a functional type [assignment_non-functional_variable_to_functional_type.ets:23:71] diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets new file mode 100644 index 00000000000..4288ab1f6b7 --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023 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. + */ + +abstract class __Observable__ { + public __memo_state_observable_handler__: () => Observable|undefined; +} + +class Observable { + static installOn(value: Object, observable: Observable|undefined) + { + (value as __Observable__).__memo_state_observable_handler__ = observable + } + +} diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt new file mode 100644 index 00000000000..366185c853b --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt @@ -0,0 +1,449 @@ +{ + "type": "Program", + "statements": [ + { + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "right": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Assigning a non-functional variable "b" to a functional type [assignment_non-functional_variable_to_functional_type_1.ets:20:9] diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets new file mode 100644 index 00000000000..fec2befa80c --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function main() +{ + let a: () => int + let b: int = 5 + a = b +} -- Gitee From 59976e060f789dceb683d47d85b91c26e35c3294 Mon Sep 17 00:00:00 2001 From: Molokanov Yaroslav Date: Thu, 28 Dec 2023 14:18:41 +0300 Subject: [PATCH 08/21] ValidateSignature method fix for CodeCheck Reducing ValidateSignature method cyclomatic complexity and size Signed-off-by: Molokanov Yaroslav --- ets2panda/checker/ETSchecker.h | 5 + ets2panda/checker/ets/function.cpp | 163 +++++++++++++++++------------ 2 files changed, 99 insertions(+), 69 deletions(-) diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 60b5410d3c0..44cd7dfbda5 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -313,6 +313,11 @@ public: Signature *ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, TypeRelationFlag initialFlags, const std::vector &argTypeInferenceRequired); + bool ValidateSignatureRequiredParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, const std::vector &argTypeInferenceRequired, + bool throwError); + bool ValidateSignatureRestParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, bool throwError); Signature *ValidateSignatures(ArenaVector &signatures, const ir::TSTypeParameterInstantiation *typeArguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index e6f781ed573..7dfecd77be3 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -182,38 +182,14 @@ Signature *ETSChecker::ValidateParameterlessConstructor(Signature *signature, co return signature; } -Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, - const ArenaVector &arguments, - const lexer::SourcePosition &pos, TypeRelationFlag flags, - const std::vector &argTypeInferenceRequired) +bool ETSChecker::ValidateSignatureRequiredParams(Signature *substitutedSig, + const ArenaVector &arguments, TypeRelationFlag flags, + const std::vector &argTypeInferenceRequired, bool throwError) { - if (signature->Function()->IsDefaultParamProxy() && ((flags & TypeRelationFlag::CHECK_PROXY) == 0)) { - return nullptr; - } - - Signature *substitutedSig = MaybeSubstituteTypeParameters(this, signature, typeArguments, arguments, pos, flags); - if (substitutedSig == nullptr) { - return nullptr; - } - - auto const hasRestParameter = substitutedSig->RestVar() != nullptr; std::size_t const argumentCount = arguments.size(); std::size_t const parameterCount = substitutedSig->MinArgCount(); - auto const throwError = (flags & TypeRelationFlag::NO_THROW) == 0; - - if (!signature->Function()->IsDefaultParamProxy()) { - if (argumentCount < parameterCount || (argumentCount > parameterCount && !hasRestParameter)) { - if (throwError) { - ThrowTypeError({"Expected ", parameterCount, " arguments, got ", argumentCount, "."}, pos); - } - return nullptr; - } - } - - // Check all required formal parameter(s) first - auto const count = std::min(parameterCount, argumentCount); - std::size_t index = 0U; - for (; index < count; ++index) { + auto count = std::min(parameterCount, argumentCount); + for (std::size_t index = 0; index < count; ++index) { auto &argument = arguments[index]; if (argument->IsObjectExpression()) { @@ -221,7 +197,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP // No chance to check the argument at this point continue; } - return nullptr; + return false; } if (argument->IsMemberExpression()) { @@ -231,7 +207,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP if (throwError) { ThrowTypeError("Spread argument cannot be passed for ordinary parameter.", argument->Start()); } - return nullptr; + return false; } if (argTypeInferenceRequired[index]) { @@ -241,7 +217,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP if (CheckLambdaAssignable(substitutedSig->Function()->Params()[index], lambda)) { continue; } - return nullptr; + return false; } if (argument->IsArrayExpression()) { @@ -251,50 +227,99 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP auto *const argumentType = argument->Check(this); - if (auto const invocationCtx = checker::InvocationContext( - Relation(), argument, argumentType, substitutedSig->Params()[index]->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_2}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; + auto const invocationCtx = checker::InvocationContext( + Relation(), argument, argumentType, substitutedSig->Params()[index]->TsType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_2}, flags); + if (!invocationCtx.IsInvocable()) { + return false; } } - // Check rest parameter(s) if any exists - if (hasRestParameter && index < argumentCount) { - auto const restCount = argumentCount - index; - - for (; index < argumentCount; ++index) { - auto &argument = arguments[index]; - - if (argument->IsSpreadElement()) { - if (restCount > 1U) { - if (throwError) { - ThrowTypeError("Spread argument for the rest parameter can be only one.", argument->Start()); - } - return nullptr; - } + return true; +} - auto *const restArgument = argument->AsSpreadElement()->Argument(); - auto *const argumentType = restArgument->Check(this); +bool ETSChecker::ValidateSignatureRestParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, bool throwError) +{ + std::size_t const argumentCount = arguments.size(); + std::size_t const parameterCount = substitutedSig->MinArgCount(); + auto count = std::min(parameterCount, argumentCount); + auto const restCount = argumentCount - count; - if (auto const invocationCtx = checker::InvocationContext( - Relation(), restArgument, argumentType, substitutedSig->RestVar()->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; - } - } else { - auto *const argumentType = argument->Check(this); - - if (auto const invocationCtx = checker::InvocationContext( - Relation(), argument, argumentType, - substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; - } + for (std::size_t index = count; index < argumentCount; ++index) { + auto &argument = arguments[index]; + + if (!argument->IsSpreadElement()) { + auto const invocationCtx = checker::InvocationContext( + Relation(), argument, argument->Check(this), + substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + if (!invocationCtx.IsInvocable()) { + return false; } + continue; } + + if (restCount > 1U) { + if (throwError) { + ThrowTypeError("Spread argument for the rest parameter can be only one.", argument->Start()); + } + return false; + } + + auto *const restArgument = argument->AsSpreadElement()->Argument(); + + auto const invocationCtx = checker::InvocationContext( + Relation(), restArgument, restArgument->Check(this), substitutedSig->RestVar()->TsType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + if (!invocationCtx.IsInvocable()) { + return false; + } + } + + return true; +} + +Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, + const ArenaVector &arguments, + const lexer::SourcePosition &pos, TypeRelationFlag flags, + const std::vector &argTypeInferenceRequired) +{ + if (signature->Function()->IsDefaultParamProxy() && ((flags & TypeRelationFlag::CHECK_PROXY) == 0)) { + return nullptr; + } + + Signature *substitutedSig = MaybeSubstituteTypeParameters(this, signature, typeArguments, arguments, pos, flags); + if (substitutedSig == nullptr) { + return nullptr; + } + + auto const hasRestParameter = substitutedSig->RestVar() != nullptr; + std::size_t const argumentCount = arguments.size(); + std::size_t const parameterCount = substitutedSig->MinArgCount(); + auto const throwError = (flags & TypeRelationFlag::NO_THROW) == 0; + + if (!signature->Function()->IsDefaultParamProxy()) { + if (argumentCount < parameterCount || (argumentCount > parameterCount && !hasRestParameter)) { + if (throwError) { + ThrowTypeError({"Expected ", parameterCount, " arguments, got ", argumentCount, "."}, pos); + } + return nullptr; + } + } + + auto count = std::min(parameterCount, argumentCount); + // Check all required formal parameter(s) first + if (!ValidateSignatureRequiredParams(substitutedSig, arguments, flags, argTypeInferenceRequired, throwError)) { + return nullptr; + } + + // Check rest parameter(s) if any exists + if (!hasRestParameter || count >= argumentCount) { + return substitutedSig; + } + if (!ValidateSignatureRestParams(substitutedSig, arguments, flags, throwError)) { + return nullptr; } return substitutedSig; -- Gitee From 955247f8f7fb161b4abcc73d94b5be34cf421d05 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 19 Jan 2024 15:46:57 +0100 Subject: [PATCH 09/21] Fix getter/setter instantiation for generic classes Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8XLK2 Internal issue: #15308 Test: build, new checker test Signed-off-by: Martin Sajti --- ets2panda/checker/types/ets/etsObjectType.cpp | 3 +- .../generic_class_getter_setter-expected.txt | 1064 +++++++++++++++++ .../ets/generic_class_getter_setter.ets | 23 + 3 files changed, 1089 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt create mode 100644 ets2panda/test/compiler/ets/generic_class_getter_setter.ets diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 17cbff5b6f9..ab6f4fb3f58 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -780,7 +780,8 @@ static varbinder::LocalVariable *CopyPropertyWithTypeArguments(varbinder::LocalV const Substitution *substitution) { auto *const checker = relation->GetChecker()->AsETSChecker(); - auto *const copiedPropType = checker->GetTypeOfVariable(prop)->Substitute(relation, substitution); + auto *const varType = ETSChecker::IsVariableGetterSetter(prop) ? prop->TsType() : checker->GetTypeOfVariable(prop); + auto *const copiedPropType = varType->Substitute(relation, substitution); auto *const copiedProp = prop->Copy(checker->Allocator(), prop->Declaration()); copiedPropType->SetVariable(copiedProp); copiedProp->SetTsType(copiedPropType); diff --git a/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt b/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt new file mode 100644 index 00000000000..aa42a014854 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt @@ -0,0 +1,1064 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "get", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "set", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "memb", + "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": "a0", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 25 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + { + "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": 16, + "column": 12 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "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": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "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": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "memb", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "init": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 29 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 40 + }, + "end": { + "line": 22, + "column": 44 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 44 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_class_getter_setter.ets b/ets2panda/test/compiler/ets/generic_class_getter_setter.ets new file mode 100644 index 00000000000..3ee99d0898d --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_class_getter_setter.ets @@ -0,0 +1,23 @@ +/* + * 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 A { + get memb(): number { return 1.0; } + set memb(a0: number): void {} +} + +function main(): void { + let memb: number = new A().memb; +} -- Gitee From 10c71afa38cb0a588f8f4324703be00f9c0b6c34 Mon Sep 17 00:00:00 2001 From: zhao-xinyuan8 Date: Thu, 11 Jan 2024 03:39:39 +0000 Subject: [PATCH 10/21] Fix new array creation workaround logic Remove new array creation workaround logic for ETS_ARRAY and GENERIC types. Remove errorneous case launch_expression.ets Signed-off-by: zhao-xinyuan8 --- ets2panda/checker/ETSAnalyzer.cpp | 2 - .../ets/launch_expression-expected.txt | 1265 +++++++++-------- .../test/compiler/ets/launch_expression.ets | 3 +- 3 files changed, 641 insertions(+), 629 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index f2d00c77dbb..885e81d2083 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -506,9 +506,7 @@ checker::Type *ETSAnalyzer::Check(ir::ETSNewArrayInstanceExpression *expr) const checker->ValidateArrayIndex(expr->dimension_, true); if (!elementType->HasTypeFlag(TypeFlag::ETS_PRIMITIVE) && !elementType->IsNullish() && - !elementType->HasTypeFlag(TypeFlag::GENERIC) && !elementType->HasTypeFlag(TypeFlag::ETS_ARRAY) && elementType->ToAssemblerName().str() != "Ball") { - // Check only valid for ETS_PRIMITIVE and IsNullish, GENERIC and ETS_ARRAY are workaround checks for stdlib // Ball is workaround for koala ui lib if (elementType->IsETSObjectType()) { auto *calleeObj = elementType->AsETSObjectType(); diff --git a/ets2panda/test/compiler/ets/launch_expression-expected.txt b/ets2panda/test/compiler/ets/launch_expression-expected.txt index ef4e77e9429..e0578ae1619 100644 --- a/ets2panda/test/compiler/ets/launch_expression-expected.txt +++ b/ets2panda/test/compiler/ets/launch_expression-expected.txt @@ -1,6 +1,131 @@ { "type": "Program", "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "P", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 7 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, { "type": "ClassDeclaration", "definition": { @@ -627,11 +752,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 10 }, "end": { - "line": 20, + "line": 21, "column": 19 } } @@ -651,11 +776,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 10 }, "end": { - "line": 20, + "line": 21, "column": 19 } } @@ -673,11 +798,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 20, + "line": 21, "column": 28 }, "end": { - "line": 20, + "line": 21, "column": 31 } } @@ -685,22 +810,22 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 20 }, "end": { - "line": 20, + "line": 21, "column": 31 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 20 }, "end": { - "line": 20, + "line": 21, "column": 31 } } @@ -714,11 +839,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 20, + "line": 21, "column": 41 }, "end": { - "line": 20, + "line": 21, "column": 44 } } @@ -726,22 +851,22 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 33 }, "end": { - "line": 20, + "line": 21, "column": 44 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 33 }, "end": { - "line": 20, + "line": 21, "column": 44 } } @@ -757,33 +882,33 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 51 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 53 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 53 } } @@ -802,11 +927,11 @@ "decorators": [], "loc": { "start": { - "line": 21, + "line": 22, "column": 9 }, "end": { - "line": 21, + "line": 22, "column": 15 } } @@ -817,22 +942,22 @@ "decorators": [], "loc": { "start": { - "line": 21, + "line": 22, "column": 19 }, "end": { - "line": 21, + "line": 22, "column": 25 } } }, "loc": { "start": { - "line": 21, + "line": 22, "column": 9 }, "end": { - "line": 21, + "line": 22, "column": 25 } } @@ -845,11 +970,11 @@ "argument": null, "loc": { "start": { - "line": 22, + "line": 23, "column": 9 }, "end": { - "line": 22, + "line": 23, "column": 16 } } @@ -857,11 +982,11 @@ ], "loc": { "start": { - "line": 21, + "line": 22, "column": 27 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -869,11 +994,11 @@ "alternate": null, "loc": { "start": { - "line": 21, + "line": 22, "column": 5 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -890,11 +1015,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 12 } } @@ -905,11 +1030,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 13 }, "end": { - "line": 24, + "line": 25, "column": 20 } } @@ -918,11 +1043,11 @@ "optional": false, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 20 } } @@ -942,11 +1067,11 @@ "value": "Values of type int are not equal: ", "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 57 } } @@ -957,22 +1082,22 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 60 }, "end": { - "line": 24, + "line": 25, "column": 66 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 66 } } @@ -982,22 +1107,22 @@ "value": " != ", "loc": { "start": { - "line": 24, + "line": 25, "column": 69 }, "end": { - "line": 24, + "line": 25, "column": 75 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 75 } } @@ -1008,22 +1133,22 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 78 }, "end": { - "line": 24, + "line": 25, "column": 84 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 84 } } @@ -1032,22 +1157,22 @@ "optional": false, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 85 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 86 } } @@ -1066,33 +1191,33 @@ "decorators": [], "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 20 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 21 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 21 } } @@ -1100,22 +1225,22 @@ "arguments": [], "loc": { "start": { - "line": 25, + "line": 26, "column": 11 }, "end": { - "line": 25, + "line": 26, "column": 23 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 5 }, "end": { - "line": 25, + "line": 26, "column": 23 } } @@ -1123,33 +1248,33 @@ ], "loc": { "start": { - "line": 20, + "line": 21, "column": 52 }, "end": { - "line": 26, + "line": 27, "column": 2 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 19 }, "end": { - "line": 26, + "line": 27, "column": 2 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 19 }, "end": { - "line": 26, + "line": 27, "column": 2 } } @@ -1158,11 +1283,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 1 }, "end": { - "line": 26, + "line": 27, "column": 2 } } @@ -1175,11 +1300,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 10 }, "end": { - "line": 27, + "line": 28, "column": 14 } } @@ -1199,11 +1324,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 10 }, "end": { - "line": 27, + "line": 28, "column": 14 } } @@ -1221,11 +1346,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 27, + "line": 28, "column": 18 }, "end": { - "line": 27, + "line": 28, "column": 21 } } @@ -1233,22 +1358,22 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 15 }, "end": { - "line": 27, + "line": 28, "column": 21 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 15 }, "end": { - "line": 27, + "line": 28, "column": 21 } } @@ -1264,33 +1389,33 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 28 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 30 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 30 } } @@ -1312,11 +1437,11 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 10 } } @@ -1326,22 +1451,22 @@ "value": 0, "loc": { "start": { - "line": 28, + "line": 29, "column": 14 }, "end": { - "line": 28, + "line": 29, "column": 15 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 15 } } @@ -1355,11 +1480,11 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 19 }, "end": { - "line": 28, + "line": 29, "column": 20 } } @@ -1370,33 +1495,33 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 23 }, "end": { - "line": 28, + "line": 29, "column": 28 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 19 }, "end": { - "line": 28, + "line": 29, "column": 28 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 28 } } @@ -1414,11 +1539,11 @@ "decorators": [], "loc": { "start": { - "line": 29, + "line": 30, "column": 16 }, "end": { - "line": 29, + "line": 30, "column": 17 } } @@ -1429,11 +1554,11 @@ "decorators": [], "loc": { "start": { - "line": 29, + "line": 30, "column": 18 }, "end": { - "line": 29, + "line": 30, "column": 19 } } @@ -1442,22 +1567,22 @@ "optional": false, "loc": { "start": { - "line": 29, + "line": 30, "column": 16 }, "end": { - "line": 29, + "line": 30, "column": 20 } } }, "loc": { "start": { - "line": 29, + "line": 30, "column": 9 }, "end": { - "line": 29, + "line": 30, "column": 21 } } @@ -1465,11 +1590,11 @@ ], "loc": { "start": { - "line": 28, + "line": 29, "column": 30 }, "end": { - "line": 30, + "line": 31, "column": 6 } } @@ -1477,11 +1602,11 @@ "alternate": null, "loc": { "start": { - "line": 28, + "line": 29, "column": 5 }, "end": { - "line": 30, + "line": 31, "column": 6 } } @@ -1502,116 +1627,60 @@ "type": "ETSTypeReferencePart", "name": { "type": "Identifier", - "name": "Promise", + "name": "P", "decorators": [], "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 19 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 23 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 24 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 24 - } - } - } - ], - "loc": { - "start": { - "line": 31, - "column": 19 - }, - "end": { - "line": 31, - "column": 24 + "line": 32, + "column": 13 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 25 + "line": 32, + "column": 14 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 25 + "line": 32, + "column": 14 } } }, "loc": { "start": { - "line": 31, - "column": 27 + "line": 32, + "column": 16 }, "end": { - "line": 31, - "column": 28 + "line": 32, + "column": 17 } } }, "decorators": [], "loc": { "start": { - "line": 31, + "line": 32, "column": 9 }, "end": { - "line": 31, + "line": 32, "column": 10 } } @@ -1624,94 +1693,38 @@ "type": "ETSTypeReferencePart", "name": { "type": "Identifier", - "name": "Promise", + "name": "P", "decorators": [], "loc": { "start": { - "line": 31, - "column": 33 - }, - "end": { - "line": 31, - "column": 40 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 44 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 45 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 45 - } - } - } - ], - "loc": { - "start": { - "line": 31, - "column": 40 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 45 + "line": 32, + "column": 23 } } }, "loc": { "start": { - "line": 31, - "column": 33 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 } } }, "loc": { "start": { - "line": 31, - "column": 33 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 } } }, @@ -1721,34 +1734,34 @@ "decorators": [], "loc": { "start": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 }, "end": { - "line": 31, - "column": 51 + "line": 32, + "column": 29 } } }, "loc": { "start": { - "line": 31, - "column": 29 + "line": 32, + "column": 18 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 9 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } } @@ -1756,12 +1769,12 @@ "kind": "let", "loc": { "start": { - "line": 31, + "line": 32, "column": 5 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } }, @@ -1778,11 +1791,11 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 14 }, "end": { - "line": 32, + "line": 33, "column": 15 } } @@ -1792,22 +1805,22 @@ "value": 0, "loc": { "start": { - "line": 32, + "line": 33, "column": 18 }, "end": { - "line": 32, + "line": 33, "column": 19 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 14 }, "end": { - "line": 32, + "line": 33, "column": 19 } } @@ -1816,11 +1829,11 @@ "kind": "let", "loc": { "start": { - "line": 32, + "line": 33, "column": 10 }, "end": { - "line": 32, + "line": 33, "column": 19 } } @@ -1834,11 +1847,11 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 21 }, "end": { - "line": 32, + "line": 33, "column": 22 } } @@ -1849,22 +1862,22 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 25 }, "end": { - "line": 32, + "line": 33, "column": 30 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 21 }, "end": { - "line": 32, + "line": 33, "column": 30 } } @@ -1879,22 +1892,22 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 34 }, "end": { - "line": 32, + "line": 33, "column": 35 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 32 }, "end": { - "line": 32, + "line": 33, "column": 35 } } @@ -1915,11 +1928,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 10 } } @@ -1930,11 +1943,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 11 }, "end": { - "line": 33, + "line": 34, "column": 12 } } @@ -1943,11 +1956,11 @@ "optional": false, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 13 } } @@ -1962,11 +1975,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 23 }, "end": { - "line": 33, + "line": 34, "column": 27 } } @@ -1984,11 +1997,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 29 } } @@ -1998,22 +2011,22 @@ "value": 1, "loc": { "start": { - "line": 33, + "line": 34, "column": 30 }, "end": { - "line": 33, + "line": 34, "column": 31 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 31 } } @@ -2024,22 +2037,22 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 32 }, "end": { - "line": 33, + "line": 34, "column": 33 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 33 } } @@ -2048,44 +2061,44 @@ "optional": false, "loc": { "start": { - "line": 33, + "line": 34, "column": 23 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 16 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 35 } } @@ -2093,22 +2106,22 @@ ], "loc": { "start": { - "line": 32, + "line": 33, "column": 37 }, "end": { - "line": 34, + "line": 35, "column": 6 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 5 }, "end": { - "line": 34, + "line": 35, "column": 6 } } @@ -2124,11 +2137,11 @@ "decorators": [], "loc": { "start": { - "line": 35, + "line": 36, "column": 9 }, "end": { - "line": 35, + "line": 36, "column": 15 } } @@ -2138,22 +2151,22 @@ "value": 0, "loc": { "start": { - "line": 35, + "line": 36, "column": 18 }, "end": { - "line": 35, + "line": 36, "column": 19 } } }, "loc": { "start": { - "line": 35, + "line": 36, "column": 9 }, "end": { - "line": 35, + "line": 36, "column": 19 } } @@ -2162,11 +2175,11 @@ "kind": "let", "loc": { "start": { - "line": 35, + "line": 36, "column": 5 }, "end": { - "line": 35, + "line": 36, "column": 19 } } @@ -2184,11 +2197,11 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 14 }, "end": { - "line": 36, + "line": 37, "column": 15 } } @@ -2198,22 +2211,22 @@ "value": 0, "loc": { "start": { - "line": 36, + "line": 37, "column": 18 }, "end": { - "line": 36, + "line": 37, "column": 19 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 14 }, "end": { - "line": 36, + "line": 37, "column": 19 } } @@ -2222,11 +2235,11 @@ "kind": "let", "loc": { "start": { - "line": 36, + "line": 37, "column": 10 }, "end": { - "line": 36, + "line": 37, "column": 19 } } @@ -2240,11 +2253,11 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 21 }, "end": { - "line": 36, + "line": 37, "column": 22 } } @@ -2255,22 +2268,22 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 25 }, "end": { - "line": 36, + "line": 37, "column": 30 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 21 }, "end": { - "line": 36, + "line": 37, "column": 30 } } @@ -2285,22 +2298,22 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 34 }, "end": { - "line": 36, + "line": 37, "column": 35 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 32 }, "end": { - "line": 36, + "line": 37, "column": 35 } } @@ -2319,11 +2332,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 15 } } @@ -2337,11 +2350,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 18 }, "end": { - "line": 37, + "line": 38, "column": 24 } } @@ -2361,11 +2374,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 28 } } @@ -2376,11 +2389,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 29 }, "end": { - "line": 37, + "line": 38, "column": 30 } } @@ -2389,11 +2402,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 31 } } @@ -2404,11 +2417,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 32 }, "end": { - "line": 37, + "line": 38, "column": 47 } } @@ -2417,11 +2430,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 47 } } @@ -2430,11 +2443,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 49 } } @@ -2447,11 +2460,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 52 }, "end": { - "line": 37, + "line": 38, "column": 53 } } @@ -2462,11 +2475,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 54 }, "end": { - "line": 37, + "line": 38, "column": 55 } } @@ -2475,55 +2488,55 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 52 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 18 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 57 } } @@ -2531,22 +2544,22 @@ ], "loc": { "start": { - "line": 36, + "line": 37, "column": 37 }, "end": { - "line": 38, + "line": 39, "column": 6 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 5 }, "end": { - "line": 38, + "line": 39, "column": 6 } } @@ -2559,22 +2572,22 @@ "decorators": [], "loc": { "start": { - "line": 39, + "line": 40, "column": 12 }, "end": { - "line": 39, + "line": 40, "column": 18 } } }, "loc": { "start": { - "line": 39, + "line": 40, "column": 5 }, "end": { - "line": 39, + "line": 40, "column": 19 } } @@ -2582,33 +2595,33 @@ ], "loc": { "start": { - "line": 27, + "line": 28, "column": 29 }, "end": { - "line": 40, + "line": 41, "column": 2 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 14 }, "end": { - "line": 40, + "line": 41, "column": 2 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 14 }, "end": { - "line": 40, + "line": 41, "column": 2 } } @@ -2617,11 +2630,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 1 }, "end": { - "line": 40, + "line": 41, "column": 2 } } @@ -2634,11 +2647,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 10 }, "end": { - "line": 41, + "line": 42, "column": 18 } } @@ -2658,11 +2671,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 10 }, "end": { - "line": 41, + "line": 42, "column": 18 } } @@ -2680,11 +2693,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 41, + "line": 42, "column": 22 }, "end": { - "line": 41, + "line": 42, "column": 25 } } @@ -2692,22 +2705,22 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 19 }, "end": { - "line": 41, + "line": 42, "column": 25 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 19 }, "end": { - "line": 41, + "line": 42, "column": 25 } } @@ -2717,11 +2730,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 41, + "line": 42, "column": 29 }, "end": { - "line": 41, + "line": 42, "column": 32 } } @@ -2743,11 +2756,11 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 10 } } @@ -2757,22 +2770,22 @@ "value": 0, "loc": { "start": { - "line": 42, + "line": 43, "column": 14 }, "end": { - "line": 42, + "line": 43, "column": 15 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 15 } } @@ -2786,11 +2799,11 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 19 }, "end": { - "line": 42, + "line": 43, "column": 20 } } @@ -2801,33 +2814,33 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 23 }, "end": { - "line": 42, + "line": 43, "column": 28 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 19 }, "end": { - "line": 42, + "line": 43, "column": 28 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 28 } } @@ -2845,11 +2858,11 @@ "decorators": [], "loc": { "start": { - "line": 43, + "line": 44, "column": 16 }, "end": { - "line": 43, + "line": 44, "column": 17 } } @@ -2860,11 +2873,11 @@ "decorators": [], "loc": { "start": { - "line": 43, + "line": 44, "column": 18 }, "end": { - "line": 43, + "line": 44, "column": 19 } } @@ -2873,22 +2886,22 @@ "optional": false, "loc": { "start": { - "line": 43, + "line": 44, "column": 16 }, "end": { - "line": 43, + "line": 44, "column": 20 } } }, "loc": { "start": { - "line": 43, + "line": 44, "column": 9 }, "end": { - "line": 43, + "line": 44, "column": 21 } } @@ -2896,11 +2909,11 @@ ], "loc": { "start": { - "line": 42, + "line": 43, "column": 30 }, "end": { - "line": 44, + "line": 45, "column": 6 } } @@ -2908,11 +2921,11 @@ "alternate": null, "loc": { "start": { - "line": 42, + "line": 43, "column": 5 }, "end": { - "line": 44, + "line": 45, "column": 6 } } @@ -2928,11 +2941,11 @@ "decorators": [], "loc": { "start": { - "line": 45, + "line": 46, "column": 9 }, "end": { - "line": 45, + "line": 46, "column": 15 } } @@ -2942,22 +2955,22 @@ "value": 0, "loc": { "start": { - "line": 45, + "line": 46, "column": 18 }, "end": { - "line": 45, + "line": 46, "column": 19 } } }, "loc": { "start": { - "line": 45, + "line": 46, "column": 9 }, "end": { - "line": 45, + "line": 46, "column": 19 } } @@ -2966,11 +2979,11 @@ "kind": "let", "loc": { "start": { - "line": 45, + "line": 46, "column": 5 }, "end": { - "line": 45, + "line": 46, "column": 19 } } @@ -2988,11 +3001,11 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 14 }, "end": { - "line": 46, + "line": 47, "column": 15 } } @@ -3002,22 +3015,22 @@ "value": 0, "loc": { "start": { - "line": 46, + "line": 47, "column": 18 }, "end": { - "line": 46, + "line": 47, "column": 19 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 14 }, "end": { - "line": 46, + "line": 47, "column": 19 } } @@ -3026,11 +3039,11 @@ "kind": "let", "loc": { "start": { - "line": 46, + "line": 47, "column": 10 }, "end": { - "line": 46, + "line": 47, "column": 19 } } @@ -3044,11 +3057,11 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 21 }, "end": { - "line": 46, + "line": 47, "column": 22 } } @@ -3059,22 +3072,22 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 25 }, "end": { - "line": 46, + "line": 47, "column": 30 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 21 }, "end": { - "line": 46, + "line": 47, "column": 30 } } @@ -3089,22 +3102,22 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 34 }, "end": { - "line": 46, + "line": 47, "column": 35 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 32 }, "end": { - "line": 46, + "line": 47, "column": 35 } } @@ -3123,11 +3136,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 15 } } @@ -3141,11 +3154,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 18 }, "end": { - "line": 47, + "line": 48, "column": 24 } } @@ -3161,11 +3174,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 35 } } @@ -3183,11 +3196,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 37 } } @@ -3197,22 +3210,22 @@ "value": 1, "loc": { "start": { - "line": 47, + "line": 48, "column": 38 }, "end": { - "line": 47, + "line": 48, "column": 39 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 39 } } @@ -3223,22 +3236,22 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 40 }, "end": { - "line": 47, + "line": 48, "column": 41 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 41 } } @@ -3247,11 +3260,11 @@ "optional": false, "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 42 } } @@ -3264,11 +3277,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 45 }, "end": { - "line": 47, + "line": 48, "column": 46 } } @@ -3279,11 +3292,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 47 }, "end": { - "line": 47, + "line": 48, "column": 48 } } @@ -3292,55 +3305,55 @@ "optional": false, "loc": { "start": { - "line": 47, + "line": 48, "column": 45 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 18 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 50 } } @@ -3348,22 +3361,22 @@ ], "loc": { "start": { - "line": 46, + "line": 47, "column": 37 }, "end": { - "line": 48, + "line": 49, "column": 6 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 5 }, "end": { - "line": 48, + "line": 49, "column": 6 } } @@ -3376,22 +3389,22 @@ "decorators": [], "loc": { "start": { - "line": 49, + "line": 50, "column": 12 }, "end": { - "line": 49, + "line": 50, "column": 18 } } }, "loc": { "start": { - "line": 49, + "line": 50, "column": 5 }, "end": { - "line": 49, + "line": 50, "column": 19 } } @@ -3399,33 +3412,33 @@ ], "loc": { "start": { - "line": 41, + "line": 42, "column": 33 }, "end": { - "line": 50, + "line": 51, "column": 2 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 18 }, "end": { - "line": 50, + "line": 51, "column": 2 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 18 }, "end": { - "line": 50, + "line": 51, "column": 2 } } @@ -3434,11 +3447,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 1 }, "end": { - "line": 50, + "line": 51, "column": 2 } } @@ -3451,11 +3464,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 17 }, "end": { - "line": 51, + "line": 52, "column": 21 } } @@ -3475,11 +3488,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 17 }, "end": { - "line": 51, + "line": 52, "column": 21 } } @@ -3492,11 +3505,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 51, + "line": 52, "column": 25 }, "end": { - "line": 51, + "line": 52, "column": 28 } } @@ -3517,11 +3530,11 @@ "decorators": [], "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 6 } } @@ -3531,11 +3544,11 @@ "value": 0, "loc": { "start": { - "line": 52, + "line": 53, "column": 7 }, "end": { - "line": 52, + "line": 53, "column": 8 } } @@ -3544,11 +3557,11 @@ "optional": false, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 9 } } @@ -3558,33 +3571,33 @@ "value": 2, "loc": { "start": { - "line": 52, + "line": 53, "column": 12 }, "end": { - "line": 52, + "line": 53, "column": 13 } } }, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 13 } } }, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 14 } } @@ -3602,11 +3615,11 @@ "decorators": [], "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 6 } } @@ -3616,11 +3629,11 @@ "value": 0, "loc": { "start": { - "line": 53, + "line": 54, "column": 7 }, "end": { - "line": 53, + "line": 54, "column": 8 } } @@ -3629,11 +3642,11 @@ "optional": false, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 9 } } @@ -3643,33 +3656,33 @@ "value": 6, "loc": { "start": { - "line": 53, + "line": 54, "column": 12 }, "end": { - "line": 53, + "line": 54, "column": 13 } } }, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 13 } } }, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 14 } } @@ -3687,11 +3700,11 @@ "decorators": [], "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 6 } } @@ -3701,11 +3714,11 @@ "value": 1, "loc": { "start": { - "line": 54, + "line": 55, "column": 7 }, "end": { - "line": 54, + "line": 55, "column": 8 } } @@ -3714,11 +3727,11 @@ "optional": false, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 9 } } @@ -3728,33 +3741,33 @@ "value": 2, "loc": { "start": { - "line": 54, + "line": 55, "column": 12 }, "end": { - "line": 54, + "line": 55, "column": 13 } } }, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 13 } } }, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 14 } } @@ -3772,11 +3785,11 @@ "decorators": [], "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 6 } } @@ -3786,11 +3799,11 @@ "value": 1, "loc": { "start": { - "line": 55, + "line": 56, "column": 7 }, "end": { - "line": 55, + "line": 56, "column": 8 } } @@ -3799,11 +3812,11 @@ "optional": false, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 9 } } @@ -3813,33 +3826,33 @@ "value": 7, "loc": { "start": { - "line": 55, + "line": 56, "column": 12 }, "end": { - "line": 55, + "line": 56, "column": 13 } } }, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 13 } } }, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 14 } } @@ -3855,11 +3868,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 9 }, "end": { - "line": 56, + "line": 57, "column": 19 } } @@ -3872,11 +3885,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 22 }, "end": { - "line": 56, + "line": 57, "column": 30 } } @@ -3888,11 +3901,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 31 }, "end": { - "line": 56, + "line": 57, "column": 32 } } @@ -3901,22 +3914,22 @@ "optional": false, "loc": { "start": { - "line": 56, + "line": 57, "column": 22 }, "end": { - "line": 56, + "line": 57, "column": 33 } } }, "loc": { "start": { - "line": 56, + "line": 57, "column": 9 }, "end": { - "line": 56, + "line": 57, "column": 33 } } @@ -3925,11 +3938,11 @@ "kind": "let", "loc": { "start": { - "line": 56, + "line": 57, "column": 5 }, "end": { - "line": 56, + "line": 57, "column": 34 } } @@ -3945,11 +3958,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 9 }, "end": { - "line": 57, + "line": 58, "column": 10 } } @@ -3964,11 +3977,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 20 }, "end": { - "line": 57, + "line": 58, "column": 24 } } @@ -3980,11 +3993,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 25 }, "end": { - "line": 57, + "line": 58, "column": 26 } } @@ -3993,33 +4006,33 @@ "optional": false, "loc": { "start": { - "line": 57, + "line": 58, "column": 20 }, "end": { - "line": 57, + "line": 58, "column": 27 } } }, "loc": { "start": { - "line": 57, + "line": 58, "column": 13 }, "end": { - "line": 57, + "line": 58, "column": 27 } } }, "loc": { "start": { - "line": 57, + "line": 58, "column": 9 }, "end": { - "line": 57, + "line": 58, "column": 27 } } @@ -4028,11 +4041,11 @@ "kind": "let", "loc": { "start": { - "line": 57, + "line": 58, "column": 5 }, "end": { - "line": 57, + "line": 58, "column": 28 } } @@ -4048,11 +4061,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 9 }, "end": { - "line": 58, + "line": 59, "column": 18 } } @@ -4067,11 +4080,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 22 } } @@ -4082,11 +4095,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 23 }, "end": { - "line": 58, + "line": 59, "column": 38 } } @@ -4095,11 +4108,11 @@ "optional": false, "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 38 } } @@ -4108,22 +4121,22 @@ "optional": false, "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 40 } } }, "loc": { "start": { - "line": 58, + "line": 59, "column": 9 }, "end": { - "line": 58, + "line": 59, "column": 40 } } @@ -4132,11 +4145,11 @@ "kind": "let", "loc": { "start": { - "line": 58, + "line": 59, "column": 5 }, "end": { - "line": 58, + "line": 59, "column": 41 } } @@ -4151,11 +4164,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 14 } } @@ -4169,11 +4182,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 15 }, "end": { - "line": 59, + "line": 60, "column": 24 } } @@ -4182,22 +4195,22 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 59, + "line": 60, "column": 28 }, "end": { - "line": 59, + "line": 60, "column": 31 } } }, "loc": { "start": { - "line": 59, + "line": 60, "column": 15 }, "end": { - "line": 59, + "line": 60, "column": 24 } } @@ -4208,11 +4221,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 33 }, "end": { - "line": 59, + "line": 60, "column": 43 } } @@ -4221,22 +4234,22 @@ "optional": false, "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 44 } } }, "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 45 } } @@ -4248,22 +4261,22 @@ "value": 0, "loc": { "start": { - "line": 60, + "line": 61, "column": 12 }, "end": { - "line": 60, + "line": 61, "column": 13 } } }, "loc": { "start": { - "line": 60, + "line": 61, "column": 5 }, "end": { - "line": 60, + "line": 61, "column": 14 } } @@ -4271,33 +4284,33 @@ ], "loc": { "start": { - "line": 51, + "line": 52, "column": 29 }, "end": { - "line": 61, + "line": 62, "column": 2 } } }, "loc": { "start": { - "line": 51, + "line": 52, "column": 21 }, "end": { - "line": 61, + "line": 62, "column": 2 } } }, "loc": { "start": { - "line": 51, + "line": 52, "column": 21 }, "end": { - "line": 61, + "line": 62, "column": 2 } } @@ -4306,11 +4319,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 8 }, "end": { - "line": 61, + "line": 62, "column": 2 } } @@ -4345,7 +4358,7 @@ "column": 1 }, "end": { - "line": 62, + "line": 63, "column": 1 } } diff --git a/ets2panda/test/compiler/ets/launch_expression.ets b/ets2panda/test/compiler/ets/launch_expression.ets index 011deb2dee7..362b63db113 100644 --- a/ets2panda/test/compiler/ets/launch_expression.ets +++ b/ets2panda/test/compiler/ets/launch_expression.ets @@ -17,6 +17,7 @@ let count = 2 let n = 9 let a: int[] = new int[count]; let v: int[] = new int[count]; +type P = Promise | null function assert_eq(value1: int, value2: int): void { if (value1 == value2) { return; @@ -28,7 +29,7 @@ function ufib(n: int) : Int { if (n >= 0 && n < count) { return v[n]; } - let p: Promise[] = new Promise[count] + let p: P[] = new P[count] for (let i = 0; i < count; ++i) { p[i] = launch ufib(n-1-i); } -- Gitee From 9f7f04b2a1f3707c77ff97e2739020011fd1c929 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 22 Dec 2023 17:17:05 +0100 Subject: [PATCH 11/21] Class getter and setter generation Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8QPT7 Internal issue: #15142 Test: build, new checker tests Change-Id: Icd1d5db370c2503933ef3a8b944ecaf1a70ada8a Signed-off-by: Gergo Torok Signed-off-by: Martin Sajti --- ets2panda/checker/ETSchecker.h | 7 ++ ets2panda/checker/ets/helpers.cpp | 89 +++++++++++++++++++ ets2panda/checker/ets/object.cpp | 62 ++++++++++++- .../ets/interfacePropertyDeclarations.cpp | 11 ++- .../test/runtime/ets/classGetterSetter.ets | 33 +++++++ ets2panda/varbinder/variableFlags.h | 1 + 6 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/runtime/ets/classGetterSetter.ets diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 44cd7dfbda5..e565e924bda 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -177,6 +177,7 @@ public: varbinder::Variable *ResolveInstanceExtension(const ir::MemberExpression *memberExpr); void CheckImplicitSuper(ETSObjectType *classType, Signature *ctorSig); void CheckValidInheritance(ETSObjectType *classType, ir::ClassDefinition *classDef); + void TransformProperties(ETSObjectType *classType); void CheckGetterSetterProperties(ETSObjectType *classType); void AddElementsToModuleObject(ETSObjectType *moduleObj, const util::StringView &str); Type *FindLeastUpperBound(Type *source, Type *target); @@ -547,6 +548,12 @@ public: void ModifyPreferredType(ir::ArrayExpression *arrayExpr, Type *newPreferredType); Type *SelectGlobalIntegerTypeForNumeric(Type *type); + void GenerateGetterSetterBody(ETSChecker *checker, ArenaVector &stmts, + ArenaVector ¶ms, ir::ClassProperty *field, + varbinder::FunctionParamScope *paramScope, bool isSetter); + static ir::MethodDefinition *GenerateDefaultGetterSetter(ir::ClassProperty *field, varbinder::ClassScope *scope, + bool isSetter, ETSChecker *checker); + // Exception ETSObjectType *CheckExceptionOrErrorType(checker::Type *type, lexer::SourcePosition pos); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 77aa9046324..5c1b3fd1877 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2633,6 +2633,95 @@ std::string GenerateImplicitInstantiateArg(varbinder::LocalVariable *instantiate return implicitInstantiateArgument; } +void ETSChecker::GenerateGetterSetterBody(ETSChecker *checker, ArenaVector &stmts, + ArenaVector ¶ms, ir::ClassProperty *const field, + varbinder::FunctionParamScope *paramScope, bool isSetter) +{ + if (!isSetter) { + stmts.push_back(checker->Allocator()->New(field->Key())); + return; + } + + auto *paramIdent = field->Key()->AsIdentifier()->Clone(checker->Allocator()); + paramIdent->SetTsTypeAnnotation(field->TypeAnnotation()->Clone(checker->Allocator())); + paramIdent->TypeAnnotation()->SetParent(paramIdent); + + auto *paramExpression = checker->AllocNode(paramIdent, nullptr); + paramExpression->SetRange(paramIdent->Range()); + auto *const paramVar = std::get<2>(paramScope->AddParamDecl(checker->Allocator(), paramExpression)); + + paramIdent->SetVariable(paramVar); + paramExpression->SetVariable(paramVar); + + params.push_back(paramExpression); + + auto *assignmentExpression = checker->AllocNode( + field->Key(), paramExpression, lexer::TokenType::PUNCTUATOR_SUBSTITUTION); + + assignmentExpression->SetRange({field->Start(), field->End()}); + + stmts.push_back(checker->AllocNode(assignmentExpression)); + stmts.push_back(checker->Allocator()->New(nullptr)); +} + +ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty *const field, + varbinder::ClassScope *classScope, bool isSetter, + ETSChecker *checker) +{ + auto *paramScope = checker->Allocator()->New(checker->Allocator(), classScope); + auto *functionScope = checker->Allocator()->New(checker->Allocator(), paramScope); + + functionScope->BindParamScope(paramScope); + paramScope->BindFunctionScope(functionScope); + + auto flags = ir::ModifierFlags::PUBLIC; + + ArenaVector params(checker->Allocator()->Adapter()); + ArenaVector stmts(checker->Allocator()->Adapter()); + checker->GenerateGetterSetterBody(checker, stmts, params, field, paramScope, isSetter); + + auto *body = checker->AllocNode(checker->Allocator(), std::move(stmts)); + auto funcFlags = isSetter ? ir::ScriptFunctionFlags::SETTER : ir::ScriptFunctionFlags::GETTER; + auto *const returnTypeAnn = isSetter ? nullptr : field->TypeAnnotation(); + auto *func = + checker->AllocNode(ir::FunctionSignature(nullptr, std::move(params), returnTypeAnn), body, + funcFlags, flags, true, Language(Language::Id::ETS)); + + func->SetRange(field->Range()); + func->SetScope(functionScope); + body->SetScope(functionScope); + + auto *methodIdent = field->Key()->AsIdentifier()->Clone(checker->Allocator()); + auto *decl = checker->Allocator()->New( + checker->Allocator(), field->Key()->AsIdentifier()->Name(), + field->Key()->AsIdentifier()->Variable()->Declaration()->Node()); + auto *var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + + methodIdent->SetVariable(var); + + auto *funcExpr = checker->AllocNode(func); + funcExpr->SetRange(func->Range()); + func->AddFlag(ir::ScriptFunctionFlags::METHOD); + + auto *method = checker->AllocNode(ir::MethodDefinitionKind::METHOD, methodIdent, funcExpr, + flags, checker->Allocator(), false); + + method->Id()->SetMutator(); + method->SetRange(field->Range()); + method->Function()->SetIdent(method->Id()); + method->Function()->AddModifier(method->Modifiers()); + method->SetVariable(var); + + paramScope->BindNode(func); + functionScope->BindNode(func); + + checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); + functionScope->BindName(classScope->Node()->AsClassDefinition()->InternalName()); + method->Check(checker); + + return method; +} + bool ETSChecker::TryTransformingToStaticInvoke(ir::Identifier *const ident, const Type *resolvedType) { ASSERT(ident->Parent()->IsCallExpression()); diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 6fbf90b9fc3..f09789c9267 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -719,6 +719,7 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc if (isGetterSetter && !functionOverridden) { for (auto *field : classType->Fields()) { if (field->Name() == (*it)->Name()) { + field->Declaration()->Node()->AddModifier(ir::ModifierFlags::SETTER); it = abstractsToBeImplemented.erase(it); functionOverridden = true; break; @@ -813,6 +814,7 @@ void ETSChecker::CheckClassDefinition(ir::ClassDefinition *classDef) } ValidateOverriding(classType, classDef->Start()); + TransformProperties(classType); CheckValidInheritance(classType, classDef); CheckConstFields(classType); CheckGetterSetterProperties(classType); @@ -1441,7 +1443,7 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini continue; } - if (!IsSameDeclarationType(it, found)) { + if (!IsSameDeclarationType(it, found) && !it->HasFlag(varbinder::VariableFlags::GETTER_SETTER)) { const char *targetType {}; if (it->HasFlag(varbinder::VariableFlags::PROPERTY)) { @@ -1468,6 +1470,64 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini } } +void ETSChecker::TransformProperties(ETSObjectType *classType) +{ + auto propertyList = classType->Fields(); + auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); + + for (auto *const field : propertyList) { + ASSERT(field->Declaration()->Node()->IsClassProperty()); + auto *const classProp = field->Declaration()->Node()->AsClassProperty(); + + if ((field->Declaration()->Node()->Modifiers() & ir::ModifierFlags::SETTER) == 0U) { + continue; + } + + field->AddFlag(varbinder::VariableFlags::GETTER_SETTER); + + auto *const scope = this->Scope(); + ASSERT(scope->IsClassScope()); + + ir::MethodDefinition *getter = GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), false, this); + classDef->Body().push_back(getter); + classType->AddProperty(getter->Variable()->AsLocalVariable()); + + auto *const methodScope = scope->AsClassScope()->InstanceMethodScope(); + auto name = getter->Key()->AsIdentifier()->Name(); + + auto *const decl = Allocator()->New(Allocator(), name, getter); + auto *const var = methodScope->AddDecl(Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); + + if (var == nullptr) { + auto *const prevDecl = methodScope->FindDecl(name); + ASSERT(prevDecl->IsFunctionDecl()); + prevDecl->Node()->AsMethodDefinition()->AddOverload(getter); + + if (!classProp->IsReadonly()) { + ir::MethodDefinition *const setter = + GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), true, this); + + classType->AddProperty(setter->Variable()->AsLocalVariable()); + prevDecl->Node()->AsMethodDefinition()->AddOverload(setter); + } + + getter->Function()->Id()->SetVariable( + methodScope->FindLocal(name, varbinder::ResolveBindingOptions::BINDINGS)); + continue; + } + + if (!classProp->IsReadonly()) { + ir::MethodDefinition *const setter = + GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), true, this); + + classType->AddProperty(setter->Variable()->AsLocalVariable()); + getter->AddOverload(setter); + } + getter->Function()->Id()->SetVariable(var); + } +} + void ETSChecker::CheckGetterSetterProperties(ETSObjectType *classType) { auto const checkGetterSetter = [this](varbinder::LocalVariable *var, util::StringView name) { diff --git a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp index 85ccf455cf9..97b56809eb6 100644 --- a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp +++ b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp @@ -49,14 +49,12 @@ static ir::MethodDefinition *GenerateGetterOrSetter(checker::ETSChecker *const c paramIdent->SetTsTypeAnnotation(field->TypeAnnotation()->Clone(checker->Allocator())); paramIdent->TypeAnnotation()->SetParent(paramIdent); - auto paramExpression = checker->AllocNode(paramIdent, nullptr); + auto *const paramExpression = checker->AllocNode(paramIdent, nullptr); paramExpression->SetRange(paramIdent->Range()); - const auto [_, __, param_var] = paramScope->AddParamDecl(checker->Allocator(), paramExpression); - (void)_; - (void)__; + auto *const paramVar = std::get<2>(paramScope->AddParamDecl(checker->Allocator(), paramExpression)); - paramIdent->SetVariable(param_var); - paramExpression->SetVariable(param_var); + paramIdent->SetVariable(paramVar); + paramExpression->SetVariable(paramVar); params.push_back(paramExpression); } @@ -122,6 +120,7 @@ static ir::Expression *UpdateInterfacePropertys(checker::ETSChecker *const check auto *decl = checker->Allocator()->New(checker->Allocator(), name, getter); auto var = methodScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); if (var == nullptr) { auto prevDecl = methodScope->FindDecl(name); diff --git a/ets2panda/test/runtime/ets/classGetterSetter.ets b/ets2panda/test/runtime/ets/classGetterSetter.ets new file mode 100644 index 00000000000..29b68b3979c --- /dev/null +++ b/ets2panda/test/runtime/ets/classGetterSetter.ets @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 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 inter { + get my_field (): int; + set my_field(param: int); +} + +class A implements inter { + my_field: int = 3; +} + +function main() { + let a0: A = new A(); + assert a0.my_field == 3; + + let a1: inter = new A(); + assert a1.my_field == 3; + a1.my_field = 5; + assert a1.my_field == 5; +} diff --git a/ets2panda/varbinder/variableFlags.h b/ets2panda/varbinder/variableFlags.h index 87e165e4cea..3c9fa9813a5 100644 --- a/ets2panda/varbinder/variableFlags.h +++ b/ets2panda/varbinder/variableFlags.h @@ -152,6 +152,7 @@ enum class VariableFlags : uint64_t { BUILTIN_TYPE = 1ULL << 32ULL, BOXED = 1ULL << 33ULL, + GETTER_SETTER = 1ULL << 34ULL, HOIST_VAR = HOIST | VAR, CLASS_OR_INTERFACE = CLASS | INTERFACE, -- Gitee From bffb0fa717279d5fde8e9b9e6faad2994691c726 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Wed, 3 Jan 2024 14:58:32 +0100 Subject: [PATCH 12/21] Fix ETSObjectType::Identical for function types Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8TEO3 Internal issue: #15034 Test: build, new checker tests Signed-off-by: Martin Sajti --- ets2panda/checker/types/ets/etsObjectType.cpp | 2 +- ets2panda/checker/types/ets/etsTupleType.cpp | 4 + .../compiler/ets/tuple_types_16-expected.txt | 1523 +++++++++++++++++ .../test/compiler/ets/tuple_types_16.ets | 24 + 4 files changed, 1552 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/tuple_types_16-expected.txt create mode 100644 ets2panda/test/compiler/ets/tuple_types_16.ets diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index ab6f4fb3f58..cb10fbfff5f 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -321,7 +321,7 @@ void ETSObjectType::IdenticalUptoNullability(TypeRelation *relation, Type *other return; } - if (relation->IgnoreTypeParameters() || (this == other)) { + if ((relation->IgnoreTypeParameters() && !HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) || (this == other)) { relation->Result(true); return; } diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index 08e74f9828b..92f9feb3171 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -23,8 +23,12 @@ namespace panda::es2panda::checker { void ETSTupleType::ToString(std::stringstream &ss) const { ss << "["; + for (const auto *const type : typeList_) { type->ToString(ss); + if (type != typeList_.back()) { + ss << ","; + } } if (spreadType_ != nullptr) { diff --git a/ets2panda/test/compiler/ets/tuple_types_16-expected.txt b/ets2panda/test/compiler/ets/tuple_types_16-expected.txt new file mode 100644 index 00000000000..ed97776aece --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_16-expected.txt @@ -0,0 +1,1523 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 29 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "Func0", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "FuncTuple2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "FuncTuple3", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func0", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 40 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + { + "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": 21, + "column": 10 + }, + "end": { + "line": 21, + "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": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "t2", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "FuncTuple2", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 40 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 44 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 63 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 67 + }, + "end": { + "line": 22, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 48 + }, + "end": { + "line": 22, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 48 + }, + "end": { + "line": 22, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 70 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 70 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "t3", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "FuncTuple3", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 7 + }, + "end": { + "line": 23, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 40 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 44 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 49 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 49 + }, + "end": { + "line": 23, + "column": 63 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 67 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 48 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 48 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 77 + }, + "end": { + "line": 23, + "column": 79 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 79 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 79 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 80 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 7 + }, + "end": { + "line": 23, + "column": 80 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 3 + }, + "end": { + "line": 23, + "column": 80 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/tuple_types_16.ets b/ets2panda/test/compiler/ets/tuple_types_16.ets new file mode 100644 index 00000000000..d0069f32aa1 --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_16.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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. + */ + +type Func1 = (value: String) => void +type Func0 = () => void; +type FuncTuple2 = [Func1, Func1] +type FuncTuple3 = [Func1, Func1, Func0] + +function main(): void { + let t2: FuncTuple2 = [(value: String) => {}, (value: String) => {}] + let t3: FuncTuple3 = [(value: String) => {}, (value: String) => {}, () => {}] +} -- Gitee From c222aa31d59f4ef2a053a3c7ec1072f304dfc34b Mon Sep 17 00:00:00 2001 From: Istvan Romai Date: Thu, 18 Jan 2024 08:50:07 +0100 Subject: [PATCH 13/21] [ArkTS frontend] Missing toString() method for arrays Added .toString() handling to arrays Fixes internal issue: #14766 Change-Id: Idc271497e2795456d840ad0a8c7416a99b10169c Signed-off-by: Istvan Romai --- ets2panda/checker/ETSAnalyzer.cpp | 22 ++++++++++++++++------ ets2panda/checker/ETSAnalyzer.h | 1 + ets2panda/compiler/core/ETSGen.cpp | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 885e81d2083..ad052f8e194 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1254,6 +1254,15 @@ checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::ImportExpression *expr) c UNREACHABLE(); } +checker::Type *ETSAnalyzer::SetAndAdjustType(ETSChecker *checker, ir::MemberExpression *expr, + ETSObjectType *objectType) const +{ + expr->SetObjectType(objectType); + auto [resType, resVar] = expr->ResolveObjectMember(checker); + expr->SetPropVar(resVar); + return expr->AdjustType(checker, resType); +} + checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const { ETSChecker *checker = GetETSChecker(); @@ -1284,15 +1293,16 @@ checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const return expr->AdjustType(checker, expr->CheckComputed(checker, baseType)); } - if (baseType->IsETSArrayType() && expr->Property()->AsIdentifier()->Name().Is("length")) { - return expr->AdjustType(checker, checker->GlobalIntType()); + if (baseType->IsETSArrayType()) { + if (expr->Property()->AsIdentifier()->Name().Is("length")) { + return expr->AdjustType(checker, checker->GlobalIntType()); + } + + return SetAndAdjustType(checker, expr, checker->GlobalETSObjectType()); } if (baseType->IsETSObjectType()) { - expr->SetObjectType(baseType->AsETSObjectType()); - auto [resType, resVar] = expr->ResolveObjectMember(checker); - expr->SetPropVar(resVar); - return expr->AdjustType(checker, resType); + return SetAndAdjustType(checker, expr, baseType->AsETSObjectType()); } if (baseType->IsETSEnumType() || baseType->IsETSStringEnumType()) { diff --git a/ets2panda/checker/ETSAnalyzer.h b/ets2panda/checker/ETSAnalyzer.h index db1f0439fbb..c284a6db262 100644 --- a/ets2panda/checker/ETSAnalyzer.h +++ b/ets2panda/checker/ETSAnalyzer.h @@ -44,6 +44,7 @@ private: bool isFunctionalInterface, bool isUnionTypeWithFunctionalInterface) const; checker::Type *GetReturnType(ir::CallExpression *expr, checker::Type *calleeType) const; checker::Type *GetFunctionReturnType(ir::ReturnStatement *st, ir::ScriptFunction *containingFunc) const; + checker::Type *SetAndAdjustType(ETSChecker *checker, ir::MemberExpression *expr, ETSObjectType *objectType) const; }; } // namespace panda::es2panda::checker diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index e222381e663..3b1b3792a21 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -2387,7 +2387,8 @@ void ETSGen::StringBuilderAppend(const ir::AstNode *node, VReg builder) signature = Signatures::BUILTIN_STRING_BUILDER_APPEND_BUILTIN_STRING; } - if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter()) && + if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter() || + GetAccumulatorType()->IsETSArrayType()) && !GetAccumulatorType()->IsETSStringType()) { if (Checker()->MayHaveNullValue(GetAccumulatorType())) { Label *ifnull = AllocLabel(); -- Gitee From 598d33640e7abe7f07f007a838b9e33d0df25fe9 Mon Sep 17 00:00:00 2001 From: Anna Antipina Date: Tue, 23 Jan 2024 18:04:28 +0300 Subject: [PATCH 14/21] Title: Fix update expression compiling Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8YE1Q Description: Fix boxing-unboxing flags in Update expression compiling Test: ${ARK_SOURCE_DIR}/tests/tests-u-runner/runner.sh ${ARK_SOURCE_DIR} --ets-runtime --build-dir="${ARK_BUILD_DIR}" --heap-verifier="fail_on_verification:pre:into:before_g1_concurrent:post" --timeout=30 --force-generate --test-file UpdateExpression.ets Signed-off-by: Anna Antipina - --- ets2panda/compiler/core/ETSCompiler.cpp | 9 ++++----- ets2panda/test/runtime/ets/UpdateExpression.ets | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index c95eceb6d3d..fd2476ce949 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -1189,8 +1189,11 @@ void ETSCompiler::Compile(const ir::UpdateExpression *expr) const const auto argumentUnboxingFlags = static_cast(expr->Argument()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG); + // workaround so argument_ does not get auto unboxed by lref.GetValue() + expr->Argument()->SetBoxingUnboxingFlags(ir::BoxingUnboxingFlags::NONE); + lref.GetValue(); + if (expr->IsPrefix()) { - lref.GetValue(); expr->Argument()->SetBoxingUnboxingFlags(argumentUnboxingFlags); etsg->ApplyConversion(expr->Argument(), nullptr); @@ -1209,10 +1212,6 @@ void ETSCompiler::Compile(const ir::UpdateExpression *expr) const return; } - // workaround so argument_ does not get auto unboxed by lref.GetValue() - expr->Argument()->SetBoxingUnboxingFlags(ir::BoxingUnboxingFlags::NONE); - lref.GetValue(); - compiler::RegScope rs(etsg); compiler::VReg originalValueReg = etsg->AllocReg(); etsg->StoreAccumulator(expr->Argument(), originalValueReg); diff --git a/ets2panda/test/runtime/ets/UpdateExpression.ets b/ets2panda/test/runtime/ets/UpdateExpression.ets index 4cb626c2c2e..ef41ddc5812 100644 --- a/ets2panda/test/runtime/ets/UpdateExpression.ets +++ b/ets2panda/test/runtime/ets/UpdateExpression.ets @@ -92,4 +92,10 @@ function main(): void { assert ++s as int == -32768; assert --s as int == 32767; } + + { + let b: Int[] = [1, 2] + assert ++b[1] == 3 + assert --b[1] == 2 + } } -- Gitee From 60b5e82f9f70db7baf62f1b3caac3b6960a9191f Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Wed, 17 Jan 2024 14:27:46 +0100 Subject: [PATCH 15/21] [ArkTs] Remove some redundant code parts around source handling Remove redundant parsedSources_ vector Rename sourceFilePath_ to sourceFile_ Remove unused fileName_ member Remove redundant absoluteName_ member Signed-off-by: Robert Sipka --- ets2panda/compiler/core/ETSemitter.cpp | 4 +-- ets2panda/compiler/core/emitter.cpp | 2 +- ets2panda/parser/ETSparser.cpp | 16 +++++------ ets2panda/parser/ETSparser.h | 3 +-- ets2panda/parser/program/program.h | 37 +++++++------------------- ets2panda/varbinder/ETSBinder.cpp | 2 +- 6 files changed, 21 insertions(+), 43 deletions(-) diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 04faea28907..116c3a74327 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -412,7 +412,7 @@ void ETSEmitter::GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceD } interfaceRecord.metadata->SetAccessFlags(accessFlags); - interfaceRecord.sourceFile = Context()->VarBinder()->Program()->AbsoluteName().Mutf8(); + interfaceRecord.sourceFile = Context()->VarBinder()->Program()->SourceFile().GetAbsolutePath().Mutf8(); interfaceRecord.metadata->SetAttributeValue(Signatures::EXTENDS_ATTRIBUTE, Signatures::BUILTIN_OBJECT); for (auto *it : baseType->Interfaces()) { @@ -456,7 +456,7 @@ void ETSEmitter::GenClassRecord(const ir::ClassDefinition *classDef, bool extern } classRecord.metadata->SetAccessFlags(accessFlags); - classRecord.sourceFile = Context()->VarBinder()->Program()->AbsoluteName().Mutf8(); + classRecord.sourceFile = Context()->VarBinder()->Program()->SourceFile().GetAbsolutePath().Mutf8(); auto *baseType = classDef->TsType()->AsETSObjectType(); if (baseType->SuperType() != nullptr) { diff --git a/ets2panda/compiler/core/emitter.cpp b/ets2panda/compiler/core/emitter.cpp index 6242436e0b1..bfc94a084dc 100644 --- a/ets2panda/compiler/core/emitter.cpp +++ b/ets2panda/compiler/core/emitter.cpp @@ -257,7 +257,7 @@ void FunctionEmitter::GenFunctionCatchTables(pandasm::Function *func) void FunctionEmitter::GenSourceFileDebugInfo(pandasm::Function *func) { - func->sourceFile = std::string {cg_->VarBinder()->Program()->AbsoluteName()}; + func->sourceFile = std::string {cg_->VarBinder()->Program()->SourceFile().GetAbsolutePath()}; if (!cg_->IsDebug()) { return; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 8bacd3eba68..7d587b6939d 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -190,7 +190,6 @@ void ETSParser::ParseETSGlobalScript(lexer::SourcePosition startLoc, ArenaVector for (const auto &item : items) { auto resolved = ResolveImportPath(item); resolvedParsedSources_.emplace(item, resolved); - parsedSources_.push_back(resolved); } }; // clang-format on @@ -288,7 +287,7 @@ static bool IsCompitableExtension(const std::string &extension) return extension == ".ets" || extension == ".ts"; } -void ETSParser::CollectDefaultSources() +std::vector ETSParser::CollectDefaultSources() { std::vector paths; std::vector stdlib = {"std/core", "std/math", "std/containers", @@ -319,9 +318,9 @@ void ETSParser::CollectDefaultSources() std::string filePath = path + "/" + entry->d_name; if (fileName == "Object.ets") { - parsedSources_.emplace(parsedSources_.begin(), filePath); + paths.emplace(paths.begin(), filePath); } else { - parsedSources_.emplace_back(filePath); + paths.emplace_back(filePath); } } @@ -340,13 +339,14 @@ void ETSParser::CollectDefaultSources() baseName.append(entry.path().string().substr(pos, entry.path().string().size())); if (entry.path().filename().string() == "Object.ets") { - parsedSources_.emplace(parsedSources_.begin(), baseName); + paths.emplace(paths.begin(), baseName); } else { - parsedSources_.emplace_back(baseName); + paths.emplace_back(baseName); } } } #endif + return paths; } ETSParser::ImportData ETSParser::GetImportData(const std::string &path) @@ -598,9 +598,7 @@ void ETSParser::ParseDefaultSources() ParseImportDeclarations(statements); GetContext().Status() &= ~ParserStatus::IN_DEFAULT_IMPORTS; - CollectDefaultSources(); - - ParseSources(parsedSources_, true); + ParseSources(CollectDefaultSources(), true); } void ETSParser::ParseSource(const SourceFile &sourceFile) diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index ca5fefcf6dd..69b26abe93e 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -124,7 +124,7 @@ private: static int NFTWCallBack(const char *fpath, const struct stat * /*unused*/, int tflag, struct FTW * /*unused*/); #endif void ParseTopLevelDeclaration(ArenaVector &statements); - void CollectDefaultSources(); + std::vector CollectDefaultSources(); void CollectUserSourcesFromIndex(const std::string &path, const std::string &resolvedPath, std::vector &userPaths); std::string ResolveImportPath(const std::string &path); @@ -382,7 +382,6 @@ public: private: parser::Program *globalProgram_; - std::vector parsedSources_; std::vector insertingNodes_ {}; std::unordered_map resolvedParsedSources_; }; diff --git a/ets2panda/parser/program/program.h b/ets2panda/parser/program/program.h index b1a29f3b1ff..a289ac73b60 100644 --- a/ets2panda/parser/program/program.h +++ b/ets2panda/parser/program/program.h @@ -94,24 +94,19 @@ public: return sourceCode_; } - util::StringView SourceFilePath() const + const util::StringView &SourceFilePath() const { - return sourceFilePath_.GetPath(); + return sourceFile_.GetPath(); } - util::StringView SourceFileFolder() const + const util::Path &SourceFile() const { - return sourceFileFolder_; + return sourceFile_; } - util::StringView FileName() const - { - return fileName_; - } - - util::StringView AbsoluteName() const + util::StringView SourceFileFolder() const { - return absoluteName_; + return sourceFileFolder_; } util::StringView ResolvedFilePath() const @@ -163,17 +158,15 @@ public: const util::StringView &sourceFileFolder) { sourceCode_ = sourceCode; - sourceFilePath_ = util::Path(sourceFilePath, Allocator()); + sourceFile_ = util::Path(sourceFilePath, Allocator()); sourceFileFolder_ = sourceFileFolder; - absoluteName_ = util::UString(os::GetAbsolutePath(sourceFilePath.Utf8()), Allocator()).View(); } void SetSource(const panda::es2panda::SourceFile &sourceFile) { sourceCode_ = util::UString(sourceFile.source, Allocator()).View(); - sourceFilePath_ = util::Path(sourceFile.filePath, Allocator()); + sourceFile_ = util::Path(sourceFile.filePath, Allocator()); sourceFileFolder_ = util::UString(sourceFile.fileFolder, Allocator()).View(); - absoluteName_ = sourceFilePath_.GetAbsolutePath(); resolvedFilePath_ = util::UString(sourceFile.resolvedPath, Allocator()).View(); } @@ -187,16 +180,6 @@ public: packageName_ = packageName; } - void SetFileName(util::StringView fileName) - { - fileName_ = util::UString(fileName, Allocator()).View(); - } - - void SetAbsoluteName(util::StringView absouleName) - { - absoluteName_ = util::UString(absouleName, Allocator()).View(); - } - const bool &IsEntryPoint() const { return entryPoint_; @@ -225,11 +208,9 @@ private: ir::BlockStatement *ast_ {}; ir::ClassDefinition *globalClass_ {}; util::StringView sourceCode_ {}; - util::Path sourceFilePath_ {}; + util::Path sourceFile_ {}; util::StringView sourceFileFolder_ {}; util::StringView packageName_ {}; - util::StringView fileName_ {}; - util::StringView absoluteName_ {}; util::StringView resolvedFilePath_ {}; ExternalSource externalSources_; ScriptKind kind_ {}; diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 3df41bd5eb4..06d08f35536 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -1036,7 +1036,7 @@ void ETSBinder::BuildETSNewClassInstanceExpression(ir::ETSNewClassInstanceExpres void ETSBinder::BuildImportDeclaration(ir::ETSImportDeclaration *decl) { - if (decl->Source()->Str() == Program()->AbsoluteName()) { + if (decl->Source()->Str() == Program()->SourceFile().GetAbsolutePath()) { return; } -- Gitee From 3bb01beedaefb70bca1d64e1ab7fe015ae1d7073 Mon Sep 17 00:00:00 2001 From: peterseres Date: Tue, 19 Dec 2023 16:09:13 +0100 Subject: [PATCH 16/21] Change-Id: Ib692ef522d53042e347e9ba79d11f7a82a57f709 Title: Add new ninja target Description: es2panda-pre-test to run parser, runtime, ets-cts, function test cases clang-tidy and clang format. This commit contain the Python script. Fixes internal issue #13849 Testing: This commit and issue doesn't contains test cases because it's a Python script and a little modification in cmake. Signed-off-by: Peter Seres --- ets2panda/scripts/es2panda_pre_test.py | 184 +++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 ets2panda/scripts/es2panda_pre_test.py diff --git a/ets2panda/scripts/es2panda_pre_test.py b/ets2panda/scripts/es2panda_pre_test.py new file mode 100644 index 00000000000..217f955c949 --- /dev/null +++ b/ets2panda/scripts/es2panda_pre_test.py @@ -0,0 +1,184 @@ +# +# Copyright (c) 2022-2023 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 re +import argparse +import subprocess +import time + +from enum import Enum + + +class TestTypes(Enum): + NORMAL = 0 + CLANGTIDY = 1 + CLANGFORMAT = 2 + + +def print_summary(log_lines, searched_words, end_word=""): + was_any_log = False + should_print = False + + for line in log_lines: + for word in searched_words: + if any(word in line for word in searched_words): + should_print = True + was_any_log = True + + if end_word and end_word in line: + should_print=False + + if should_print: + print(line.strip()) + + if not was_any_log: + print("Problem occourd: ", was_any_log) + + print(f"\n") + + +def print_progress_to_console(line, test_type, pattern_progress_bar, pattern_clang_tidy, pattern_clang_format): + match test_type: + case TestTypes.NORMAL: + match = pattern_progress_bar.search(line) + if match: + print(line, end='') + case TestTypes.CLANGTIDY: + match = re.match(pattern_clang_tidy, line) + if match: + print(f"[{match.group(1)}/{match.group(2)}] {match.group(3)}") + case TestTypes.CLANGFORMAT: + match = re.match(pattern_clang_format, line) + if match: + print(f"[{match.group(1)}/{match.group(2)}] Clang-format: {match.group(3)} \n") + + +def test_process_runner(runtime_process, test_type): + pattern_progress_bar = re.compile(r'\b(\d{1,3}%)\|(.+?)\| (\d+)/(\d+) \[\d{2}:\d{2}<\d{2}:\d{2},\s*\d+\.\d+it/s\]$') + pattern_clang_tidy = re.compile(r"^\[(\d+)\/(\d+)\]\s+Done\s+clang-tidy:\s+.*?\/es2panda\/(.+\.cpp)$") + pattern_clang_format = re.compile(r"\[(\d+)/(\d+)\]\s+Running clang-format: (.+)") + + should_print = False + log_lines = [] + + for line in runtime_process.stdout: + log_lines.append(line) + print_progress_to_console(line, test_type, pattern_progress_bar, pattern_clang_tidy, pattern_clang_format) + + return log_lines + + +def command_runner(command_to_run, test_type): + try: + process = subprocess.Popen(command_to_run, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + process_log = test_process_runner(process, test_type) + + except subprocess.CalledProcessError as e: + process_log = str(e) + print("An error occurred during processing:", process_log) + + except Exception as e: + process_log = str(e) + print("An error occurred during processing:", process_log) + + return process_log + +def print_summary_to_file(log, file_name, word_for_success, word_for_fail, test_name): + has_to_write_out_the_log = False + + for line in log: + if word_for_fail in line: + has_to_write_out_the_log = True + elif word_for_success in line: + print(line) + + if has_to_write_out_the_log: + with open(file_name, "w") as file: + for line in log: + file.write(line) + + print(f"{test_name} is faild. Please check the log file: {file_name}") + + +def main(): + parser = argparse.ArgumentParser(description="Program description.") + parser.add_argument("--working-dir", required=False, help="Path to the working dir") + parser.add_argument("--build-root", required=False, help="Path to the working dir") + + args = parser.parse_args() + print(f"Argumentum: Working dir: {args.working_dir}") + print(f"Argumentum: Build root: {args.build_root}") + + command_helper = ["python3", f"{args.working_dir}/tests/tests-u-runner/main.py", "--build-dir", f"{args.build_root}", + "--processes", "all", "--show-progress", "--force-generate" + ] + + # Run Parser tests + parser_test_run_command = command_helper + ["--parser" ] + + parser_log = command_runner(parser_test_run_command, TestTypes.NORMAL) + + # Run Runtime tests + runtime_test_run_command = command_helper + ["--ets-runtime", "--timeout", "60"] + + runtime_log = command_runner(runtime_test_run_command, TestTypes.NORMAL) + + # Functional tests + functional_test_run_command = command_helper + ["--ets-func-tests"] + + functional_log = command_runner(functional_test_run_command, TestTypes.NORMAL) + + # Run Ets-cts test + ets_cts_test_run_command = command_helper + ["--ets-cts"] + + ets_cts_log = command_runner(ets_cts_test_run_command, TestTypes.NORMAL) + + # Run Clang-format + clang_format_run_command = ["ninja", "-C", f"{args.build_root}", "code-style-check"] + + clang_format_log = command_runner(clang_format_run_command, TestTypes.CLANGFORMAT) + + # Run Clang-tidy + clang_tidy_run_command = [f"{args.working_dir}/scripts/clang-tidy/clang_tidy_check.py", "--filename-filter=ets2panda", + f"{args.working_dir}", f"{args.build_root}" + ] + + clang_tidy_log = command_runner(clang_tidy_run_command,TestTypes.CLANGTIDY) + + print("\n\nTest results: \n") + + print(f"\nParser test results:") + print_summary(parser_log, ["New failures at", "Summary("]) + + print(f"\nRuntime test results:") + print_summary(runtime_log, ["New failures at", "Summary("]) + + print(f"\nFunctional test results:") + print_summary(functional_log, ["Summary("]) + + print(f"\nEts-cts test results:") + print_summary(ets_cts_log, ["New failures at", "Summary("]) + + print(f"\nClang-format results:") + print_summary_to_file(clang_format_log, "clang-format.log", "Clang-format was passed successfully!", "Failed:", "Clang-format") + + print(f"\nClang-tidy results:") + print_summary(clang_tidy_log,["Clang-tidy was passed successfully!", "Failed:"], "Done clang-tidy:") + + +if __name__ == "__main__": + start_time = time.perf_counter() + main() + end_time = time.perf_counter() + print("Runtime: ", (end_time - start_time)/60, " minute" ) -- Gitee From c50a1cafe7a3ca75d35247597b3fe50b956b297f Mon Sep 17 00:00:00 2001 From: Soma Simon Date: Fri, 19 Jan 2024 11:24:35 +0100 Subject: [PATCH 17/21] [ArkTS frontend] Fix call static interface method with type alias Fix condition when creating the searchflag, to find the property Fixed internal issue #14507 Change-Id: Iba1ae4c08e48752f182c21bbb9c3fcc2425bf69d Signed-off-by: Soma Simon --- ets2panda/checker/ets/object.cpp | 6 ++- .../ets/call_static_with_type_alias.ets | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/call_static_with_type_alias.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index f09789c9267..8c06deccbc4 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1250,8 +1250,10 @@ PropertySearchFlags ETSChecker::GetSearchFlags(const ir::MemberExpression *const { auto searchFlag = GetInitialSearchFlags(memberExpr); searchFlag |= PropertySearchFlags::SEARCH_IN_BASE | PropertySearchFlags::SEARCH_IN_INTERFACES; - - if (targetRef != nullptr && targetRef->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE)) { + if (targetRef != nullptr && + (targetRef->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE) || + (targetRef->HasFlag(varbinder::VariableFlags::TYPE_ALIAS) && + targetRef->TsType()->Variable()->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE)))) { searchFlag &= ~(PropertySearchFlags::SEARCH_INSTANCE); } else if (memberExpr->Object()->IsThisExpression() || (memberExpr->Object()->IsIdentifier() && memberExpr->ObjType()->GetDeclNode() != nullptr && diff --git a/ets2panda/test/runtime/ets/call_static_with_type_alias.ets b/ets2panda/test/runtime/ets/call_static_with_type_alias.ets new file mode 100644 index 00000000000..0a86ff562a0 --- /dev/null +++ b/ets2panda/test/runtime/ets/call_static_with_type_alias.ets @@ -0,0 +1,52 @@ +/* + * 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 I { + static inc(p: number): number { + return p + 1; + } +} + +class C1 { + static inc2(p: number): number { + return p + 2; + } +} + +class C2 implements I { +} + +type TI = I; +type TTI = TI; + +type TC1 = C1; +type TTC1 = TC1; + +type TC2 = C2; +type TTC2 = TC2; + +function main(): void { + assert I.inc(20) == 21; + assert TI.inc(20) == 21; + assert TTI.inc(20) == 21; + + assert C1.inc2(20) == 22; + assert TC1.inc2(20) == 22; + assert TTC1.inc2(20) == 22; + + assert C2.inc(20) == 21; + assert TC2.inc(20) == 21; + assert TTC2.inc(20) == 21; +} -- Gitee From 6a10ca88a160c726a2e2844544470a34090ac534 Mon Sep 17 00:00:00 2001 From: aleksisch Date: Fri, 12 Jan 2024 14:52:45 +0300 Subject: [PATCH 18/21] make field & register traverse deterministic Signed-off-by: aleksisch --- ets2panda/checker/types/ets/etsObjectType.cpp | 7 ++-- ets2panda/checker/types/ets/etsObjectType.h | 2 +- ets2panda/compiler/core/envScope.cpp | 2 +- ets2panda/compiler/core/regScope.cpp | 4 +- .../lowering/scopesInit/savedBindingsCtx.cpp | 2 +- ets2panda/parser/ETSparser.cpp | 39 ++++++++++++------- ets2panda/parser/ETSparser.h | 1 + ets2panda/varbinder/scope.h | 7 ++++ 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index cb10fbfff5f..2a7695b0539 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -272,12 +272,11 @@ std::vector ETSObjectType::ForeignProperties() return foreignProps; } -std::unordered_map ETSObjectType::CollectAllProperties() const +ArenaMap ETSObjectType::CollectAllProperties() const { - std::unordered_map propMap; + ArenaMap propMap(allocator_->Adapter()); EnsurePropertiesInstantiated(); - propMap.reserve(properties_.size()); - Iterate([&propMap](const varbinder::LocalVariable *var) { propMap.insert({var->Name(), var}); }); + Iterate([&propMap](const varbinder::LocalVariable *var) { propMap.emplace(var->Name(), var); }); return propMap; } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 464754a587b..ff6cca7a94d 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -545,7 +545,7 @@ private: propertiesInstantiated_ = true; } } - std::unordered_map CollectAllProperties() const; + ArenaMap CollectAllProperties() const; void IdenticalUptoNullability(TypeRelation *relation, Type *other); bool CastWideningNarrowing(TypeRelation *relation, Type *target, TypeFlag unboxFlags, TypeFlag wideningFlags, TypeFlag narrowingFlags); diff --git a/ets2panda/compiler/core/envScope.cpp b/ets2panda/compiler/core/envScope.cpp index 7adc372f8f2..cb870fdce8a 100644 --- a/ets2panda/compiler/core/envScope.cpp +++ b/ets2panda/compiler/core/envScope.cpp @@ -60,7 +60,7 @@ void LoopEnvScope::CopyBindings(PandaGen *pg, varbinder::VariableScope *scope, v ASSERT(scope->NeedLexEnv()); - for (const auto &[_, variable] : scope_->Bindings()) { + for (const auto &[_, variable] : scope_->OrderedBindings(pg_->Allocator())) { (void)_; if (!variable->HasFlag(flag)) { continue; diff --git a/ets2panda/compiler/core/regScope.cpp b/ets2panda/compiler/core/regScope.cpp index 6109ffd80f0..9f7a6d7e41b 100644 --- a/ets2panda/compiler/core/regScope.cpp +++ b/ets2panda/compiler/core/regScope.cpp @@ -52,7 +52,7 @@ LocalRegScope::LocalRegScope(CodeGen *cg, varbinder::Scope *scope) : RegScope(cg prevScope_ = cg_->scope_; cg_->scope_ = scope; - for (const auto &[_, var] : scope->Bindings()) { + for (const auto &[_, var] : scope->OrderedBindings(cg_->Allocator())) { (void)_; if (!var->LexicalBound() && var->IsLocalVariable()) { var->AsLocalVariable()->BindVReg(cg_->AllocReg()); @@ -99,7 +99,7 @@ void FunctionRegScope::InitializeParams(const StoreParamCb &cb) paramReg++; } - for (const auto it : funcScope->Bindings()) { + for (const auto it : funcScope->OrderedBindings(cg_->Allocator())) { auto *const var = std::get<1>(it); if (var->Declaration()->IsParameterDecl() || var->Declaration()->IsTypeAliasDecl()) { continue; diff --git a/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp b/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp index 72bab547bda..c8197d78c21 100644 --- a/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp +++ b/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp @@ -26,7 +26,7 @@ void ImportDeclarationContext::BindImportDecl(ir::ImportDeclaration *importDecl) { varbinder::ModuleScope::ImportDeclList declList(Allocator()->Adapter()); - for (const auto &[name, variable] : VarBinder()->GetScope()->Bindings()) { + for (const auto &[name, variable] : VarBinder()->GetScope()->OrderedBindings(Allocator())) { if (SavedBindings().find(name) != SavedBindings().end()) { continue; } diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 7d587b6939d..0634e8bdfa9 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -287,12 +287,9 @@ static bool IsCompitableExtension(const std::string &extension) return extension == ".ets" || extension == ".ts"; } -std::vector ETSParser::CollectDefaultSources() +std::vector ETSParser::UnixApiDefaultSources([[maybe_unused]] const std::vector &stdlib) { std::vector paths; - std::vector stdlib = {"std/core", "std/math", "std/containers", - "std/time", "std/interop/js", "escompat"}; - #ifdef USE_UNIX_SYSCALL for (auto const &path : stdlib) { auto resolvedPath = ResolveImportPath(path); @@ -303,6 +300,7 @@ std::vector ETSParser::CollectDefaultSources() } struct dirent *entry; + std::set orderedFiles; while ((entry = readdir(dir)) != nullptr) { if (entry->d_type != DT_REG) { continue; @@ -310,7 +308,6 @@ std::vector ETSParser::CollectDefaultSources() std::string fileName = entry->d_name; std::string::size_type pos = fileName.find_last_of('.'); - if (pos == std::string::npos || !IsCompitableExtension(fileName.substr(pos))) { continue; } @@ -320,14 +317,27 @@ std::vector ETSParser::CollectDefaultSources() if (fileName == "Object.ets") { paths.emplace(paths.begin(), filePath); } else { - paths.emplace_back(filePath); + orderedFiles.emplace(filePath); } } - + paths.insert(paths.end(), orderedFiles.begin(), orderedFiles.end()); closedir(dir); } +#endif + return paths; +} + +std::vector ETSParser::CollectDefaultSources() +{ + std::vector stdlib = {"std/core", "std/math", "std/containers", + "std/time", "std/interop/js", "escompat"}; + +#ifdef USE_UNIX_SYSCALL + return UnixApiDefaultSources(stdlib); #else + std::vector paths; for (auto const &path : stdlib) { + std::set orderedFiles; for (auto const &entry : fs::directory_iterator(ResolveImportPath(path))) { if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { continue; @@ -341,12 +351,13 @@ std::vector ETSParser::CollectDefaultSources() if (entry.path().filename().string() == "Object.ets") { paths.emplace(paths.begin(), baseName); } else { - paths.emplace_back(baseName); + orderedFiles.emplace(baseName); } } + paths.insert(paths.end(), orderedFiles.begin(), orderedFiles.end()); } -#endif return paths; +#endif } ETSParser::ImportData ETSParser::GetImportData(const std::string &path) @@ -464,7 +475,7 @@ void ETSParser::CollectUserSourcesFromIndex([[maybe_unused]] const std::string & #ifdef USE_UNIX_SYSCALL DIR *dir = opendir(resolvedPath.c_str()); bool isIndex = false; - std::vector tmpPaths; + std::set tmpPaths; if (dir == nullptr) { ThrowSyntaxError({"Cannot open folder: ", resolvedPath}); @@ -489,9 +500,9 @@ void ETSParser::CollectUserSourcesFromIndex([[maybe_unused]] const std::string & isIndex = true; break; } else if (fileName == "Object.ets") { - tmpPaths.emplace(userPaths.begin(), filePath); + userPaths.emplace(userPaths.begin(), filePath); } else { - tmpPaths.emplace_back(filePath); + tmpPaths.insert(filePath); } } @@ -531,6 +542,7 @@ std::tuple, bool> ETSParser::CollectUserSources(const s } else if (fs::exists(resolvedPath + "/index.ts")) { userPaths.emplace_back(path + "/index.ts"); } else { + std::set orderedFiles; for (auto const &entry : fs::directory_iterator(resolvedPath)) { if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { continue; @@ -540,8 +552,9 @@ std::tuple, bool> ETSParser::CollectUserSources(const s std::size_t pos = entry.path().string().find_last_of(panda::os::file::File::GetPathDelim()); baseName.append(entry.path().string().substr(pos, entry.path().string().size())); - userPaths.emplace_back(baseName); + orderedFiles.emplace(baseName); } + userPaths.insert(userPaths.begin(), orderedFiles.begin(), orderedFiles.end()); } #endif return {userPaths, false}; diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 69b26abe93e..f23f3442dc1 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -124,6 +124,7 @@ private: static int NFTWCallBack(const char *fpath, const struct stat * /*unused*/, int tflag, struct FTW * /*unused*/); #endif void ParseTopLevelDeclaration(ArenaVector &statements); + std::vector UnixApiDefaultSources(const std::vector &stdlib); std::vector CollectDefaultSources(); void CollectUserSourcesFromIndex(const std::string &path, const std::string &resolvedPath, std::vector &userPaths); diff --git a/ets2panda/varbinder/scope.h b/ets2panda/varbinder/scope.h index b53625d68e4..f1030ca4089 100644 --- a/ets2panda/varbinder/scope.h +++ b/ets2panda/varbinder/scope.h @@ -245,6 +245,13 @@ public: return bindings_; } + ArenaMap OrderedBindings(ArenaAllocator *allocator) const + { + ArenaMap result(allocator->Adapter()); + result.insert(bindings_.begin(), bindings_.end()); + return result; + } + virtual Variable *AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension) = 0; -- Gitee From 0fb479ac658e4d60c0873dbfdfd1d4919546726f Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Thu, 11 Jan 2024 10:49:37 +0100 Subject: [PATCH 19/21] 'type' is hard keyword Change 'type' to soft keyword. Fixes internal issue #13941 Change-Id: Ia3a52c904e2ccc12b0c37448aa1f5eddf59a93e4 Signed-off-by: Vivien Voros --- ets2panda/lexer/scripts/keywords.yaml | 3 +- ets2panda/lexer/token/token.cpp | 1 + ets2panda/parser/ETSparser.cpp | 15 +- ets2panda/parser/ETSparser.h | 1 + .../parser/ets/user_defined_22-expected.txt | 830 ++++++++++++++++++ ets2panda/test/parser/ets/user_defined_22.ets | 22 + .../parser/ets/user_defined_23-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_23.ets | 18 + .../parser/ets/user_defined_24-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_24.ets | 18 + .../parser/ets/user_defined_25-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_25.ets | 18 + 12 files changed, 923 insertions(+), 6 deletions(-) create mode 100644 ets2panda/test/parser/ets/user_defined_22-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_22.ets create mode 100644 ets2panda/test/parser/ets/user_defined_23-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_23.ets create mode 100644 ets2panda/test/parser/ets/user_defined_24-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_24.ets create mode 100644 ets2panda/test/parser/ets/user_defined_25-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_25.ets diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index e437a74c86e..d6f8a5540f9 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -430,8 +430,7 @@ keywords: - name: 'type' token: KEYW_TYPE - keyword: [ets] - keyword_like: [js, ts] + keyword_like: [ets, js, ts] - name: 'typeof' token: KEYW_TYPEOF diff --git a/ets2panda/lexer/token/token.cpp b/ets2panda/lexer/token/token.cpp index cf4e3e30337..1135f0f0a3b 100644 --- a/ets2panda/lexer/token/token.cpp +++ b/ets2panda/lexer/token/token.cpp @@ -107,6 +107,7 @@ bool Token::IsDefinableTypeName() const case TokenType::KEYW_INT: case TokenType::KEYW_CHAR: case TokenType::KEYW_LONG: + case TokenType::KEYW_TYPE: return true; default: return false; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 0634e8bdfa9..b91b25f8162 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -741,6 +741,11 @@ void ETSParser::ParseTopLevelNextTokenDefault(ArenaVector &stat return; } + if (IsTypeKeyword()) { + ParseTopLevelType(statements, defaultExport, currentPos, &ETSParser::ParseTypeAliasDeclaration); + return; + } + if (initFunction != nullptr) { if (auto *const statement = ParseTopLevelStatement(); statement != nullptr) { statement->SetParent(initFunction->Body()); @@ -809,10 +814,6 @@ lexer::SourcePosition ETSParser::ParseTopLevelNextTokenResolution(ArenaVectorGetToken().KeywordType() == lexer::TokenType::KEYW_STRUCT); } +bool ETSParser::IsTypeKeyword() const +{ + return (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && + Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_TYPE); +} + void ETSParser::ValidateInstanceOfExpression(ir::Expression *expr) { ValidateGroupedExpression(expr); diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index f23f3442dc1..4b4afef5112 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -325,6 +325,7 @@ private: } bool IsStructKeyword() const; + bool IsTypeKeyword() const; // NOLINTNEXTLINE(google-default-arguments) ir::Expression *ParseExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; diff --git a/ets2panda/test/parser/ets/user_defined_22-expected.txt b/ets2panda/test/parser/ets/user_defined_22-expected.txt new file mode 100644 index 00000000000..6b33f241435 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_22-expected.txt @@ -0,0 +1,830 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "type", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 7 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + { + "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": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 7 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "property": { + "type": "Identifier", + "name": "type", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "", + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 14 + } + } + } + ], + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 11 + } + } + } + ], + "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 + } + } +} diff --git a/ets2panda/test/parser/ets/user_defined_22.ets b/ets2panda/test/parser/ets/user_defined_22.ets new file mode 100644 index 00000000000..75ace1622ec --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_22.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 A { + type: number = 0 +} +type B = string +let a = new A +a.type +let b: B = "" diff --git a/ets2panda/test/parser/ets/user_defined_23-expected.txt b/ets2panda/test/parser/ets/user_defined_23-expected.txt new file mode 100644 index 00000000000..6fb05ccbb52 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_23-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_23.ets:16:7] diff --git a/ets2panda/test/parser/ets/user_defined_23.ets b/ets2panda/test/parser/ets/user_defined_23.ets new file mode 100644 index 00000000000..43577e643e5 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_23.ets @@ -0,0 +1,18 @@ +/* + * 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 type { + type: number = 0 +} diff --git a/ets2panda/test/parser/ets/user_defined_24-expected.txt b/ets2panda/test/parser/ets/user_defined_24-expected.txt new file mode 100644 index 00000000000..458482bb15f --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_24-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_24.ets:16:8] diff --git a/ets2panda/test/parser/ets/user_defined_24.ets b/ets2panda/test/parser/ets/user_defined_24.ets new file mode 100644 index 00000000000..c52cb720a56 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_24.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +struct type { + type: number = 0 +} diff --git a/ets2panda/test/parser/ets/user_defined_25-expected.txt b/ets2panda/test/parser/ets/user_defined_25-expected.txt new file mode 100644 index 00000000000..105c5ae7614 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_25-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_25.ets:16:11] diff --git a/ets2panda/test/parser/ets/user_defined_25.ets b/ets2panda/test/parser/ets/user_defined_25.ets new file mode 100644 index 00000000000..db7c0eb73b4 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_25.ets @@ -0,0 +1,18 @@ +/* + * 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 type { + type: number = 0 +} -- Gitee From 036c639fa7287de4ca75efda0756048b419abd5e Mon Sep 17 00:00:00 2001 From: Tatiana Date: Mon, 29 Jan 2024 14:44:43 +0300 Subject: [PATCH 20/21] Turn on ForLoopCorrectlyInitialized check in AST Verifier Fix internal issue #15239 Signed-off-by: Tatiana --- ets2panda/util/options.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ets2panda/util/options.cpp b/ets2panda/util/options.cpp index db863804e2a..7961ee7c144 100644 --- a/ets2panda/util/options.cpp +++ b/ets2panda/util/options.cpp @@ -181,8 +181,22 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg genStdLib("gen-stdlib", false, "Gen standard library"); panda::PandArg plugins("plugins", "", "Plugins"); panda::PandArg skipPhases("skip-phases", "", "Phases to skip"); - panda::PandArg verifierWarnings("verifier-warnings", "", "Show warnings form verifier"); - panda::PandArg verifierErrors("verifier-errors", "", "Show warnings form verifier"); + panda::PandArg verifierWarnings( + "verifier-warnings", "", + "Print errors and continue compilation if AST tree is incorrect. " + "Possible values: " + "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll," + "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll," + "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll," + "ImportExportAccessValid"); + panda::PandArg verifierErrors( + "verifier-errors", "ForLoopCorrectlyInitializedForAll", + "Print errors and stop compilation if AST tree is incorrect. " + "Possible values: " + "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll," + "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll," + "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll," + "ImportExportAccessValid"); panda::PandArg dumpBeforePhases("dump-before-phases", "", "Generate program dump before running phases in the list"); panda::PandArg dumpEtsSrcBeforePhases( -- Gitee From ae94b6e9e839988ec76846b83b6ec41459f8f138 Mon Sep 17 00:00:00 2001 From: Otto Eotvos Date: Fri, 19 Jan 2024 13:03:55 +0100 Subject: [PATCH 21/21] [ArkTS] Rework assignment related error messages Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8L5TI Testing: All required pre-merge tests have passed. Results are available in the ggwatcher. Description: Error messages have been rewritten to show the types, which cannot be assigned to each other. Also some modification to the toString of Objects. Fixes internal issue #14547 Change-Id: Ib3d826662e07c3f973a441b4db725ac1b643637b Signed-off-by: Soma Simon Signed-off-by: Otto Eotvos --- ets2panda/checker/ETSAnalyzer.cpp | 84 +- ets2panda/checker/ETSAnalyzer.h | 2 + ets2panda/checker/ETSchecker.h | 1 + ets2panda/checker/ets/function.cpp | 27 +- ets2panda/checker/ets/helpers.cpp | 42 +- ets2panda/checker/ets/typeRelationContext.cpp | 5 +- ets2panda/checker/ets/typeRelationContext.h | 2 + ets2panda/checker/types/ets/etsArrayType.cpp | 5 + ets2panda/checker/types/ets/etsObjectType.cpp | 30 +- ets2panda/checker/types/ets/etsObjectType.h | 2 +- ets2panda/checker/types/ets/etsTupleType.cpp | 9 +- .../compiler/ets/FunctionType1-expected.txt | 2 +- .../compiler/ets/FunctionType3-expected.txt | 2 +- ...rsion_Double-to-Int_typeerror-expected.txt | 2 +- ...le-to-Int_w_try_stmt_typerror-expected.txt | 2 +- ...rsion_Int-to-Double_typeerror-expected.txt | 2 +- ...ntext_Int-to-Double_typeerror-expected.txt | 2 +- .../ets/etsObjectToString0-expected.txt | 584 ++++++++++++ .../test/compiler/ets/etsObjectToString0.ets | 21 + .../ets/etsObjectToString1-expected.txt | 640 +++++++++++++ .../test/compiler/ets/etsObjectToString1.ets | 21 + .../ets/etsObjectToString2-expected.txt | 668 +++++++++++++ .../test/compiler/ets/etsObjectToString2.ets | 21 + .../ets/etsObjectToString3-expected.txt | 890 ++++++++++++++++++ .../test/compiler/ets/etsObjectToString3.ets | 21 + .../ets/etsObjectToString4-expected.txt | 468 +++++++++ .../test/compiler/ets/etsObjectToString4.ets | 18 + .../ets/etsObjectToString5-expected.txt | 848 +++++++++++++++++ .../test/compiler/ets/etsObjectToString5.ets | 20 + .../ets/generic_function_call_2-expected.txt | 2 +- ...cs_primitive_type_param_neg_1-expected.txt | 2 +- .../ets/identifierReference10-expected.txt | 2 +- .../ets/identifierReference2-expected.txt | 2 +- .../ets/identifierReference3-expected.txt | 2 +- .../inferTypeOfArrayNegative3-expected.txt | 2 +- ...utBlockStatementDifferentType-expected.txt | 2 +- ...tementDifferentTypeInfunction-expected.txt | 2 +- .../compiler/ets/lambdaFunction3-expected.txt | 2 +- .../compiler/ets/lambdaFunction5-expected.txt | 2 +- ...ullableTypeParamToNonNullable-expected.txt | 2 +- ...ctLiteralPrimitiveContextType-expected.txt | 2 +- .../objectLiteralWrongValueType-expected.txt | 2 +- .../switchStatementWrongBoxing-expected.txt | 2 +- .../ets/tuple_types_10_neg-expected.txt | 2 +- .../ets/tuple_types_11_neg-expected.txt | 2 +- .../ets/tuple_types_6_neg-expected.txt | 2 +- .../ets/tuple_types_9_neg-expected.txt | 2 +- .../compiler/ets/union_types_4-expected.txt | 2 +- .../async_func_return_type_bad-expected.txt | 2 +- ...ic_class_ctor_decl_import_bad-expected.txt | 2 +- ...class_field_decl_import_bad_1-expected.txt | 2 +- ...class_field_decl_import_bad_2-expected.txt | 2 +- ...lass_method_decl_import_bad_1-expected.txt | 2 +- ...lass_method_decl_import_bad_2-expected.txt | 2 +- .../dynamic_func_decl_import_bad-expected.txt | 2 +- ets2panda/test/parser/ets/enum8-expected.txt | 2 +- ...unction_implicit_return_type2-expected.txt | 2 +- ...unction_implicit_return_type3-expected.txt | 2 +- ...unction_implicit_return_type6-expected.txt | 2 +- ...unction_implicit_return_type7-expected.txt | 2 +- .../ets/n_arrayHoldingNullValue-expected.txt | 2 +- ...ableFromFunctionToNonNullable-expected.txt | 2 +- ...ableFromMethodToNullableParam-expected.txt | 2 +- ...n_assignNullableToNonNullable-expected.txt | 2 +- ...ignNullableToNonNullableArray-expected.txt | 2 +- ...ullableToNonNullableTypeAlias-expected.txt | 2 +- ...callFunctionWithNullableParam-expected.txt | 2 +- ...erfaceMethodWithNullableParam-expected.txt | 2 +- ...n_callMethodWithNullableParam-expected.txt | 2 +- .../ets/n_returnNullFromFunction-expected.txt | 2 +- .../ets/n_returnNullFromMethod-expected.txt | 2 +- .../n_returnNullableFromFunction-expected.txt | 2 +- .../n_returnNullableFromMethod-expected.txt | 2 +- .../test/parser/ets/null_invalid-expected.txt | 2 +- .../test/parser/ets/switch_enum2-expected.txt | 2 +- ...eadonly_member_different_enum-expected.txt | 2 +- ...donly_member_different_enum_2-expected.txt | 2 +- .../test/unit/public/es2panda_public_test.cpp | 2 +- 78 files changed, 4421 insertions(+), 118 deletions(-) create mode 100644 ets2panda/test/compiler/ets/etsObjectToString0-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString0.ets create mode 100644 ets2panda/test/compiler/ets/etsObjectToString1-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString1.ets create mode 100644 ets2panda/test/compiler/ets/etsObjectToString2-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString2.ets create mode 100644 ets2panda/test/compiler/ets/etsObjectToString3-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString3.ets create mode 100644 ets2panda/test/compiler/ets/etsObjectToString4-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString4.ets create mode 100644 ets2panda/test/compiler/ets/etsObjectToString5-expected.txt create mode 100644 ets2panda/test/compiler/ets/etsObjectToString5.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index ad052f8e194..810934b8e53 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -821,18 +821,30 @@ checker::Type *ETSAnalyzer::Check(ir::AssignmentExpression *expr) const checker->ThrowTypeError("Setting the length of an array is not permitted", expr->Left()->Start()); } - if (expr->Left()->IsIdentifier()) { - expr->target_ = expr->Left()->AsIdentifier()->Variable(); - } else { - expr->target_ = expr->Left()->AsMemberExpression()->PropVar(); - } + expr->target_ = expr->Left()->IsIdentifier() ? expr->Left()->AsIdentifier()->Variable() + : expr->Left()->AsMemberExpression()->PropVar(); if (expr->target_ != nullptr) { checker->ValidateUnaryOperatorOperand(expr->target_); } + auto [sourceType, relationNode] = CheckAssignmentExprOperatorType(expr); + const checker::Type *targetType = checker->TryGettingFunctionTypeFromInvokeFunction(leftType); + const checker::Type *rightType = checker->TryGettingFunctionTypeFromInvokeFunction(sourceType); + + checker::AssignmentContext(checker->Relation(), relationNode, sourceType, leftType, expr->Right()->Start(), + {"Type '", rightType, "' cannot be assigned to type '", targetType, "'"}); + + expr->SetTsType(expr->Left()->TsType()); + return expr->TsType(); +} + +std::tuple ETSAnalyzer::CheckAssignmentExprOperatorType(ir::AssignmentExpression *expr) const +{ + ETSChecker *checker = GetETSChecker(); checker::Type *sourceType {}; ir::Expression *relationNode = expr->Right(); + auto *leftType = expr->Left()->Check(checker); switch (expr->OperatorType()) { case lexer::TokenType::PUNCTUATOR_MULTIPLY_EQUAL: case lexer::TokenType::PUNCTUATOR_EXPONENTIATION_EQUAL: @@ -873,11 +885,7 @@ checker::Type *ETSAnalyzer::Check(ir::AssignmentExpression *expr) const } } - checker::AssignmentContext(checker->Relation(), relationNode, sourceType, leftType, expr->Right()->Start(), - {"Initializers type is not assignable to the target type"}); - - expr->SetTsType(expr->Left()->TsType()); - return expr->TsType(); + return {sourceType, relationNode}; } checker::Type *ETSAnalyzer::Check(ir::AwaitExpression *expr) const @@ -1347,7 +1355,9 @@ checker::Type *ETSAnalyzer::Check(ir::ObjectExpression *expr) const checker->ThrowTypeError({"need to specify target type for class composite"}, expr->Start()); } if (!expr->PreferredType()->IsETSObjectType()) { - checker->ThrowTypeError({"target type for class composite needs to be an object type"}, expr->Start()); + checker->ThrowTypeError( + {"Target type for class composite needs to be an object type, found '", expr->PreferredType(), "'"}, + expr->Start()); } if (expr->PreferredType()->IsETSDynamicType()) { @@ -1381,6 +1391,17 @@ checker::Type *ETSAnalyzer::Check(ir::ObjectExpression *expr) const checker->ThrowTypeError({"type ", objType->Name(), " has no parameterless constructor"}, expr->Start()); } + CheckObjectExprProps(expr); + + expr->SetTsType(objType); + return objType; +} + +void ETSAnalyzer::CheckObjectExprProps(const ir::ObjectExpression *expr) const +{ + ETSChecker *checker = GetETSChecker(); + checker::ETSObjectType *objType = expr->PreferredType()->AsETSObjectType(); + for (ir::Expression *propExpr : expr->Properties()) { ASSERT(propExpr->IsProperty()); ir::Property *prop = propExpr->AsProperty(); @@ -1413,12 +1434,13 @@ checker::Type *ETSAnalyzer::Check(ir::ObjectExpression *expr) const value->AsObjectExpression()->SetPreferredType(propType); } value->SetTsType(value->Check(checker)); - checker::AssignmentContext(checker->Relation(), value, value->TsType(), propType, value->Start(), - {"value type is not assignable to the property type"}); - } - expr->SetTsType(objType); - return objType; + const checker::Type *targetType = checker->TryGettingFunctionTypeFromInvokeFunction(propType); + + checker::AssignmentContext( + checker->Relation(), value, value->TsType(), propType, value->Start(), + {"Type '", value->TsType(), "' is not compatible with type '", targetType, "' at property '", pname, "'"}); + } } checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::OmittedExpression *expr) const @@ -2146,9 +2168,13 @@ void CheckReturnType(ETSChecker *checker, checker::Type *funcReturnType, checker stArgument->Start()); } } else { - checker::AssignmentContext(checker->Relation(), stArgument, argumentType, funcReturnType, stArgument->Start(), - {"Return statement type is not compatible with the enclosing method's return type."}, - checker::TypeRelationFlag::DIRECT_RETURN); + const Type *targetType = checker->TryGettingFunctionTypeFromInvokeFunction(funcReturnType); + const Type *sourceType = checker->TryGettingFunctionTypeFromInvokeFunction(argumentType); + + checker::AssignmentContext( + checker->Relation(), stArgument, argumentType, funcReturnType, stArgument->Start(), + {"Type '", sourceType, "' is not compatible with the enclosing method's return type '", targetType, "'"}, + checker::TypeRelationFlag::DIRECT_RETURN); } } @@ -2176,10 +2202,13 @@ void InferReturnType(ETSChecker *checker, ir::ScriptFunction *containingFunc, ch auto arrowFunc = stArgument->AsArrowFunctionExpression(); auto typeAnnotation = arrowFunc->CreateTypeAnnotation(checker); funcReturnType = typeAnnotation->GetType(checker); - checker::AssignmentContext(checker->Relation(), arrowFunc, arrowFunc->TsType(), funcReturnType, - stArgument->Start(), - {"Return statement type is not compatible with the enclosing method's return type."}, - checker::TypeRelationFlag::DIRECT_RETURN); + const Type *sourceType = checker->TryGettingFunctionTypeFromInvokeFunction(arrowFunc->TsType()); + const Type *targetType = checker->TryGettingFunctionTypeFromInvokeFunction(funcReturnType); + + checker::AssignmentContext( + checker->Relation(), arrowFunc, arrowFunc->TsType(), funcReturnType, stArgument->Start(), + {"Type '", sourceType, "' is not compatible with the enclosing method's return type '", targetType, "'"}, + checker::TypeRelationFlag::DIRECT_RETURN); } containingFunc->Signature()->SetReturnType(funcReturnType); @@ -2362,16 +2391,17 @@ checker::Type *ETSAnalyzer::Check(ir::SwitchStatement *st) const } else { checker::AssignmentContext( checker->Relation(), st->discriminant_, caseType, unboxedDiscType, it->Test()->Start(), - {"Switch case type ", caseType, " is not comparable to discriminant type ", comparedExprType}, + {"Switch case type '", caseType, "' is not comparable to discriminant type '", comparedExprType, + "'"}, (comparedExprType->IsETSObjectType() ? checker::TypeRelationFlag::NO_WIDENING : checker::TypeRelationFlag::NO_UNBOXING) | checker::TypeRelationFlag::NO_BOXING); } if (!validCaseType) { - checker->ThrowTypeError( - {"Switch case type ", caseType, " is not comparable to discriminant type ", comparedExprType}, - it->Test()->Start()); + checker->ThrowTypeError({"Switch case type '", caseType, "' is not comparable to discriminant type '", + comparedExprType, "'"}, + it->Test()->Start()); } } diff --git a/ets2panda/checker/ETSAnalyzer.h b/ets2panda/checker/ETSAnalyzer.h index c284a6db262..0e4e717facc 100644 --- a/ets2panda/checker/ETSAnalyzer.h +++ b/ets2panda/checker/ETSAnalyzer.h @@ -36,6 +36,8 @@ public: #undef DECLARE_ETSANALYZER_CHECK_METHOD checker::Type *PreferredType(ir::ObjectExpression *expr) const; checker::Type *GetPreferredType(ir::ArrayExpression *expr) const; + void CheckObjectExprProps(const ir::ObjectExpression *expr) const; + std::tuple CheckAssignmentExprOperatorType(ir::AssignmentExpression *expr) const; private: ETSChecker *GetETSChecker() const; diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index e565e924bda..e909286f937 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -547,6 +547,7 @@ public: void ValidateTupleMinElementSize(ir::ArrayExpression *arrayExpr, ETSTupleType *tuple); void ModifyPreferredType(ir::ArrayExpression *arrayExpr, Type *newPreferredType); Type *SelectGlobalIntegerTypeForNumeric(Type *type); + const Type *TryGettingFunctionTypeFromInvokeFunction(const Type *type) const; void GenerateGetterSetterBody(ETSChecker *checker, ArenaVector &stmts, ArenaVector ¶ms, ir::ClassProperty *field, diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 7dfecd77be3..e6476c47e5a 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -162,11 +162,6 @@ bool ETSChecker::EnhanceSubstitutionForObject(const ArenaVector &typePar return true; } -// NOLINTBEGIN(modernize-avoid-c-arrays) -static constexpr char const INVALID_CALL_ARGUMENT_1[] = "Call argument at index "; -static constexpr char const INVALID_CALL_ARGUMENT_2[] = " is not compatible with the signature's type at that index."; -static constexpr char const INVALID_CALL_ARGUMENT_3[] = " is not compatible with the signature's rest parameter type."; -// NOLINTEND(modernize-avoid-c-arrays) Signature *ETSChecker::ValidateParameterlessConstructor(Signature *signature, const lexer::SourcePosition &pos, TypeRelationFlag flags) { @@ -226,10 +221,11 @@ bool ETSChecker::ValidateSignatureRequiredParams(Signature *substitutedSig, } auto *const argumentType = argument->Check(this); + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(substitutedSig->Params()[index]->TsType()); auto const invocationCtx = checker::InvocationContext( Relation(), argument, argumentType, substitutedSig->Params()[index]->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_2}, flags); + {"Type '", argumentType, "' is not compatible with type '", targetType, "' at index ", index + 1}, flags); if (!invocationCtx.IsInvocable()) { return false; } @@ -250,10 +246,16 @@ bool ETSChecker::ValidateSignatureRestParams(Signature *substitutedSig, const Ar auto &argument = arguments[index]; if (!argument->IsSpreadElement()) { + auto *const argumentType = argument->Check(this); + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction( + substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType()); + const Type *sourceType = TryGettingFunctionTypeFromInvokeFunction(argumentType); auto const invocationCtx = checker::InvocationContext( - Relation(), argument, argument->Check(this), + Relation(), argument, argumentType, substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + {"Type '", sourceType, "' is not compatible with rest parameter type '", targetType, "' at index ", + index + 1}, + flags); if (!invocationCtx.IsInvocable()) { return false; } @@ -268,10 +270,15 @@ bool ETSChecker::ValidateSignatureRestParams(Signature *substitutedSig, const Ar } auto *const restArgument = argument->AsSpreadElement()->Argument(); + auto *const argumentType = restArgument->Check(this); + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(substitutedSig->RestVar()->TsType()); + const Type *sourceType = TryGettingFunctionTypeFromInvokeFunction(argumentType); auto const invocationCtx = checker::InvocationContext( - Relation(), restArgument, restArgument->Check(this), substitutedSig->RestVar()->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + Relation(), restArgument, argumentType, substitutedSig->RestVar()->TsType(), argument->Start(), + {"Type '", sourceType, "' is not compatible with rest parameter type '", targetType, "' at index ", + index + 1}, + flags); if (!invocationCtx.IsInvocable()) { return false; } diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 5c1b3fd1877..41e52fafab4 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1011,9 +1011,11 @@ void ETSChecker::ResolveReturnStatement(checker::Type *funcReturnType, checker:: containingFunc->Signature()->SetReturnType(funcReturnType); containingFunc->Signature()->AddSignatureFlag(checker::SignatureFlags::INFERRED_RETURN_TYPE); } else if (!Relation()->IsAssignableTo(argumentType, funcReturnType)) { - ThrowTypeError( - "Return statement type is not compatible with previous method's return statement " - "type(s).", + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(funcReturnType); + const Type *sourceType = TryGettingFunctionTypeFromInvokeFunction(argumentType); + + Relation()->RaiseError( + {"Function cannot have different primitive return types, found '", targetType, "', '", sourceType, "'"}, st->Argument()->Start()); } } else { @@ -1130,8 +1132,11 @@ checker::Type *ETSChecker::CheckVariableDeclaration(ir::Identifier *ident, ir::T } if (annotationType != nullptr) { + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(annotationType); + const Type *sourceType = TryGettingFunctionTypeFromInvokeFunction(initType); + AssignmentContext(Relation(), init, initType, annotationType, init->Start(), - {"Initializers type is not assignable to the target type"}); + {"Type '", sourceType, "' cannot be assigned to type '", targetType, "'"}); if (isConst && initType->HasTypeFlag(TypeFlag::ETS_PRIMITIVE) && annotationType->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) { bindingVar->SetTsType(init->TsType()); @@ -1158,11 +1163,8 @@ checker::Type *ETSChecker::CheckVariableDeclaration(ir::Identifier *ident, ir::T init->Start()); } - if (initType->IsNullish() || isConst) { - bindingVar->SetTsType(initType); - } else { - bindingVar->SetTsType(GetNonConstantTypeFromPrimitiveType(initType)); - } + (initType->IsNullish() || isConst) ? bindingVar->SetTsType(initType) + : bindingVar->SetTsType(GetNonConstantTypeFromPrimitiveType(initType)); return bindingVar->TsType(); } @@ -2541,10 +2543,13 @@ bool ETSChecker::TypeInference(Signature *signature, const ArenaVectorIsETSFunctionType()); InferTypesForLambda(lambda, typeAnn->AsETSFunctionType()); Type *const argType = arrowFuncExpr->Check(this); + const Type *targetType = TryGettingFunctionTypeFromInvokeFunction(signature->Params()[index]->TsType()); + const std::initializer_list msg = { + "Type '", argType, "' is not compatible with type '", targetType, "' at index ", index + 1}; - checker::InvocationContext invokationCtx( - Relation(), arguments[index], argType, signature->Params()[index]->TsType(), arrowFuncExpr->Start(), - {"Call argument at index ", index, " is not compatible with the signature's type at that index"}, flags); + checker::InvocationContext invokationCtx(Relation(), arguments[index], argType, + signature->Params()[index]->TsType(), arrowFuncExpr->Start(), msg, + flags); invocable &= invokationCtx.IsInvocable(); } @@ -2722,6 +2727,19 @@ ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty return method; } +const Type *ETSChecker::TryGettingFunctionTypeFromInvokeFunction(const Type *type) const +{ + if (type->IsETSObjectType() && type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) { + auto const propInvoke = type->AsETSObjectType()->GetProperty(util::StringView("invoke"), + PropertySearchFlags::SEARCH_INSTANCE_METHOD); + ASSERT(propInvoke != nullptr); + + return propInvoke->TsType(); + } + + return type; +} + bool ETSChecker::TryTransformingToStaticInvoke(ir::Identifier *const ident, const Type *resolvedType) { ASSERT(ident->Parent()->IsCallExpression()); diff --git a/ets2panda/checker/ets/typeRelationContext.cpp b/ets2panda/checker/ets/typeRelationContext.cpp index e28b250c73a..b74e3dcbc3d 100644 --- a/ets2panda/checker/ets/typeRelationContext.cpp +++ b/ets2panda/checker/ets/typeRelationContext.cpp @@ -32,9 +32,12 @@ void AssignmentContext::ValidateArrayTypeInitializerByElement(TypeRelation *rela for (uint32_t index = 0; index < node->Elements().size(); index++) { ir::Expression *currentArrayElem = node->Elements()[index]; + auto *const currentArrayElementType = currentArrayElem->Check(relation->GetChecker()->AsETSChecker()); + AssignmentContext(relation, currentArrayElem, currentArrayElem->Check(relation->GetChecker()->AsETSChecker()), target->ElementType(), currentArrayElem->Start(), - {"Array element at index ", index, " is not compatible with the target array element type."}); + {"Array element at index ", index, " with type '", currentArrayElementType, + "' is not compatible with the target array element type '", target->ElementType(), "'"}); } } diff --git a/ets2panda/checker/ets/typeRelationContext.h b/ets2panda/checker/ets/typeRelationContext.h index 155aa6ae64e..b4a9b19a73f 100644 --- a/ets2panda/checker/ets/typeRelationContext.h +++ b/ets2panda/checker/ets/typeRelationContext.h @@ -47,6 +47,8 @@ public: flags_ |= flags; relation->SetNode(node); + // NOTE (oeotvos) The narrowing flag will be applied here. It means, that the result of "let tmp: int = 1.5" + // will be 1, which could cause problems. if (source->HasTypeFlag(TypeFlag::CONSTANT)) { flags_ |= TypeRelationFlag::NARROWING; } diff --git a/ets2panda/checker/types/ets/etsArrayType.cpp b/ets2panda/checker/types/ets/etsArrayType.cpp index 91989dc86e1..9b0e9403aad 100644 --- a/ets2panda/checker/types/ets/etsArrayType.cpp +++ b/ets2panda/checker/types/ets/etsArrayType.cpp @@ -25,6 +25,11 @@ void ETSArrayType::ToString(std::stringstream &ss) const { element_->ToString(ss); ss << "[]"; + + if (IsNullish()) { + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_BITWISE_OR) + << lexer::TokenToString(lexer::TokenType::LITERAL_NULL); + } } void ETSArrayType::ToAssemblerType(std::stringstream &ss) const diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 2a7695b0539..9e82f59c79b 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -283,26 +283,40 @@ ArenaMap ETSObjectType::Coll void ETSObjectType::ToString(std::stringstream &ss) const { - ss << name_; + if (HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) { + if (IsNullish() && this != GetConstOriginalBaseType() && !name_.Is("NullType") && !IsETSNullLike() && + !name_.Empty()) { + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS); + } + GetFunctionalInterfaceInvokeType()->ToString(ss); + } else { + ss << name_; + } if (!typeArguments_.empty()) { - auto const typeArgumentsSize = typeArguments_.size(); ss << compiler::Signatures::GENERIC_BEGIN; - typeArguments_[0]->ToString(ss); - for (std::size_t i = 1U; i < typeArgumentsSize; ++i) { - ss << ','; - typeArguments_[i]->ToString(ss); + for (auto arg = typeArguments_.cbegin(); arg != typeArguments_.cend(); ++arg) { + (*arg)->ToString(ss); + + if (next(arg) != typeArguments_.cend()) { + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_COMMA); + } } ss << compiler::Signatures::GENERIC_END; } if (IsNullish() && this != GetConstOriginalBaseType() && !name_.Is("NullType") && !IsETSNullLike() && !name_.Empty()) { + if (HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) { + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_RIGHT_PARENTHESIS); + } if (ContainsNull()) { - ss << "|null"; + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_BITWISE_OR) + << lexer::TokenToString(lexer::TokenType::LITERAL_NULL); } if (ContainsUndefined()) { - ss << "|undefined"; + ss << lexer::TokenToString(lexer::TokenType::PUNCTUATOR_BITWISE_OR) + << lexer::TokenToString(lexer::TokenType::KEYW_UNDEFINED); } } } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index ff6cca7a94d..c59e37eb679 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -389,7 +389,7 @@ public: return (flags_ & flag) != 0; } - ETSFunctionType *GetFunctionalInterfaceInvokeType() + ETSFunctionType *GetFunctionalInterfaceInvokeType() const { ASSERT(HasObjectFlag(ETSObjectFlags::FUNCTIONAL)); auto *invoke = GetOwnProperty("invoke"); diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index 92f9feb3171..1f79e6f9972 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -24,10 +24,11 @@ void ETSTupleType::ToString(std::stringstream &ss) const { ss << "["; - for (const auto *const type : typeList_) { - type->ToString(ss); - if (type != typeList_.back()) { - ss << ","; + for (auto it = typeList_.begin(); it != typeList_.end(); it++) { + (*it)->ToString(ss); + + if (std::next(it) != typeList_.end()) { + ss << ", "; } } diff --git a/ets2panda/test/compiler/ets/FunctionType1-expected.txt b/ets2panda/test/compiler/ets/FunctionType1-expected.txt index 067f0174687..eb64020f40e 100644 --- a/ets2panda/test/compiler/ets/FunctionType1-expected.txt +++ b/ets2panda/test/compiler/ets/FunctionType1-expected.txt @@ -1001,4 +1001,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [FunctionType1.ets:24:12] +TypeError: Type '(a: int, b: int) => int' cannot be assigned to type '(a: int, b: int) => void' [FunctionType1.ets:24:12] diff --git a/ets2panda/test/compiler/ets/FunctionType3-expected.txt b/ets2panda/test/compiler/ets/FunctionType3-expected.txt index 9f4f79c8571..bbb4c6e27b1 100644 --- a/ets2panda/test/compiler/ets/FunctionType3-expected.txt +++ b/ets2panda/test/compiler/ets/FunctionType3-expected.txt @@ -809,4 +809,4 @@ } } } -TypeError: Call argument at index 1 is not compatible with the signature's type at that index. [FunctionType3.ets:23:16] +TypeError: Type 'string' is not compatible with type 'int' at index 2 [FunctionType3.ets:23:16] diff --git a/ets2panda/test/compiler/ets/conversion_Double-to-Int_typeerror-expected.txt b/ets2panda/test/compiler/ets/conversion_Double-to-Int_typeerror-expected.txt index b96e1faf4dd..b4bf32c6330 100644 --- a/ets2panda/test/compiler/ets/conversion_Double-to-Int_typeerror-expected.txt +++ b/ets2panda/test/compiler/ets/conversion_Double-to-Int_typeerror-expected.txt @@ -456,4 +456,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [conversion_Double-to-Int_typeerror.ets:17:17] +TypeError: Type 'Double' cannot be assigned to type 'Int' [conversion_Double-to-Int_typeerror.ets:17:17] diff --git a/ets2panda/test/compiler/ets/conversion_Double-to-Int_w_try_stmt_typerror-expected.txt b/ets2panda/test/compiler/ets/conversion_Double-to-Int_w_try_stmt_typerror-expected.txt index 96c8b5fd3ce..34209cbf2af 100644 --- a/ets2panda/test/compiler/ets/conversion_Double-to-Int_w_try_stmt_typerror-expected.txt +++ b/ets2panda/test/compiler/ets/conversion_Double-to-Int_w_try_stmt_typerror-expected.txt @@ -718,4 +718,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [conversion_Double-to-Int_w_try_stmt_typerror.ets:17:18] +TypeError: Type 'Double' is not compatible with the enclosing method's return type 'Int' [conversion_Double-to-Int_w_try_stmt_typerror.ets:17:18] diff --git a/ets2panda/test/compiler/ets/conversion_Int-to-Double_typeerror-expected.txt b/ets2panda/test/compiler/ets/conversion_Int-to-Double_typeerror-expected.txt index 18cfe0261da..af949c27061 100644 --- a/ets2panda/test/compiler/ets/conversion_Int-to-Double_typeerror-expected.txt +++ b/ets2panda/test/compiler/ets/conversion_Int-to-Double_typeerror-expected.txt @@ -456,4 +456,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [conversion_Int-to-Double_typeerror.ets:17:20] +TypeError: Type 'Int' cannot be assigned to type 'Double' [conversion_Int-to-Double_typeerror.ets:17:20] diff --git a/ets2panda/test/compiler/ets/conversion_call-context_Int-to-Double_typeerror-expected.txt b/ets2panda/test/compiler/ets/conversion_call-context_Int-to-Double_typeerror-expected.txt index a4f0dff3f57..b811e85e80a 100644 --- a/ets2panda/test/compiler/ets/conversion_call-context_Int-to-Double_typeerror-expected.txt +++ b/ets2panda/test/compiler/ets/conversion_call-context_Int-to-Double_typeerror-expected.txt @@ -679,4 +679,4 @@ } } } -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [conversion_call-context_Int-to-Double_typeerror.ets:20:7] +TypeError: Type 'Int' is not compatible with type 'Double' at index 1 [conversion_call-context_Int-to-Double_typeerror.ets:20:7] diff --git a/ets2panda/test/compiler/ets/etsObjectToString0-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString0-expected.txt new file mode 100644 index 00000000000..0f8108b11a0 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString0-expected.txt @@ -0,0 +1,584 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": "test", + "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": "test", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type 'A|null' is not compatible with the enclosing method's return type 'int' [etsObjectToString0.ets:20:12] diff --git a/ets2panda/test/compiler/ets/etsObjectToString0.ets b/ets2panda/test/compiler/ets/etsObjectToString0.ets new file mode 100644 index 00000000000..8601ec1d893 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString0.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +function test(): int{ + let a: A|null; + return a; +} diff --git a/ets2panda/test/compiler/ets/etsObjectToString1-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString1-expected.txt new file mode 100644 index 00000000000..da2b83b6cf7 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString1-expected.txt @@ -0,0 +1,640 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": "test0", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "test0", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type 'A|null' is not compatible with the enclosing method's return type 'A' [etsObjectToString1.ets:20:12] diff --git a/ets2panda/test/compiler/ets/etsObjectToString1.ets b/ets2panda/test/compiler/ets/etsObjectToString1.ets new file mode 100644 index 00000000000..f6d7434f8ca --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString1.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +function test0(): A { + let a: A|null; + return a; +} diff --git a/ets2panda/test/compiler/ets/etsObjectToString2-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString2-expected.txt new file mode 100644 index 00000000000..34fb46d94ac --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString2-expected.txt @@ -0,0 +1,668 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": "test1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "test1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 34 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type 'A|null' is not compatible with the enclosing method's return type 'A|null' [etsObjectToString2.ets:20:12] diff --git a/ets2panda/test/compiler/ets/etsObjectToString2.ets b/ets2panda/test/compiler/ets/etsObjectToString2.ets new file mode 100644 index 00000000000..07125b85a19 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString2.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +function test1(): A|null { + let a: A|null; + return a; +} diff --git a/ets2panda/test/compiler/ets/etsObjectToString3-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString3-expected.txt new file mode 100644 index 00000000000..b3fdc5aa267 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString3-expected.txt @@ -0,0 +1,890 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "z", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 15 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 15 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 15 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 28 + }, + "end": { + "line": 19, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 28 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 28 + }, + "end": { + "line": 19, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 35 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 35 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Type '() => A' cannot be assigned to type 'A' [etsObjectToString3.ets:18:17] diff --git a/ets2panda/test/compiler/ets/etsObjectToString3.ets b/ets2panda/test/compiler/ets/etsObjectToString3.ets new file mode 100644 index 00000000000..afd5cff9f14 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString3.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +let z: A = (): A => { + let b : A = new A(); + return b; +} diff --git a/ets2panda/test/compiler/ets/etsObjectToString4-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString4-expected.txt new file mode 100644 index 00000000000..a92c6032836 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString4-expected.txt @@ -0,0 +1,468 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "f", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 31 + } + } + } + ], + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "f", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 30 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 30 + } + } + } + ], + "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": 19, + "column": 1 + } + } +} +TypeError: Type 'int' cannot be assigned to type '() => int' [etsObjectToString4.ets:18:29] diff --git a/ets2panda/test/compiler/ets/etsObjectToString4.ets b/ets2panda/test/compiler/ets/etsObjectToString4.ets new file mode 100644 index 00000000000..f957ea6d940 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString4.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +let f: (() => int) | null = 5; diff --git a/ets2panda/test/compiler/ets/etsObjectToString5-expected.txt b/ets2panda/test/compiler/ets/etsObjectToString5-expected.txt new file mode 100644 index 00000000000..c96da27d618 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString5-expected.txt @@ -0,0 +1,848 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "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": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 11 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + { + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "g", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 45 + }, + "end": { + "line": 18, + "column": 46 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 47 + }, + "end": { + "line": 18, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 47 + }, + "end": { + "line": 18, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 47 + }, + "end": { + "line": 18, + "column": 54 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 46 + }, + "end": { + "line": 18, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 45 + }, + "end": { + "line": 18, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 45 + }, + "end": { + "line": 18, + "column": 55 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 55 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 57 + }, + "end": { + "line": 18, + "column": 58 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 59 + }, + "end": { + "line": 18, + "column": 65 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 59 + }, + "end": { + "line": 18, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 59 + }, + "end": { + "line": 18, + "column": 66 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 58 + }, + "end": { + "line": 18, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 57 + }, + "end": { + "line": 18, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 57 + }, + "end": { + "line": 18, + "column": 67 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "y", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 75 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 42 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 20, + "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": 21, + "column": 1 + } + } +} +TypeError: Type '(y: A) => A|null' cannot be assigned to type '(y: A) => A|null' [etsObjectToString5.ets:18:42] diff --git a/ets2panda/test/compiler/ets/etsObjectToString5.ets b/ets2panda/test/compiler/ets/etsObjectToString5.ets new file mode 100644 index 00000000000..c4535d9f0f1 --- /dev/null +++ b/ets2panda/test/compiler/ets/etsObjectToString5.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A{} + +let g:((y:A) => A|null)|null = (y:A): A|null => { + return y; +} diff --git a/ets2panda/test/compiler/ets/generic_function_call_2-expected.txt b/ets2panda/test/compiler/ets/generic_function_call_2-expected.txt index d7f2b191479..40b5c23fb50 100644 --- a/ets2panda/test/compiler/ets/generic_function_call_2-expected.txt +++ b/ets2panda/test/compiler/ets/generic_function_call_2-expected.txt @@ -982,4 +982,4 @@ } } } -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [generic_function_call_2.ets:24:9] +TypeError: Type 'B' is not compatible with type 'A' at index 1 [generic_function_call_2.ets:24:9] diff --git a/ets2panda/test/compiler/ets/generics_primitive_type_param_neg_1-expected.txt b/ets2panda/test/compiler/ets/generics_primitive_type_param_neg_1-expected.txt index 981fbdf9d51..6fac876a48b 100644 --- a/ets2panda/test/compiler/ets/generics_primitive_type_param_neg_1-expected.txt +++ b/ets2panda/test/compiler/ets/generics_primitive_type_param_neg_1-expected.txt @@ -902,4 +902,4 @@ } } } -TypeError: Switch case type Int is not comparable to discriminant type int [generics_primitive_type_param_neg_1.ets:25:14] +TypeError: Switch case type 'Int' is not comparable to discriminant type 'int' [generics_primitive_type_param_neg_1.ets:25:14] diff --git a/ets2panda/test/compiler/ets/identifierReference10-expected.txt b/ets2panda/test/compiler/ets/identifierReference10-expected.txt index c956b389498..ac3b7839e92 100644 --- a/ets2panda/test/compiler/ets/identifierReference10-expected.txt +++ b/ets2panda/test/compiler/ets/identifierReference10-expected.txt @@ -870,4 +870,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [identifierReference10.ets:24:16] +TypeError: Type 'string' cannot be assigned to type 'int' [identifierReference10.ets:24:16] diff --git a/ets2panda/test/compiler/ets/identifierReference2-expected.txt b/ets2panda/test/compiler/ets/identifierReference2-expected.txt index 4f28a2426c9..c0fb2875eaa 100644 --- a/ets2panda/test/compiler/ets/identifierReference2-expected.txt +++ b/ets2panda/test/compiler/ets/identifierReference2-expected.txt @@ -398,4 +398,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [identifierReference2.ets:16:34] +TypeError: Type '(v: double, u: double) => double' cannot be assigned to type '(a: int, b: int) => void' [identifierReference2.ets:16:34] diff --git a/ets2panda/test/compiler/ets/identifierReference3-expected.txt b/ets2panda/test/compiler/ets/identifierReference3-expected.txt index 6e49f3f6047..f5c375d18a7 100644 --- a/ets2panda/test/compiler/ets/identifierReference3-expected.txt +++ b/ets2panda/test/compiler/ets/identifierReference3-expected.txt @@ -586,4 +586,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [identifierReference3.ets:20:9] +TypeError: Type 'string' cannot be assigned to type 'int' [identifierReference3.ets:20:9] diff --git a/ets2panda/test/compiler/ets/inferTypeOfArrayNegative3-expected.txt b/ets2panda/test/compiler/ets/inferTypeOfArrayNegative3-expected.txt index 6b9887c2443..50c685879a5 100644 --- a/ets2panda/test/compiler/ets/inferTypeOfArrayNegative3-expected.txt +++ b/ets2panda/test/compiler/ets/inferTypeOfArrayNegative3-expected.txt @@ -429,4 +429,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [inferTypeOfArrayNegative3.ets:17:8] +TypeError: Type 'string' cannot be assigned to type 'double' [inferTypeOfArrayNegative3.ets:17:8] diff --git a/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentType-expected.txt b/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentType-expected.txt index 358b42f55c9..a17df28bb33 100644 --- a/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentType-expected.txt +++ b/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentType-expected.txt @@ -515,4 +515,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [lambdaExpressionWithoutBlockStatementDifferentType.ets:17:28] +TypeError: Type '() => String' cannot be assigned to type '() => int' [lambdaExpressionWithoutBlockStatementDifferentType.ets:17:28] diff --git a/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentTypeInfunction-expected.txt b/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentTypeInfunction-expected.txt index 8d7e876115a..c35a6a6a7b7 100644 --- a/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentTypeInfunction-expected.txt +++ b/ets2panda/test/compiler/ets/lambdaExpressionWithoutBlockStatementDifferentTypeInfunction-expected.txt @@ -666,4 +666,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [lambdaExpressionWithoutBlockStatementDifferentTypeInfunction.ets:21:39] +TypeError: Type 'String' is not compatible with the enclosing method's return type 'int' [lambdaExpressionWithoutBlockStatementDifferentTypeInfunction.ets:21:39] diff --git a/ets2panda/test/compiler/ets/lambdaFunction3-expected.txt b/ets2panda/test/compiler/ets/lambdaFunction3-expected.txt index 4fbb638c04d..22b2a3c2f29 100644 --- a/ets2panda/test/compiler/ets/lambdaFunction3-expected.txt +++ b/ets2panda/test/compiler/ets/lambdaFunction3-expected.txt @@ -642,4 +642,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [lambdaFunction3.ets:18:39] +TypeError: Type '(b: int) => void' cannot be assigned to type '(b: String) => void' [lambdaFunction3.ets:18:39] diff --git a/ets2panda/test/compiler/ets/lambdaFunction5-expected.txt b/ets2panda/test/compiler/ets/lambdaFunction5-expected.txt index 131a90f02e3..5737ced2b4e 100644 --- a/ets2panda/test/compiler/ets/lambdaFunction5-expected.txt +++ b/ets2panda/test/compiler/ets/lambdaFunction5-expected.txt @@ -1705,4 +1705,4 @@ } } } -TypeError: Call argument at index 1 is not compatible with the signature's type at that index. [lambdaFunction5.ets:35:17] +TypeError: Type 'string' is not compatible with type 'int' at index 2 [lambdaFunction5.ets:35:17] diff --git a/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt index a2cc17f3065..218cfffdcb3 100644 --- a/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt +++ b/ets2panda/test/compiler/ets/n_assignGenericWithNullableTypeParamToNonNullable-expected.txt @@ -1012,4 +1012,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignGenericWithNullableTypeParamToNonNullable.ets:21:19] +TypeError: Type 'A' cannot be assigned to type 'A' [n_assignGenericWithNullableTypeParamToNonNullable.ets:21:19] diff --git a/ets2panda/test/compiler/ets/objectLiteralPrimitiveContextType-expected.txt b/ets2panda/test/compiler/ets/objectLiteralPrimitiveContextType-expected.txt index 15dc9ad6b1f..d4c28c364bd 100644 --- a/ets2panda/test/compiler/ets/objectLiteralPrimitiveContextType-expected.txt +++ b/ets2panda/test/compiler/ets/objectLiteralPrimitiveContextType-expected.txt @@ -257,4 +257,4 @@ } } } -TypeError: target type for class composite needs to be an object type [objectLiteralPrimitiveContextType.ets:16:14] +TypeError: Target type for class composite needs to be an object type, found 'int' [objectLiteralPrimitiveContextType.ets:16:14] diff --git a/ets2panda/test/compiler/ets/objectLiteralWrongValueType-expected.txt b/ets2panda/test/compiler/ets/objectLiteralWrongValueType-expected.txt index b391dd77615..ba7bf8cb407 100644 --- a/ets2panda/test/compiler/ets/objectLiteralWrongValueType-expected.txt +++ b/ets2panda/test/compiler/ets/objectLiteralWrongValueType-expected.txt @@ -518,4 +518,4 @@ } } } -TypeError: value type is not assignable to the property type [objectLiteralWrongValueType.ets:21:8] +TypeError: Type 'string' is not compatible with type 'int' at property 'f' [objectLiteralWrongValueType.ets:21:8] diff --git a/ets2panda/test/compiler/ets/switchStatementWrongBoxing-expected.txt b/ets2panda/test/compiler/ets/switchStatementWrongBoxing-expected.txt index 8ffd0b9a5d1..f893cf2cf94 100644 --- a/ets2panda/test/compiler/ets/switchStatementWrongBoxing-expected.txt +++ b/ets2panda/test/compiler/ets/switchStatementWrongBoxing-expected.txt @@ -696,4 +696,4 @@ } } } -TypeError: Switch case type byte is not comparable to discriminant type Int [switchStatementWrongBoxing.ets:21:18] +TypeError: Switch case type 'byte' is not comparable to discriminant type 'Int' [switchStatementWrongBoxing.ets:21:18] diff --git a/ets2panda/test/compiler/ets/tuple_types_10_neg-expected.txt b/ets2panda/test/compiler/ets/tuple_types_10_neg-expected.txt index d72397b0812..7f3a1736aab 100644 --- a/ets2panda/test/compiler/ets/tuple_types_10_neg-expected.txt +++ b/ets2panda/test/compiler/ets/tuple_types_10_neg-expected.txt @@ -670,4 +670,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [tuple_types_10_neg.ets:19:39] +TypeError: Type '[double, double]' cannot be assigned to type '[double, double, double]' [tuple_types_10_neg.ets:19:39] diff --git a/ets2panda/test/compiler/ets/tuple_types_11_neg-expected.txt b/ets2panda/test/compiler/ets/tuple_types_11_neg-expected.txt index 669daa11446..999b175e3a0 100644 --- a/ets2panda/test/compiler/ets/tuple_types_11_neg-expected.txt +++ b/ets2panda/test/compiler/ets/tuple_types_11_neg-expected.txt @@ -684,4 +684,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [tuple_types_11_neg.ets:19:31] +TypeError: Type '[double, double, double]' cannot be assigned to type '[double, double]' [tuple_types_11_neg.ets:19:31] diff --git a/ets2panda/test/compiler/ets/tuple_types_6_neg-expected.txt b/ets2panda/test/compiler/ets/tuple_types_6_neg-expected.txt index b54e9465492..01342b30431 100644 --- a/ets2panda/test/compiler/ets/tuple_types_6_neg-expected.txt +++ b/ets2panda/test/compiler/ets/tuple_types_6_neg-expected.txt @@ -533,4 +533,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [tuple_types_6_neg.ets:19:23] +TypeError: Type '[double]' cannot be assigned to type '[Double]' [tuple_types_6_neg.ets:19:23] diff --git a/ets2panda/test/compiler/ets/tuple_types_9_neg-expected.txt b/ets2panda/test/compiler/ets/tuple_types_9_neg-expected.txt index b3ff7ada9a5..959d61e1011 100644 --- a/ets2panda/test/compiler/ets/tuple_types_9_neg-expected.txt +++ b/ets2panda/test/compiler/ets/tuple_types_9_neg-expected.txt @@ -737,4 +737,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [tuple_types_9_neg.ets:19:36] +TypeError: Type '[double, String, ...Int[]]' cannot be assigned to type '[double, String, Int]' [tuple_types_9_neg.ets:19:36] diff --git a/ets2panda/test/compiler/ets/union_types_4-expected.txt b/ets2panda/test/compiler/ets/union_types_4-expected.txt index 3ae46f4f245..bdf5420f325 100644 --- a/ets2panda/test/compiler/ets/union_types_4-expected.txt +++ b/ets2panda/test/compiler/ets/union_types_4-expected.txt @@ -1555,4 +1555,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [union_types_4.ets:30:9] +TypeError: Type 'B' cannot be assigned to type 'String|Short|A' [union_types_4.ets:30:9] diff --git a/ets2panda/test/parser/ets/async_func_return_type_bad-expected.txt b/ets2panda/test/parser/ets/async_func_return_type_bad-expected.txt index f10a8253c6c..1ddada09283 100644 --- a/ets2panda/test/parser/ets/async_func_return_type_bad-expected.txt +++ b/ets2panda/test/parser/ets/async_func_return_type_bad-expected.txt @@ -342,4 +342,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [async_func_return_type_bad.ets:17:12] +TypeError: Type 'string' is not compatible with the enclosing method's return type 'Promise | Int' [async_func_return_type_bad.ets:17:12] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_ctor_decl_import_bad-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_ctor_decl_import_bad-expected.txt index fa2438b241b..f644e4d39a8 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_ctor_decl_import_bad-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_ctor_decl_import_bad-expected.txt @@ -1 +1 @@ -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [dynamic_class_ctor_decl_import_bad.ets:23:19] +TypeError: Type 'string' is not compatible with type 'double' at index 1 [dynamic_class_ctor_decl_import_bad.ets:23:19] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_1-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_1-expected.txt index 1a691d4d82c..d7211e1b8f0 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_1-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_1-expected.txt @@ -1 +1 @@ -TypeError: Initializers type is not assignable to the target type [dynamic_class_field_decl_import_bad_1.ets:23:12] +TypeError: Type 'int' cannot be assigned to type 'String' [dynamic_class_field_decl_import_bad_1.ets:23:12] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_2-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_2-expected.txt index 88718c571e3..cd3b95281f1 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_2-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_field_decl_import_bad_2-expected.txt @@ -1 +1 @@ -TypeError: Initializers type is not assignable to the target type [dynamic_class_field_decl_import_bad_2.ets:23:21] +TypeError: Type 'double' cannot be assigned to type 'String' [dynamic_class_field_decl_import_bad_2.ets:23:21] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_1-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_1-expected.txt index 6eac80975ca..68bb0527cee 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_1-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_1-expected.txt @@ -1 +1 @@ -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [dynamic_class_method_decl_import_bad_1.ets:23:11] +TypeError: Type 'string' is not compatible with type 'double' at index 1 [dynamic_class_method_decl_import_bad_1.ets:23:11] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_2-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_2-expected.txt index 9b52cc0ef3d..5c8704ac8c2 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_2-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_class_method_decl_import_bad_2-expected.txt @@ -1 +1 @@ -TypeError: Initializers type is not assignable to the target type [dynamic_class_method_decl_import_bad_2.ets:23:21] +TypeError: Type 'double' cannot be assigned to type 'String' [dynamic_class_method_decl_import_bad_2.ets:23:21] diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_func_decl_import_bad-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_func_decl_import_bad-expected.txt index 3e84372b587..b835f137b07 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_func_decl_import_bad-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/dynamic_func_decl_import_bad-expected.txt @@ -1 +1 @@ -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [dynamic_func_decl_import_bad.ets:23:9] +TypeError: Type 'string' is not compatible with type 'A' at index 1 [dynamic_func_decl_import_bad.ets:23:9] diff --git a/ets2panda/test/parser/ets/enum8-expected.txt b/ets2panda/test/parser/ets/enum8-expected.txt index 67d246372e6..150b136c75e 100644 --- a/ets2panda/test/parser/ets/enum8-expected.txt +++ b/ets2panda/test/parser/ets/enum8-expected.txt @@ -563,4 +563,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [enum8.ets:20:20] +TypeError: Type 'Color2' cannot be assigned to type 'Color' [enum8.ets:20:20] diff --git a/ets2panda/test/parser/ets/function_implicit_return_type2-expected.txt b/ets2panda/test/parser/ets/function_implicit_return_type2-expected.txt index 86dadbaca55..b9803f021ff 100644 --- a/ets2panda/test/parser/ets/function_implicit_return_type2-expected.txt +++ b/ets2panda/test/parser/ets/function_implicit_return_type2-expected.txt @@ -316,4 +316,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [function_implicit_return_type2.ets:17:27] +TypeError: Type '() => void' cannot be assigned to type '(i: int) => int' [function_implicit_return_type2.ets:17:27] diff --git a/ets2panda/test/parser/ets/function_implicit_return_type3-expected.txt b/ets2panda/test/parser/ets/function_implicit_return_type3-expected.txt index 41e4545d599..86af963d88c 100644 --- a/ets2panda/test/parser/ets/function_implicit_return_type3-expected.txt +++ b/ets2panda/test/parser/ets/function_implicit_return_type3-expected.txt @@ -465,4 +465,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [function_implicit_return_type3.ets:18:27] +TypeError: Type '(i: int) => void' cannot be assigned to type '(i: int) => int' [function_implicit_return_type3.ets:18:27] diff --git a/ets2panda/test/parser/ets/function_implicit_return_type6-expected.txt b/ets2panda/test/parser/ets/function_implicit_return_type6-expected.txt index c942bc192dc..924369f75a6 100644 --- a/ets2panda/test/parser/ets/function_implicit_return_type6-expected.txt +++ b/ets2panda/test/parser/ets/function_implicit_return_type6-expected.txt @@ -442,4 +442,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [function_implicit_return_type6.ets:20:16] +TypeError: Type 'void' cannot be assigned to type 'int' [function_implicit_return_type6.ets:20:16] diff --git a/ets2panda/test/parser/ets/function_implicit_return_type7-expected.txt b/ets2panda/test/parser/ets/function_implicit_return_type7-expected.txt index 478d07c0ed0..dd9c583fa77 100644 --- a/ets2panda/test/parser/ets/function_implicit_return_type7-expected.txt +++ b/ets2panda/test/parser/ets/function_implicit_return_type7-expected.txt @@ -598,4 +598,4 @@ } } } -TypeError: Return statement type is not compatible with previous method's return statement type(s). [function_implicit_return_type7.ets:21:10] +TypeError: Function cannot have different primitive return types, found 'boolean', 'int' [function_implicit_return_type7.ets:21:10] diff --git a/ets2panda/test/parser/ets/n_arrayHoldingNullValue-expected.txt b/ets2panda/test/parser/ets/n_arrayHoldingNullValue-expected.txt index d90b7f2cbad..10346aee633 100644 --- a/ets2panda/test/parser/ets/n_arrayHoldingNullValue-expected.txt +++ b/ets2panda/test/parser/ets/n_arrayHoldingNullValue-expected.txt @@ -455,4 +455,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_arrayHoldingNullValue.ets:17:24] +TypeError: Type 'null' cannot be assigned to type 'float[]' [n_arrayHoldingNullValue.ets:17:24] diff --git a/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt b/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt index cf852cc4c9f..57e9c0bfdea 100644 --- a/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableFromFunctionToNonNullable-expected.txt @@ -743,4 +743,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignNullableFromFunctionToNonNullable.ets:23:22] +TypeError: Type 'A|null' cannot be assigned to type 'Object' [n_assignNullableFromFunctionToNonNullable.ets:23:22] diff --git a/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt b/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt index 2f578633792..5698d12fe67 100644 --- a/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableFromMethodToNullableParam-expected.txt @@ -913,4 +913,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignNullableFromMethodToNullableParam.ets:24:22] +TypeError: Type 'A|null' cannot be assigned to type 'Object' [n_assignNullableFromMethodToNullableParam.ets:24:22] diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt index c8a3c89c365..ff8820fa74e 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullable-expected.txt @@ -707,4 +707,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignNullableToNonNullable.ets:22:9] +TypeError: Type 'A|null' cannot be assigned to type 'Object' [n_assignNullableToNonNullable.ets:22:9] diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt index 9b424caf232..c5df8b79393 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullableArray-expected.txt @@ -803,4 +803,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignNullableToNonNullableArray.ets:22:10] +TypeError: Type 'A[]|null' cannot be assigned to type 'Object[]' [n_assignNullableToNonNullableArray.ets:22:10] diff --git a/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt b/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt index 2ac81c66dd1..28ddeff4fb1 100644 --- a/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt +++ b/ets2panda/test/parser/ets/n_assignNullableToNonNullableTypeAlias-expected.txt @@ -776,4 +776,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [n_assignNullableToNonNullableTypeAlias.ets:24:9] +TypeError: Type 'A|null' cannot be assigned to type 'Object' [n_assignNullableToNonNullableTypeAlias.ets:24:9] diff --git a/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt index 5dffc7d900e..229bee6d941 100644 --- a/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callFunctionWithNullableParam-expected.txt @@ -828,4 +828,4 @@ } } } -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [n_callFunctionWithNullableParam.ets:23:9] +TypeError: Type 'A|null' is not compatible with type 'A' at index 1 [n_callFunctionWithNullableParam.ets:23:9] diff --git a/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt index 1aed437dc32..9e0b6dfba65 100644 --- a/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callInterfaceMethodWithNullableParam-expected.txt @@ -1149,4 +1149,4 @@ } } } -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [n_callInterfaceMethodWithNullableParam.ets:27:12] +TypeError: Type 'I|null' is not compatible with type 'I' at index 1 [n_callInterfaceMethodWithNullableParam.ets:27:12] diff --git a/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt b/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt index 66b61478716..d6718171262 100644 --- a/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt +++ b/ets2panda/test/parser/ets/n_callMethodWithNullableParam-expected.txt @@ -858,4 +858,4 @@ } } } -TypeError: Call argument at index 0 is not compatible with the signature's type at that index. [n_callMethodWithNullableParam.ets:23:12] +TypeError: Type 'A|null' is not compatible with type 'A' at index 1 [n_callMethodWithNullableParam.ets:23:12] diff --git a/ets2panda/test/parser/ets/n_returnNullFromFunction-expected.txt b/ets2panda/test/parser/ets/n_returnNullFromFunction-expected.txt index 8d0cbdb7442..1153d45e329 100644 --- a/ets2panda/test/parser/ets/n_returnNullFromFunction-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullFromFunction-expected.txt @@ -451,4 +451,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [n_returnNullFromFunction.ets:19:12] +TypeError: Type 'null' is not compatible with the enclosing method's return type 'A' [n_returnNullFromFunction.ets:19:12] diff --git a/ets2panda/test/parser/ets/n_returnNullFromMethod-expected.txt b/ets2panda/test/parser/ets/n_returnNullFromMethod-expected.txt index 6475dcf314b..62e4dded196 100644 --- a/ets2panda/test/parser/ets/n_returnNullFromMethod-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullFromMethod-expected.txt @@ -451,4 +451,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [n_returnNullFromMethod.ets:18:16] +TypeError: Type 'null' is not compatible with the enclosing method's return type 'A' [n_returnNullFromMethod.ets:18:16] diff --git a/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt b/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt index 7a4275b3329..3c41b4b7afd 100644 --- a/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullableFromFunction-expected.txt @@ -592,4 +592,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [n_returnNullableFromFunction.ets:21:12] +TypeError: Type 'A|null' is not compatible with the enclosing method's return type 'A' [n_returnNullableFromFunction.ets:21:12] diff --git a/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt b/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt index df711d1502c..3c9ac3ca46d 100644 --- a/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt +++ b/ets2panda/test/parser/ets/n_returnNullableFromMethod-expected.txt @@ -592,4 +592,4 @@ } } } -TypeError: Return statement type is not compatible with the enclosing method's return type. [n_returnNullableFromMethod.ets:20:16] +TypeError: Type 'A|null' is not compatible with the enclosing method's return type 'A' [n_returnNullableFromMethod.ets:20:16] diff --git a/ets2panda/test/parser/ets/null_invalid-expected.txt b/ets2panda/test/parser/ets/null_invalid-expected.txt index 1c9ab91eeea..4f1b9e0d7c1 100644 --- a/ets2panda/test/parser/ets/null_invalid-expected.txt +++ b/ets2panda/test/parser/ets/null_invalid-expected.txt @@ -336,4 +336,4 @@ } } } -TypeError: Initializers type is not assignable to the target type [null_invalid.ets:17:18] +TypeError: Type 'Object|null' cannot be assigned to type 'Object' [null_invalid.ets:17:18] diff --git a/ets2panda/test/parser/ets/switch_enum2-expected.txt b/ets2panda/test/parser/ets/switch_enum2-expected.txt index 18f714c8312..59ed0f5ca24 100644 --- a/ets2panda/test/parser/ets/switch_enum2-expected.txt +++ b/ets2panda/test/parser/ets/switch_enum2-expected.txt @@ -577,4 +577,4 @@ } } } -TypeError: Switch case type int is not comparable to discriminant type Color [switch_enum2.ets:20:14] +TypeError: Switch case type 'int' is not comparable to discriminant type 'Color' [switch_enum2.ets:20:14] diff --git a/ets2panda/test/parser/ets/switch_readonly_member_different_enum-expected.txt b/ets2panda/test/parser/ets/switch_readonly_member_different_enum-expected.txt index f34d7e3f924..8ce637786ed 100644 --- a/ets2panda/test/parser/ets/switch_readonly_member_different_enum-expected.txt +++ b/ets2panda/test/parser/ets/switch_readonly_member_different_enum-expected.txt @@ -1293,4 +1293,4 @@ } } } -TypeError: Switch case type A is not comparable to discriminant type E [switch_readonly_member_different_enum.ets:35:14] +TypeError: Switch case type 'A' is not comparable to discriminant type 'E' [switch_readonly_member_different_enum.ets:35:14] diff --git a/ets2panda/test/parser/ets/switch_readonly_member_different_enum_2-expected.txt b/ets2panda/test/parser/ets/switch_readonly_member_different_enum_2-expected.txt index 3ef004f3ec9..5891ffb6e13 100644 --- a/ets2panda/test/parser/ets/switch_readonly_member_different_enum_2-expected.txt +++ b/ets2panda/test/parser/ets/switch_readonly_member_different_enum_2-expected.txt @@ -1293,4 +1293,4 @@ } } } -TypeError: Switch case type A is not comparable to discriminant type E [switch_readonly_member_different_enum_2.ets:34:14] +TypeError: Switch case type 'A' is not comparable to discriminant type 'E' [switch_readonly_member_different_enum_2.ets:34:14] diff --git a/ets2panda/test/unit/public/es2panda_public_test.cpp b/ets2panda/test/unit/public/es2panda_public_test.cpp index fcdef1dd363..2987e79dc31 100644 --- a/ets2panda/test/unit/public/es2panda_public_test.cpp +++ b/ets2panda/test/unit/public/es2panda_public_test.cpp @@ -57,7 +57,7 @@ TEST_F(Es2PandaLibTest, TypeError) impl_->ProceedToState(ctx, ES2PANDA_STATE_ASM_GENERATED); ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_ERROR); ASSERT_EQ(std::string(impl_->ContextErrorMessage(ctx)), - "TypeError: Initializers type is not assignable to the target type[type-error.ets:1,32]"); + "TypeError: Type 'string' cannot be assigned to type 'int'[type-error.ets:1,32]"); impl_->DestroyContext(ctx); } -- Gitee