From 15b827f180998ea6c3b29249a4d9412701933dac Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 5 Jan 2024 16:03:17 +0800 Subject: [PATCH] HIR/MIR - Handle `break return` properly. --- src/hir_typeck/expr_cs__enum.cpp | 2 +- src/mir/from_hir.cpp | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/hir_typeck/expr_cs__enum.cpp b/src/hir_typeck/expr_cs__enum.cpp index 74a30d935..63664757d 100644 --- a/src/hir_typeck/expr_cs__enum.cpp +++ b/src/hir_typeck/expr_cs__enum.cpp @@ -810,7 +810,7 @@ namespace typecheck if( node.m_value ) { this->context.add_ivars(node.m_value->m_res_type); node.m_value->visit(*this); - this->context.equate_types(node.span(), loop_node.m_res_type, node.m_value->m_res_type); + this->context.equate_types_coerce(node.span(), loop_node.m_res_type, node.m_value); this->context.require_sized(node.span(), node.m_value->m_res_type); } else { diff --git a/src/mir/from_hir.cpp b/src/mir/from_hir.cpp index 140f7bdd9..282d6c492 100644 --- a/src/mir/from_hir.cpp +++ b/src/mir/from_hir.cpp @@ -805,6 +805,15 @@ namespace { ASSERT_BUG(node.span(), !node.m_continue, "Continue with a value isn't valid"); DEBUG("break value;"); this->visit_node_ptr(node.m_value); + //if( m_builder.resolve().type_is_impossible(node.span(), node.m_value->m_res_type) ) { + if( node.m_value->m_res_type.data().is_Diverge() ) { + //ASSERT_BUG(node.span(), !m_builder.has_result(), "Result present when value type is uninhabited - " << node.m_value->m_res_type); + //ASSERT_BUG(node.span(), !m_builder.block_active(), "Result present when value type is uninhabited - " << node.m_value->m_res_type); + } + } + if( !m_builder.block_active() ) { + // No block is currently active, not worth running the rest + return ; } // TODO: Use node.m_target_node @@ -815,12 +824,10 @@ namespace { m_builder.end_block( ::MIR::Terminator::make_Goto(target_block.cur) ); } else { - if( node.m_value ) - { + if( node.m_value ) { m_builder.push_stmt_assign( node.span(), target_block.res_value.clone(), m_builder.get_result(node.span()) ); } - else - { + else { // Set result to () m_builder.push_stmt_assign( node.span(), target_block.res_value.clone(), ::MIR::RValue::make_Tuple({{}}) ); }