@@ -3,10 +3,14 @@ use ast::{
33 tree:: { ASTTreeNode , ASTTreeNodeKind } ,
44} ;
55use astoir_hir:: {
6- ctx:: { HIRBranchedContext , HIRContext } ,
6+ ctx:: { EndingPointKind , HIRBranchedContext , HIRContext } ,
77 nodes:: { HIRNode , HIRNodeKind } ,
88} ;
9- use diagnostics:: { DiagnosticResult , DiagnosticSpanOrigin , move_current_diagnostic_pos} ;
9+ use diagnostics:: {
10+ DiagnosticResult , DiagnosticSpanOrigin ,
11+ builders:: { make_ret_type_kind, make_unreachable_code} ,
12+ move_current_diagnostic_pos,
13+ } ;
1014use prelude:: apply_prelude;
1115
1216use crate :: {
@@ -68,20 +72,24 @@ pub fn lower_ast_body_node(
6872 ASTTreeNodeKind :: ReturnStatement { val } => {
6973 let v;
7074
75+ if val. is_none ( ) != curr_ctx. return_type . is_none ( ) {
76+ return Err ( make_ret_type_kind ( & * node) . into ( ) ) ;
77+ }
78+
7179 if val. is_none ( ) {
80+ curr_ctx. introduce_ending_point ( EndingPointKind :: NoneReturn ) ;
81+
7282 v = None ;
7383 } else {
74- let mut k = lower_ast_value ( context, curr_ctx, val. unwrap ( ) ) ?;
75-
76- if curr_ctx. return_type . is_some ( ) {
77- k = Box :: new ( k. use_as (
78- context,
79- curr_ctx,
80- curr_ctx. return_type . clone ( ) . unwrap ( ) ,
81- & * node,
82- None ,
83- ) ?) ;
84- }
84+ curr_ctx. introduce_ending_point ( EndingPointKind :: Return ) ;
85+
86+ let k = Box :: new ( lower_ast_value ( context, curr_ctx, val. unwrap ( ) ) ?. use_as (
87+ context,
88+ curr_ctx,
89+ curr_ctx. return_type . clone ( ) . unwrap ( ) ,
90+ & * node,
91+ None ,
92+ ) ?) ;
8593
8694 v = Some ( k)
8795 }
@@ -129,7 +137,17 @@ pub fn lower_ast_body(
129137 }
130138
131139 for n in nodes {
132- hir_nodes. push ( lower_ast_body_node ( context, curr_ctx, n) ?) ;
140+ let is_code_dead_pre = !curr_ctx. is_code_alive ( curr_ctx. current_branch ) ;
141+
142+ let node = lower_ast_body_node ( context, curr_ctx, n) ?;
143+
144+ if !curr_ctx. is_code_alive ( curr_ctx. current_branch ) {
145+ if is_code_dead_pre || !node. is_ending_point ( ) {
146+ return Err ( make_unreachable_code ( & * node) . into ( ) ) ;
147+ }
148+ }
149+
150+ hir_nodes. push ( node) ;
133151 }
134152
135153 if introduce_era {
0 commit comments