diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 9e26143b69d04766ede7873bbab22abb01dd8671..95339cc9fde793b58aadc30fdee9b5d0cc483db4 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 0000000000000000000000000000000000000000..ad9df595f38b54c0e55d29aefb7dd4deaf1bac63 --- /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