@@ -392,32 +392,49 @@ impl LoweringContext<'_> {
392
392
)
393
393
}
394
394
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.
395
398
fn lower_expr_try_block ( & mut self , body : & Block ) -> hir:: ExprKind {
396
399
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 (
398
403
DesugaringKind :: TryBlock ,
399
404
body. span ,
400
405
this. allow_try_trait . clone ( ) ,
401
406
) ;
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) ) ,
405
411
|x : P < hir:: Expr > | x. into_inner ( ) ,
406
412
) ;
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
+
408
424
hir:: ExprKind :: Block ( P ( block) , None )
409
425
} )
410
426
}
411
427
412
428
fn wrap_in_try_constructor (
413
429
& mut self ,
414
430
method : Symbol ,
415
- e : hir:: Expr ,
416
- unstable_span : Span ,
431
+ method_span : Span ,
432
+ expr : hir:: Expr ,
433
+ overall_span : Span ,
417
434
) -> P < hir:: Expr > {
418
435
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 ] ) )
421
438
}
422
439
423
440
fn lower_arm ( & mut self , arm : & Arm ) -> hir:: Arm {
@@ -1244,7 +1261,7 @@ impl LoweringContext<'_> {
1244
1261
self . expr_call_std_path ( try_span, from_path, hir_vec ! [ err_expr] )
1245
1262
} ;
1246
1263
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 ) ;
1248
1265
let thin_attrs = ThinVec :: from ( attrs) ;
1249
1266
let catch_scope = self . catch_scopes . last ( ) . map ( |x| * x) ;
1250
1267
let ret_expr = if let Some ( catch_node) = catch_scope {
0 commit comments