From f1e7ae236ba67cab6d6f33470451c1e5b9732824 Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Fri, 10 Nov 2023 21:27:59 +0300 Subject: [PATCH] Fix implicit generic call with generic lambda parameter Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8G6SE Signed-off-by: Ilya Trubachev --- ets2panda/checker/ETSchecker.h | 7 +- ets2panda/checker/ets/function.cpp | 46 +- ets2panda/checker/ets/function_helpers.h | 3 +- .../checker/types/ets/etsFunctionType.cpp | 36 +- .../generics_implicit_lambda1-expected.txt | 2178 +++++++++++++++++ .../ets/generics_implicit_lambda1.ets | 44 + .../generics_implicit_lambda2-expected.txt | 1085 ++++++++ .../ets/generics_implicit_lambda2.ets | 20 + 8 files changed, 3391 insertions(+), 28 deletions(-) create mode 100644 ets2panda/test/compiler/ets/generics_implicit_lambda1-expected.txt create mode 100644 ets2panda/test/compiler/ets/generics_implicit_lambda1.ets create mode 100644 ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt create mode 100644 ets2panda/test/compiler/ets/generics_implicit_lambda2.ets diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 756489ee87..0c05f7557c 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -266,8 +266,13 @@ public: { return Allocator()->New(*src); } + ArenaUnorderedSet *NewInstantiatedTypeParamsSet() + { + return Allocator()->New>(Allocator()->Adapter()); + } void EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, Type *argument_type, - Substitution *substitution); + Substitution *substitution, + ArenaUnorderedSet *instantiated_type_params); Signature *ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *type_arguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, TypeRelationFlag initial_flags, const std::vector &arg_type_inference_required); diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 7e6538ad77..a2f43fc6c8 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -96,7 +96,8 @@ bool ETSChecker::IsCompatibleTypeArgument(Type *type_param, Type *type_argument) /* A very rough and imprecise partial type inference */ void ETSChecker::EnhanceSubstitutionForType(const ArenaVector &type_params, Type *param_type, - Type *argument_type, Substitution *substitution) + Type *argument_type, Substitution *substitution, + ArenaUnorderedSet *instantiated_type_params) { if (!param_type->IsETSObjectType()) { return; @@ -104,24 +105,47 @@ void ETSChecker::EnhanceSubstitutionForType(const ArenaVector &type_para auto *param_obj_type = param_type->AsETSObjectType(); if (param_obj_type->HasObjectFlag(ETSObjectFlags::TYPE_PARAMETER)) { auto *param_base = GetOriginalBaseType(param_obj_type); + if (instantiated_type_params->find(param_obj_type) != instantiated_type_params->end() && + substitution->at(param_base) != argument_type) { + ThrowTypeError({"Type parameter already instantiated with another type "}, + param_obj_type->GetDeclNode()->Start()); + } if (std::find(type_params.begin(), type_params.end(), param_base) != type_params.end() && substitution->count(param_base) == 0) { substitution->emplace(param_base, argument_type); + instantiated_type_params->insert(param_obj_type); return; } } - if (!argument_type->IsETSObjectType()) { + if (argument_type->IsETSObjectType()) { + auto *arg_obj_type = argument_type->AsETSObjectType(); + if (GetOriginalBaseType(arg_obj_type) != GetOriginalBaseType(param_obj_type)) { + return; // don't attempt anything fancy for now + } + ASSERT(arg_obj_type->TypeArguments().size() == param_obj_type->TypeArguments().size()); + for (size_t ix = 0; ix < arg_obj_type->TypeArguments().size(); ix++) { + EnhanceSubstitutionForType(type_params, param_obj_type->TypeArguments()[ix], + arg_obj_type->TypeArguments()[ix], substitution, instantiated_type_params); + } + } else if (argument_type->IsETSFunctionType() && + param_obj_type->HasObjectFlag(ETSObjectFlags::FUNCTIONAL_INTERFACE)) { + auto parameter_signatures = param_obj_type->GetOwnProperty("invoke") + ->TsType() + ->AsETSFunctionType() + ->CallSignatures(); + auto argument_signatures = argument_type->AsETSFunctionType()->CallSignatures(); + ASSERT(argument_signatures.size() == 1); + ASSERT(parameter_signatures.size() == 1); + for (size_t idx = 0; idx < argument_signatures[0]->GetSignatureInfo()->params.size(); idx++) { + EnhanceSubstitutionForType(type_params, parameter_signatures[0]->GetSignatureInfo()->params[idx]->TsType(), + argument_signatures[0]->GetSignatureInfo()->params[idx]->TsType(), substitution, + instantiated_type_params); + } + EnhanceSubstitutionForType(type_params, parameter_signatures[0]->ReturnType(), + argument_signatures[0]->ReturnType(), substitution, instantiated_type_params); + } else { return; } - auto *arg_obj_type = argument_type->AsETSObjectType(); - if (GetOriginalBaseType(arg_obj_type) != GetOriginalBaseType(param_obj_type)) { - return; // don't attempt anything fancy for now - } - ASSERT(arg_obj_type->TypeArguments().size() == param_obj_type->TypeArguments().size()); - for (size_t ix = 0; ix < arg_obj_type->TypeArguments().size(); ix++) { - EnhanceSubstitutionForType(type_params, param_obj_type->TypeArguments()[ix], arg_obj_type->TypeArguments()[ix], - substitution); - } } // NOLINTBEGIN(modernize-avoid-c-arrays) diff --git a/ets2panda/checker/ets/function_helpers.h b/ets2panda/checker/ets/function_helpers.h index c112c116f7..76524b98d1 100644 --- a/ets2panda/checker/ets/function_helpers.h +++ b/ets2panda/checker/ets/function_helpers.h @@ -86,6 +86,7 @@ static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *che const ArenaVector &arguments) { Substitution *substitution = checker->NewSubstitution(); + auto *instantiated_type_params = checker->NewInstantiatedTypeParamsSet(); auto *sig_info = signature->GetSignatureInfo(); ArenaVector &type_params = sig_info->type_params; for (size_t ix = 0; ix < arguments.size(); ix++) { @@ -100,7 +101,7 @@ static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *che if (param_type == nullptr) { continue; } - checker->EnhanceSubstitutionForType(type_params, param_type, arg_type, substitution); + checker->EnhanceSubstitutionForType(type_params, param_type, arg_type, substitution, instantiated_type_params); } return substitution; } diff --git a/ets2panda/checker/types/ets/etsFunctionType.cpp b/ets2panda/checker/types/ets/etsFunctionType.cpp index b67c53ca99..9a62df6fc0 100644 --- a/ets2panda/checker/types/ets/etsFunctionType.cpp +++ b/ets2panda/checker/types/ets/etsFunctionType.cpp @@ -70,21 +70,9 @@ bool ETSFunctionType::AssignmentSource(TypeRelation *relation, Type *target) return false; } -void ETSFunctionType::AssignmentTarget(TypeRelation *relation, Type *source) +static Signature *ProcessSignatures(TypeRelation *relation, Signature *target, ETSFunctionType *source_func_type) { - if (!source->IsETSFunctionType() && - (!source->IsETSObjectType() || !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL))) { - return; - } - - ASSERT(call_signatures_.size() == 1 && call_signatures_[0]->HasSignatureFlag(SignatureFlags::TYPE)); - - Signature *target = call_signatures_[0]; Signature *match {}; - bool source_is_functional = source->IsETSObjectType(); - auto *source_func_type = source_is_functional ? source->AsETSObjectType()->GetFunctionalInterfaceInvokeType() - : source->AsETSFunctionType(); - for (auto *it : source_func_type->CallSignatures()) { if (target->MinArgCount() != it->MinArgCount()) { continue; @@ -97,15 +85,16 @@ void ETSFunctionType::AssignmentTarget(TypeRelation *relation, Type *source) if (!it->GetSignatureInfo()->type_params.empty()) { auto *substitution = relation->GetChecker()->AsETSChecker()->NewSubstitution(); + auto *instantiated_type_params = relation->GetChecker()->AsETSChecker()->NewInstantiatedTypeParamsSet(); for (size_t ix = 0; ix < target->MinArgCount(); ix++) { relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( it->GetSignatureInfo()->type_params, it->GetSignatureInfo()->params[ix]->TsType(), - target->GetSignatureInfo()->params[ix]->TsType(), substitution); + target->GetSignatureInfo()->params[ix]->TsType(), substitution, instantiated_type_params); } if (target->RestVar() != nullptr) { relation->GetChecker()->AsETSChecker()->EnhanceSubstitutionForType( it->GetSignatureInfo()->type_params, it->RestVar()->TsType(), target->RestVar()->TsType(), - substitution); + substitution, instantiated_type_params); } it = it->Substitute(relation, substitution); } @@ -133,6 +122,23 @@ void ETSFunctionType::AssignmentTarget(TypeRelation *relation, Type *source) match = it; break; } + return match; +} + +void ETSFunctionType::AssignmentTarget(TypeRelation *relation, Type *source) +{ + if (!source->IsETSFunctionType() && + (!source->IsETSObjectType() || !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL))) { + return; + } + + ASSERT(call_signatures_.size() == 1 && call_signatures_[0]->HasSignatureFlag(SignatureFlags::TYPE)); + + Signature *target = call_signatures_[0]; + bool source_is_functional = source->IsETSObjectType(); + auto *source_func_type = source_is_functional ? source->AsETSObjectType()->GetFunctionalInterfaceInvokeType() + : source->AsETSFunctionType(); + Signature *match = ProcessSignatures(relation, target, source_func_type); if (match == nullptr) { relation->Result(false); diff --git a/ets2panda/test/compiler/ets/generics_implicit_lambda1-expected.txt b/ets2panda/test/compiler/ets/generics_implicit_lambda1-expected.txt new file mode 100644 index 0000000000..6e86df9a9d --- /dev/null +++ b/ets2panda/test/compiler/ets/generics_implicit_lambda1-expected.txt @@ -0,0 +1,2178 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "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": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "first", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 34 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "second", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 43 + }, + "end": { + "line": 22, + "column": 54 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 35 + }, + "end": { + "line": 22, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 35 + }, + "end": { + "line": 22, + "column": 54 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 59 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 15 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "first", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 15 + }, + "end": { + "line": 23, + "column": 33 + } + } + } + ], + "kind": "const", + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "second", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 15 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 9 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 58 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "invoke", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 18 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "invoke", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 28, + "column": 18 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "first", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 35 + }, + "end": { + "line": 28, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 35 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 35 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 29 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "second", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 52 + }, + "end": { + "line": 28, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 52 + }, + "end": { + "line": 28, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 52 + }, + "end": { + "line": 28, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 46 + }, + "end": { + "line": 28, + "column": 57 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 38 + }, + "end": { + "line": 28, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 38 + }, + "end": { + "line": 28, + "column": 57 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 59 + }, + "end": { + "line": 28, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 59 + }, + "end": { + "line": 28, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 59 + }, + "end": { + "line": 28, + "column": 62 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 19 + }, + "end": { + "line": 28, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 19 + }, + "end": { + "line": 28, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 28, + "column": 21 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 15 + }, + "end": { + "line": 29, + "column": 23 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "first", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 31 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 15 + }, + "end": { + "line": 29, + "column": 33 + } + } + } + ], + "kind": "const", + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "second", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 15 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 17 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 31, + "column": 16 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 61 + }, + "end": { + "line": 32, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 32, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 18 + }, + "end": { + "line": 32, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 32, + "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": 33, + "column": 2 + }, + "end": { + "line": 33, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 33, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "first", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 30 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 24 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 32 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "first", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 29 + } + } + } + ], + "kind": "const", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 29 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "instance", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 36 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 10 + }, + "end": { + "line": 35, + "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": 35, + "column": 10 + }, + "end": { + "line": 35, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 13 + }, + "end": { + "line": 36, + "column": 22 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "XXX", + "loc": { + "start": { + "line": 36, + "column": 32 + }, + "end": { + "line": 36, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 25 + }, + "end": { + "line": 36, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 36, + "column": 23 + }, + "end": { + "line": 36, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 9 + }, + "end": { + "line": 36, + "column": 39 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 36, + "column": 5 + }, + "end": { + "line": 36, + "column": 41 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 38, + "column": 13 + }, + "end": { + "line": 38, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 38, + "column": 29 + }, + "end": { + "line": 38, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 22 + }, + "end": { + "line": 38, + "column": 31 + } + } + } + ], + "loc": { + "start": { + "line": 38, + "column": 20 + }, + "end": { + "line": 38, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 38, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 9 + }, + "end": { + "line": 38, + "column": 33 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 38, + "column": 5 + }, + "end": { + "line": 38, + "column": 35 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 40, + "column": 6 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 41, + "column": 9 + }, + "end": { + "line": 41, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 9 + }, + "end": { + "line": 41, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 9 + }, + "end": { + "line": 41, + "column": 18 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "XXX", + "loc": { + "start": { + "line": 41, + "column": 28 + }, + "end": { + "line": 41, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 21 + }, + "end": { + "line": 41, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 41, + "column": 19 + }, + "end": { + "line": 41, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 5 + }, + "end": { + "line": 41, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 41, + "column": 5 + }, + "end": { + "line": 41, + "column": 35 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 42, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 40, + "column": 5 + }, + "end": { + "line": 42, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 35, + "column": 17 + }, + "end": { + "line": 44, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 14 + }, + "end": { + "line": 44, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 35, + "column": 14 + }, + "end": { + "line": 44, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 35, + "column": 1 + }, + "end": { + "line": 44, + "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": 44, + "column": 2 + } + } +} diff --git a/ets2panda/test/compiler/ets/generics_implicit_lambda1.ets b/ets2panda/test/compiler/ets/generics_implicit_lambda1.ets new file mode 100644 index 0000000000..4a3a18e2c8 --- /dev/null +++ b/ets2panda/test/compiler/ets/generics_implicit_lambda1.ets @@ -0,0 +1,44 @@ +/* + * 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 foo(first: () => T): T { + const instance = first() + return instance +} + +class X { + static foo(first: () => T, second: () => void): T { + const instance = first() + second() + return instance + } + + static invoke(first: () => T, second: () => void): T { + const instance = first() + second() + return instance + } +} + +function main() { + foo((): String => { return "XXX" }); + + foo((): int => { return 42 }); + + X( + (): String => { return "XXX" }, + ) { + } +} \ No newline at end of file diff --git a/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt b/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt new file mode 100644 index 0000000000..642f8dc36d --- /dev/null +++ b/ets2panda/test/compiler/ets/generics_implicit_lambda2-expected.txt @@ -0,0 +1,1085 @@ +{ + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "first", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 28 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 37 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 37 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 37 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "c", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 43 + }, + "end": { + "line": 16, + "column": 45 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 40 + }, + "end": { + "line": 16, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 40 + }, + "end": { + "line": 16, + "column": 45 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 51 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 54 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 53 + }, + "end": { + "line": 16, + "column": 56 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 55 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "c", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 32 + }, + "end": { + "line": 19, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 32 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 32 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 29 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 29 + }, + "end": { + "line": 19, + "column": 39 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 41 + }, + "end": { + "line": 19, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 41 + }, + "end": { + "line": 19, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 41 + }, + "end": { + "line": 19, + "column": 50 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "XXX", + "loc": { + "start": { + "line": 19, + "column": 60 + }, + "end": { + "line": 19, + "column": 65 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 53 + }, + "end": { + "line": 19, + "column": 65 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 51 + }, + "end": { + "line": 19, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 67 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 67 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "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": 20, + "column": 2 + } + } +} +TypeError: Type parameter already instantiated with another type [generics_implicit_lambda2.ets:16:17] diff --git a/ets2panda/test/compiler/ets/generics_implicit_lambda2.ets b/ets2panda/test/compiler/ets/generics_implicit_lambda2.ets new file mode 100644 index 0000000000..df06746de6 --- /dev/null +++ b/ets2panda/test/compiler/ets/generics_implicit_lambda2.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. + */ + +function foo(first: (a: U, b: T, c: U) => T): T {} + +function main() { + foo((a: int, b: String, c: String): String => { return "XXX" }); +} \ No newline at end of file -- Gitee