From c0cec046f3f92b0a347b7868bfdc4b122d5aaea9 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Tue, 12 Aug 2025 16:46:30 +0800 Subject: [PATCH] Fix issue when defaultInex of switch is 0 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSLIQ Signed-off-by: anjiaqi --- ets2panda/compiler/core/ETSCompiler.cpp | 4 +- .../test/runtime/ets/switch_default_case.ets | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/switch_default_case.ets diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 9e26143b69..95339cc9fd 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -1454,7 +1454,7 @@ static void CompileImpl(const ir::SwitchStatement *self, ETSGen *etsg) compiler::VReg tag = etsg->AllocReg(); builder.CompileTagOfSwitch(tag); - uint32_t defaultIndex = 0; + int32_t defaultIndex = -1; for (size_t i = 0; i < self->Cases().size(); i++) { const auto *clause = self->Cases()[i]; @@ -1467,7 +1467,7 @@ static void CompileImpl(const ir::SwitchStatement *self, ETSGen *etsg) builder.JumpIfCase(tag, i); } - if (defaultIndex > 0) { + if (defaultIndex >= 0) { builder.JumpToDefault(defaultIndex); } else { builder.Break(); diff --git a/ets2panda/test/runtime/ets/switch_default_case.ets b/ets2panda/test/runtime/ets/switch_default_case.ets new file mode 100644 index 0000000000..ad9df595f3 --- /dev/null +++ b/ets2panda/test/runtime/ets/switch_default_case.ets @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 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 { + status: int = 0; + first: int = 0; + second: int = 0; + + errorAlert(): void { + this.status = 2; + } + + incFirst(): void { + this.first++; + } + + incSecond(): void { + this.second++; + } + + run(): int { + switch (5) { + default: + this.incFirst(); + this.incSecond(); + break; + case 2: + this.errorAlert(); + break; + } + if (this.first !== 1) this.errorAlert(); + if (this.second !== 1) this.errorAlert(); + return this.status; + } +} + +function main() { + arktest.assertEQ(new A().run(), 0) +} \ No newline at end of file -- Gitee