diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index f85b20e3bb57c7db113cad1c8417804cab8e0f72..5d9a674a95083a0f9ef00ba52eef5e40c263408f 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -33,7 +33,7 @@ namespace panda::es2panda::compiler { static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) { - bool hasReturn = false; + bool endReturn = false; const auto &statements = block->Statements(); pg->SetFirstStmt(statements.empty() ? block : statements.front()); @@ -41,12 +41,12 @@ static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) for (const auto *stmt : statements) { stmt->Compile(pg); - if (stmt->IsReturnStatement()) { - hasReturn = true; + if (stmt->IsReturnStatement() && (stmt == statements[statements.size() - 1])) { + endReturn = true; } } - if (hasReturn) { + if (endReturn) { return; } diff --git a/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js new file mode 100644 index 0000000000000000000000000000000000000000..326967778015b11c07d444a76680dffba47495b2 --- /dev/null +++ b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js @@ -0,0 +1,29 @@ +/* + * 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. + */ + + +function f() {} + +function f0() { + print(1); + return; + try { + f(); + } catch (e) { + + } +} + +f0();