From 9cbd86ec197599f7ff3de9f7f93e6337b5ffb884 Mon Sep 17 00:00:00 2001 From: Gergo Csizi Date: Thu, 15 Feb 2024 08:26:46 +0100 Subject: [PATCH] Fix pritive conversion on class fields Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I91NYO Internal issue: #15735 Test: new runtime test Signed-off-by: Gergo Csizi --- ets2panda/compiler/core/ETSCompiler.cpp | 2 +- .../test/runtime/ets/fieldImplicitCasting.ets | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/fieldImplicitCasting.ets diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 1cbdb9ff54..eb59c2e753 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -69,7 +69,7 @@ void ETSCompiler::Compile(const ir::ClassProperty *st) const if (!etsg->TryLoadConstantExpression(st->Value())) { st->Value()->Compile(etsg); - etsg->ApplyConversion(st->Value(), nullptr); + etsg->ApplyConversion(st->Value(), st->TsType()); } if (st->IsStatic()) { diff --git a/ets2panda/test/runtime/ets/fieldImplicitCasting.ets b/ets2panda/test/runtime/ets/fieldImplicitCasting.ets new file mode 100644 index 0000000000..9b7f6a93a8 --- /dev/null +++ b/ets2panda/test/runtime/ets/fieldImplicitCasting.ets @@ -0,0 +1,57 @@ +/* + * 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 A1 { + private bSource: byte = 0 as byte + private sSource: short = 1 as short + private iSource: int = 2 as int + private lSource: long = 3 as long + private fSource: float = 4 as float + private cSource: char = 5 as char + public blTarget: long = this.bSource + public bfTarget: float = this.bSource + public bdTarget: double = this.bSource + public slTarget: long = this.sSource + public sfTarget: float = this.sSource + public sdTarget: double = this.sSource + public ilTarget: long = this.iSource + public ifTarget: float = this.iSource + public idTarget: double = this.iSource + public lfTarget: float = this.lSource + public ldTarget: double = this.lSource + public fdTarget: double = this.fSource + public clTarget: long = this.cSource + public cfTarget: float = this.cSource + public cdTarget: double = this.cSource +} + +function main(): void { + let a1: A1 = new A1() + assert (a1.blTarget == 0) + assert (a1.bfTarget == 0.0) + assert (a1.bdTarget == 0.0) + assert (a1.slTarget == 1) + assert (a1.sfTarget == 1.0) + assert (a1.sdTarget == 1.0) + assert (a1.ilTarget == 2) + assert (a1.ifTarget == 2.0) + assert (a1.idTarget == 2) + assert (a1.lfTarget == 3.0) + assert (a1.ldTarget == 3.0) + assert (a1.fdTarget == 4.0) + assert (a1.clTarget == 5) + assert (a1.cfTarget == 5.0) + assert (a1.cdTarget == 5.0) +} -- Gitee