diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 242a225d3fd..5ed17e1e1dc 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -2330,7 +2330,8 @@ Bytecode::CodeGenerationErrorOr> IfStatement::generate_b auto& true_block = generator.make_block(); auto& false_block = generator.make_block(); - auto& end_block = generator.make_block(); + // NOTE: if there is no 'else' block the end block is the same as the false block + auto& end_block = m_alternate ? generator.make_block() : false_block; Optional completion; if (generator.must_propagate_completion()) { @@ -2347,25 +2348,19 @@ Bytecode::CodeGenerationErrorOr> IfStatement::generate_b generator.switch_to_basic_block(true_block); auto consequent = TRY(m_consequent->generate_bytecode(generator, completion)); if (!generator.is_current_block_terminated()) { - if (generator.must_propagate_completion()) { - if (consequent.has_value()) - generator.emit(*completion, *consequent); - } + if (generator.must_propagate_completion() && consequent.has_value()) + generator.emit(*completion, *consequent); generator.emit(Bytecode::Label { end_block }); } - generator.switch_to_basic_block(false_block); - - Optional alternate; if (m_alternate) { - alternate = TRY(m_alternate->generate_bytecode(generator, completion)); - } - if (!generator.is_current_block_terminated()) { - if (generator.must_propagate_completion()) { - if (alternate.has_value()) + generator.switch_to_basic_block(false_block); + auto alternate = TRY(m_alternate->generate_bytecode(generator, completion)); + if (!generator.is_current_block_terminated()) { + if (generator.must_propagate_completion() && alternate.has_value()) generator.emit(*completion, *alternate); + generator.emit(Bytecode::Label { end_block }); } - generator.emit(Bytecode::Label { end_block }); } generator.switch_to_basic_block(end_block);