@@ -392,32 +392,49 @@ impl LoweringContext<'_> {
392392 )
393393 }
394394
395+ /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
396+ /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
397+ /// and save the block id to use it as a break target for desugaring of the `?` operator.
395398 fn lower_expr_try_block ( & mut self , body : & Block ) -> hir:: ExprKind {
396399 self . with_catch_scope ( body. id , |this| {
397- let unstable_span = this. mark_span_with_reason (
400+ let mut block = this. lower_block ( body, true ) . into_inner ( ) ;
401+
402+ let try_span = this. mark_span_with_reason (
398403 DesugaringKind :: TryBlock ,
399404 body. span ,
400405 this. allow_try_trait . clone ( ) ,
401406 ) ;
402- let mut block = this. lower_block ( body, true ) . into_inner ( ) ;
403- let tail = block. expr . take ( ) . map_or_else (
404- || this. expr_unit ( this. sess . source_map ( ) . end_point ( unstable_span) ) ,
407+
408+ // Final expression of the block (if present) or `()` with span at the end of block
409+ let tail_expr = block. expr . take ( ) . map_or_else (
410+ || this. expr_unit ( this. sess . source_map ( ) . end_point ( try_span) ) ,
405411 |x : P < hir:: Expr > | x. into_inner ( ) ,
406412 ) ;
407- block. expr = Some ( this. wrap_in_try_constructor ( sym:: from_ok, tail, unstable_span) ) ;
413+
414+ let ok_wrapped_span = this. mark_span_with_reason (
415+ DesugaringKind :: TryBlock ,
416+ tail_expr. span ,
417+ None
418+ ) ;
419+
420+ // `::std::ops::Try::from_ok($tail_expr)`
421+ block. expr = Some ( this. wrap_in_try_constructor (
422+ sym:: from_ok, try_span, tail_expr, ok_wrapped_span) ) ;
423+
408424 hir:: ExprKind :: Block ( P ( block) , None )
409425 } )
410426 }
411427
412428 fn wrap_in_try_constructor (
413429 & mut self ,
414430 method : Symbol ,
415- e : hir:: Expr ,
416- unstable_span : Span ,
431+ method_span : Span ,
432+ expr : hir:: Expr ,
433+ overall_span : Span ,
417434 ) -> P < hir:: Expr > {
418435 let path = & [ sym:: ops, sym:: Try , method] ;
419- let from_err = P ( self . expr_std_path ( unstable_span , path, None , ThinVec :: new ( ) ) ) ;
420- P ( self . expr_call ( e . span , from_err , hir_vec ! [ e ] ) )
436+ let constructor = P ( self . expr_std_path ( method_span , path, None , ThinVec :: new ( ) ) ) ;
437+ P ( self . expr_call ( overall_span , constructor , hir_vec ! [ expr ] ) )
421438 }
422439
423440 fn lower_arm ( & mut self , arm : & Arm ) -> hir:: Arm {
@@ -1244,7 +1261,7 @@ impl LoweringContext<'_> {
12441261 self . expr_call_std_path ( try_span, from_path, hir_vec ! [ err_expr] )
12451262 } ;
12461263 let from_err_expr =
1247- self . wrap_in_try_constructor ( sym:: from_error, from_expr, unstable_span ) ;
1264+ self . wrap_in_try_constructor ( sym:: from_error, unstable_span , from_expr, try_span ) ;
12481265 let thin_attrs = ThinVec :: from ( attrs) ;
12491266 let catch_scope = self . catch_scopes . last ( ) . map ( |x| * x) ;
12501267 let ret_expr = if let Some ( catch_node) = catch_scope {
0 commit comments