From f3b8e5f9561fc55d13982280d75786df78fb5e71 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Tue, 15 Aug 2023 14:48:17 +0800 Subject: [PATCH] Fix switchStatement with empty body Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I7TC44 Signed-off-by: ctw-ian Change-Id: I7103eefb1b7ea7117fa22769d269a60195316810 --- es2panda/ir/statements/switchStatement.cpp | 4 ++++ .../switchStatement-empty-body-expected.txt | 1 + .../switchStatement-empty-body.js | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 es2panda/test/compiler/js/switchStatement/switchStatement-empty-body-expected.txt create mode 100644 es2panda/test/compiler/js/switchStatement/switchStatement-empty-body.js diff --git a/es2panda/ir/statements/switchStatement.cpp b/es2panda/ir/statements/switchStatement.cpp index 4e51aa02e4..38c5c90f26 100644 --- a/es2panda/ir/statements/switchStatement.cpp +++ b/es2panda/ir/statements/switchStatement.cpp @@ -51,6 +51,10 @@ void SwitchStatement::Compile(compiler::PandaGen *pg) const compiler::LocalRegScope lrs(pg, scope_); uint32_t defaultIndex = 0; + if (cases_.size() == 0) { + return; + } + for (size_t i = 0; i < cases_.size(); i++) { const auto *clause = cases_[i]; diff --git a/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body-expected.txt b/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body-expected.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body.js b/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body.js new file mode 100644 index 0000000000..1dbfe66982 --- /dev/null +++ b/es2panda/test/compiler/js/switchStatement/switchStatement-empty-body.js @@ -0,0 +1,21 @@ +/* + * 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. + */ + +let i = 1; +switch (i) { + +} + +print(i); \ No newline at end of file -- Gitee