@@ -37,7 +37,7 @@ use crate::{
3737 item_scope:: BuiltinShadowMode ,
3838 path:: { GenericArgs , Path } ,
3939 type_ref:: { Mutability , Rawness , TypeRef } ,
40- AdtId , BlockLoc , ModuleDefId , UnresolvedMacro ,
40+ AdtId , BlockId , BlockLoc , ModuleDefId , UnresolvedMacro ,
4141} ;
4242
4343pub struct LowerCtx < ' a > {
@@ -238,33 +238,32 @@ impl ExprCollector<'_> {
238238 }
239239 ast:: Expr :: BlockExpr ( e) => match e. modifier ( ) {
240240 Some ( ast:: BlockModifier :: Try ( _) ) => {
241- let body = self . collect_block ( e) ;
242- self . alloc_expr ( Expr :: TryBlock { body } , syntax_ptr)
241+ self . collect_block_ ( e, |id, statements, tail| Expr :: TryBlock {
242+ id,
243+ statements,
244+ tail,
245+ } )
243246 }
244247 Some ( ast:: BlockModifier :: Unsafe ( _) ) => {
245- let body = self . collect_block ( e) ;
246- self . alloc_expr ( Expr :: Unsafe { body } , syntax_ptr)
248+ self . collect_block_ ( e, |id, statements, tail| Expr :: Unsafe {
249+ id,
250+ statements,
251+ tail,
252+ } )
247253 }
248- // FIXME: we need to record these effects somewhere...
249254 Some ( ast:: BlockModifier :: Label ( label) ) => {
250255 let label = self . collect_label ( label) ;
251- let res = self . collect_block ( e) ;
252- match & mut self . body . exprs [ res] {
253- Expr :: Block { label : block_label, .. } => {
254- * block_label = Some ( label) ;
255- }
256- _ => unreachable ! ( ) ,
257- }
258- res
259- }
260- Some ( ast:: BlockModifier :: Async ( _) ) => {
261- let body = self . collect_block ( e) ;
262- self . alloc_expr ( Expr :: Async { body } , syntax_ptr)
263- }
264- Some ( ast:: BlockModifier :: Const ( _) ) => {
265- let body = self . collect_block ( e) ;
266- self . alloc_expr ( Expr :: Const { body } , syntax_ptr)
256+ self . collect_block_ ( e, |id, statements, tail| Expr :: Block {
257+ id,
258+ statements,
259+ tail,
260+ label : Some ( label) ,
261+ } )
267262 }
263+ Some ( ast:: BlockModifier :: Async ( _) ) => self
264+ . collect_block_ ( e, |id, statements, tail| Expr :: Async { id, statements, tail } ) ,
265+ Some ( ast:: BlockModifier :: Const ( _) ) => self
266+ . collect_block_ ( e, |id, statements, tail| Expr :: Const { id, statements, tail } ) ,
268267 None => self . collect_block ( e) ,
269268 } ,
270269 ast:: Expr :: LoopExpr ( e) => {
@@ -737,6 +736,19 @@ impl ExprCollector<'_> {
737736 }
738737
739738 fn collect_block ( & mut self , block : ast:: BlockExpr ) -> ExprId {
739+ self . collect_block_ ( block, |id, statements, tail| Expr :: Block {
740+ id,
741+ statements,
742+ tail,
743+ label : None ,
744+ } )
745+ }
746+
747+ fn collect_block_ (
748+ & mut self ,
749+ block : ast:: BlockExpr ,
750+ mk_block : impl FnOnce ( BlockId , Box < [ Statement ] > , Option < ExprId > ) -> Expr ,
751+ ) -> ExprId {
740752 let file_local_id = self . ast_id_map . ast_id ( & block) ;
741753 let ast_id = AstId :: new ( self . expander . current_file_id , file_local_id) ;
742754 let block_loc =
@@ -769,15 +781,8 @@ impl ExprCollector<'_> {
769781 } ) ;
770782
771783 let syntax_node_ptr = AstPtr :: new ( & block. into ( ) ) ;
772- let expr_id = self . alloc_expr (
773- Expr :: Block {
774- id : block_id,
775- statements : statements. into_boxed_slice ( ) ,
776- tail,
777- label : None ,
778- } ,
779- syntax_node_ptr,
780- ) ;
784+ let expr_id = self
785+ . alloc_expr ( mk_block ( block_id, statements. into_boxed_slice ( ) , tail) , syntax_node_ptr) ;
781786
782787 self . expander . def_map = prev_def_map;
783788 self . expander . module = prev_local_module;
0 commit comments