diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 1f6e386a21bb9dfb0a3a4a6b8966956fd68efb35..c46ea59f9df3f0eb8a68bb4c50249c99dec11e81 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -987,6 +987,14 @@ checker::Type *ETSChecker::CheckVariableDeclaration(ir::Identifier *ident, ir::T init->AsObjectExpression()->SetPreferredType(annotation_type); } + if (type_annotation != nullptr && type_annotation->IsETSFunctionType() && init->IsArrowFunctionExpression()) { + auto *const arrow_func_expr = init->AsArrowFunctionExpression(); + ir::ScriptFunction *const lambda = arrow_func_expr->Function(); + if (lambda->Params().size() == type_annotation->AsETSFunctionType()->Params().size() && + NeedTypeInference(lambda)) { + InferTypesForLambda(lambda, type_annotation->AsETSFunctionType()); + } + } checker::Type *init_type = init->Check(this); if (init_type == nullptr) { diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index b3335689a18c33baa4b690a8167e8452716399ba..acd087976f9f971719ad0f70c50f202e273cb7e1 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -34,7 +34,12 @@ void ETSUnionType::ToString(std::stringstream &ss) const void ETSUnionType::ToAssemblerType(std::stringstream &ss) const { - ss << compiler::Signatures::BUILTIN_OBJECT; + lub_type_->ToAssemblerType(ss); +} + +void ETSUnionType::ToDebugInfoType(std::stringstream &ss) const +{ + lub_type_->ToDebugInfoType(ss); } bool ETSUnionType::EachTypeRelatedToSomeType(TypeRelation *relation, ETSUnionType *source, ETSUnionType *target) @@ -59,6 +64,10 @@ void ETSUnionType::SetLeastUpperBoundType(ETSChecker *checker) lub_type_ = checker->GetGlobalTypesHolder()->GlobalETSObjectType(); return; } + if (t->IsETSObjectType() && t->AsETSObjectType()->SuperType() == nullptr) { + lub_type_ = checker->GetGlobalTypesHolder()->GlobalETSObjectType(); + return; + } lub_type_ = checker->FindLeastUpperBound(lub_type_, t); } } @@ -140,7 +149,7 @@ Type *ETSUnionType::HandleUnionType(ETSUnionType *union_type) Type *ETSUnionType::Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) { - ArenaVector copied_constituents(constituent_types_.size(), allocator->Adapter()); + ArenaVector copied_constituents(allocator->Adapter()); for (auto *it : constituent_types_) { copied_constituents.push_back(it->HasTypeFlag(checker::TypeFlag::ETS_PRIMITIVE) @@ -158,6 +167,16 @@ Type *ETSUnionType::Instantiate(ArenaAllocator *allocator, TypeRelation *relatio return HandleUnionType(new_union_type); } +Type *ETSUnionType::Substitute(TypeRelation *relation, const Substitution *substitution) +{ + auto *const checker = relation->GetChecker()->AsETSChecker(); + ArenaVector substituted_constituents(checker->Allocator()->Adapter()); + for (auto *ctype : constituent_types_) { + substituted_constituents.push_back(ctype->Substitute(relation, substitution)); + } + return checker->CreateETSUnionType(std::move(substituted_constituents)); +} + void ETSUnionType::Cast(TypeRelation *relation, Type *target) { auto *const checker = relation->GetChecker()->AsETSChecker(); diff --git a/ets2panda/checker/types/ets/etsUnionType.h b/ets2panda/checker/types/ets/etsUnionType.h index ce032fcef4712cb86bab8f42048c78a0057a698d..081bd7ae4364fa6643b217693e938f194004b966 100644 --- a/ets2panda/checker/types/ets/etsUnionType.h +++ b/ets2panda/checker/types/ets/etsUnionType.h @@ -62,10 +62,12 @@ public: void ToString(std::stringstream &ss) const override; void ToAssemblerType(std::stringstream &ss) const override; + void ToDebugInfoType(std::stringstream &ss) const override; void Identical(TypeRelation *relation, Type *other) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; bool AssignmentSource(TypeRelation *relation, Type *target) override; Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *global_types) override; + Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; void CastToThis(TypeRelation *relation, Type *source); Type *FindTypeIsCastableToThis(ir::Expression *node, TypeRelation *relation, Type *source) const; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index ddbd2de9a45528a7cd2b147dc38a6264888d2e01..568ac0f8b56e7aaa841e838a5bee7f5d2e849e85 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -3858,7 +3858,11 @@ ir::Expression *ETSParser::ParseDefaultPrimaryExpression(ExpressionParseFlags fl Lexer()->Rewind(saved_pos); - if (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT) { + Lexer()->NextToken(); + bool pretend_arrow = Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_ARROW; + Lexer()->Rewind(saved_pos); + + if (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && !pretend_arrow) { return ParsePrimaryExpressionIdent(flags); } diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..005762970dd4dfb9a13c822bca67bd42e7b92f94 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1-expected.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected token ''. [lambda_infer_type_param1.ets:17:38] diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1.ets b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1.ets new file mode 100644 index 0000000000000000000000000000000000000000..b153b5f426f55f6a785fd86dd3ea6e6943cf87d4 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param1.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. + */ + +function main() { + let x: (value: float) => float = value => 3 as float // CTE +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..44862ac8933321671364870ce2aaee217f559dfe --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2-expected.txt @@ -0,0 +1,489 @@ +{ + "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": "x", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 25 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 35 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": true, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 44 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "TSAsExpression", + "expression": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 17, + "column": 49 + }, + "end": { + "line": 17, + "column": 50 + } + } + }, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 54 + }, + "end": { + "line": 17, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 49 + }, + "end": { + "line": 17, + "column": 50 + } + } + }, + "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": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 50 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 50 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "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": 19, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2.ets b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2.ets new file mode 100644 index 0000000000000000000000000000000000000000..978778c6f97c7792ade1574a99b1399e0186e523 --- /dev/null +++ b/ets2panda/test/parser/ets/lambda_infer_type/lambda_infer_type_param2.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. + */ + +function main() { + let x: (value: float) => float = (value) => 3 as float +} diff --git a/ets2panda/test/runtime/ets/union_type_parameters.ets b/ets2panda/test/runtime/ets/union_type_parameters.ets new file mode 100644 index 0000000000000000000000000000000000000000..b305590d444c05fb53a2ba95eb214e451df3f611 --- /dev/null +++ b/ets2panda/test/runtime/ets/union_type_parameters.ets @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021-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 A{} +class B{} +class C implements A{} +class D{} +function foo(x: A|B) { + if (x instanceof A) { + return 41; + } else if (x instanceof B) { + return 42; + } + return -1; +} + +function main() { + assert foo(new B()) == 42: "Error! branch with return 42 must be executed"; + assert foo(new C()) == 41: "Error! branch with return 41 must be executed"; +}