From bfabac26f8da1936cc02d5765da00f129ae26d69 Mon Sep 17 00:00:00 2001 From: Vyacheslav Cherkashin Date: Mon, 10 Apr 2023 18:57:20 +0300 Subject: [PATCH] Fix returnStatement Signed-off-by: Vyacheslav Cherkashin --- ir/statements/returnStatement.cpp | 6 ++++-- ir/statements/returnStatement.h | 1 + test/runtime/ets/ir-returnStatement.ets | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/runtime/ets/ir-returnStatement.ets diff --git a/ir/statements/returnStatement.cpp b/ir/statements/returnStatement.cpp index 7bd586c62..33e8635ab 100644 --- a/ir/statements/returnStatement.cpp +++ b/ir/statements/returnStatement.cpp @@ -92,6 +92,7 @@ void ReturnStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const etsg->LoadAccumulator(this, res); } + etsg->ApplyConversion(this, return_type_); etsg->ReturnAcc(this); } @@ -145,6 +146,7 @@ checker::Type *ReturnStatement::Check(checker::ETSChecker *checker) auto *const func_return_type = containing_func->ReturnTypeAnnotation() != nullptr ? containing_func->ReturnTypeAnnotation()->GetType(checker) : containing_func->Signature()->ReturnType(); + return_type_ = func_return_type; if (argument_ == nullptr && func_return_type->IsETSVoidType()) { return nullptr; @@ -159,10 +161,10 @@ checker::Type *ReturnStatement::Check(checker::ETSChecker *checker) } ASSERT(argument_); - checker::Type *return_type = argument_->Check(checker); + checker::Type *argument_type = argument_->Check(checker); checker::AssignmentContext( - checker->Relation(), argument_, return_type, func_return_type, argument_->Start(), + checker->Relation(), argument_, argument_type, func_return_type, argument_->Start(), {"Return statements return type is not compatible with the containing functions return type"}, checker::TypeRelationFlag::DIRECT_RETURN); diff --git a/ir/statements/returnStatement.h b/ir/statements/returnStatement.h index 90d6bf6ca..35daf5682 100644 --- a/ir/statements/returnStatement.h +++ b/ir/statements/returnStatement.h @@ -43,6 +43,7 @@ public: private: Expression *argument_ {}; + checker::Type *return_type_ {}; }; } // namespace panda::es2panda::ir diff --git a/test/runtime/ets/ir-returnStatement.ets b/test/runtime/ets/ir-returnStatement.ets new file mode 100644 index 000000000..9caa1cb40 --- /dev/null +++ b/test/runtime/ets/ir-returnStatement.ets @@ -0,0 +1,25 @@ +/* + * 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 get_value(v: short): long { + return v; +} + +function main(): void { + let v: short = 5; + assert(v == get_value(v)); + return; +} -- Gitee