From 493888b0753af4eed9796cc6556c89390a946bef Mon Sep 17 00:00:00 2001 From: zmw Date: Wed, 6 Aug 2025 10:39:48 +0800 Subject: [PATCH] Fix switch with non case crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRFND Signed-off-by: zmw --- ets2panda/checker/checkerContext.cpp | 3 ++- .../compiler/ets/stwitch_with_non_case.ets | 21 ++++++++++++++++ .../test/runtime/ets/switch_with_non_case.ets | 24 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets create mode 100644 ets2panda/test/runtime/ets/switch_with_non_case.ets diff --git a/ets2panda/checker/checkerContext.cpp b/ets2panda/checker/checkerContext.cpp index 28bf22ca92..fa10d82fac 100644 --- a/ets2panda/checker/checkerContext.cpp +++ b/ets2panda/checker/checkerContext.cpp @@ -494,7 +494,8 @@ void CheckerContext::AddBreakSmartCasts(ir::Statement const *targetStatement, Sm void CheckerContext::CombineBreakSmartCasts(ir::Statement const *targetStatement) { - ES2PANDA_ASSERT(smartCasts_.empty()); + ES2PANDA_ASSERT((targetStatement->IsSwitchStatement() && targetStatement->AsSwitchStatement()->Cases().empty()) || + smartCasts_.empty()); if (!breakSmartCasts_.empty()) { bool firstCase = true; diff --git a/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets b/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets new file mode 100644 index 0000000000..9741d6c34d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/stwitch_with_non_case.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +function foo(x: int): string { + let rc = "default"; + switch (x) { } +} + +/* @@? 16:10 Error TypeError: Function with a non void return type must return a value. */ diff --git a/ets2panda/test/runtime/ets/switch_with_non_case.ets b/ets2panda/test/runtime/ets/switch_with_non_case.ets new file mode 100644 index 0000000000..12c625abe8 --- /dev/null +++ b/ets2panda/test/runtime/ets/switch_with_non_case.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +function foo(x: int): string { + let rc = "default"; + switch (x) { } + return rc; +} + +function main() { + arktest.assertEQ(foo(1), "default"); +} -- Gitee