diff --git a/ets2panda/checker/checkerContext.cpp b/ets2panda/checker/checkerContext.cpp index b8da863c9b340d571fb4daa2bd1bab4c91eeea8f..28bf22ca9231c641dc7fb9f1b5f876d75065cc7e 100644 --- a/ets2panda/checker/checkerContext.cpp +++ b/ets2panda/checker/checkerContext.cpp @@ -473,10 +473,6 @@ void CheckerContext::OnBreakStatement(ir::BreakStatement const *breakStatement) status_ |= CheckerStatus::MEET_BREAK; - if (smartCasts_.empty()) { - return; - } - SmartCastArray smartCasts {}; smartCasts.reserve(smartCasts_.size()); @@ -486,9 +482,7 @@ void CheckerContext::OnBreakStatement(ir::BreakStatement const *breakStatement) } } - if (!smartCasts.empty()) { - AddBreakSmartCasts(targetStatement, std::move(smartCasts)); - } + AddBreakSmartCasts(targetStatement, std::move(smartCasts)); ClearSmartCasts(); } diff --git a/ets2panda/test/ast/compiler/ets/smart_cast_while_test_mod.sts b/ets2panda/test/ast/compiler/ets/smart_cast_while_test_mod.ets similarity index 100% rename from ets2panda/test/ast/compiler/ets/smart_cast_while_test_mod.sts rename to ets2panda/test/ast/compiler/ets/smart_cast_while_test_mod.ets diff --git a/ets2panda/test/ast/compiler/ets/smart_cast_wuth_break.ets b/ets2panda/test/ast/compiler/ets/smart_cast_wuth_break.ets new file mode 100644 index 0000000000000000000000000000000000000000..aedd842942ecc7d3995d365635d377ee9ba0d2a9 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/smart_cast_wuth_break.ets @@ -0,0 +1,35 @@ +/* + * 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(str: string | undefined): undefined { + let x: string | undefined = undefined; + for (let i = 0; i < 1; ++i) { + x = str; + if (x == "a") { + break; + } else { + x = undefined; + } + } + return /* @@ label */x; +} + + +function main() { + console.log(foo("a")); +} + +/* @@@ label Error TypeError: Type 'String|undefined' is not compatible with the enclosing method's return type 'undefined' */