@@ -2733,7 +2733,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2733
2733
// or if-else.
2734
2734
fn check_then_else ( & self ,
2735
2735
cond_expr : & ' gcx hir:: Expr ,
2736
- then_blk : & ' gcx hir:: Block ,
2736
+ then_expr : & ' gcx hir:: Expr ,
2737
2737
opt_else_expr : Option < & ' gcx hir:: Expr > ,
2738
2738
sp : Span ,
2739
2739
expected : Expectation < ' tcx > ) -> Ty < ' tcx > {
@@ -2742,7 +2742,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2742
2742
self . diverges . set ( Diverges :: Maybe ) ;
2743
2743
2744
2744
let expected = expected. adjust_for_branches ( self ) ;
2745
- let then_ty = self . check_block_with_expected ( then_blk , expected) ;
2745
+ let then_ty = self . check_expr_with_expectation ( then_expr , expected) ;
2746
2746
let then_diverges = self . diverges . get ( ) ;
2747
2747
self . diverges . set ( Diverges :: Maybe ) ;
2748
2748
@@ -2757,26 +2757,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2757
2757
// to assign coercions to, otherwise it's () or diverging.
2758
2758
expected_ty = then_ty;
2759
2759
found_ty = else_ty;
2760
- result = if let Some ( ref then) = then_blk. expr {
2761
- let res = self . try_find_coercion_lub ( & cause, || Some ( & * * then) ,
2762
- then_ty, else_expr, else_ty) ;
2763
-
2764
- // In case we did perform an adjustment, we have to update
2765
- // the type of the block, because old trans still uses it.
2766
- if res. is_ok ( ) {
2767
- let adj = self . tables . borrow ( ) . adjustments . get ( & then. id ) . cloned ( ) ;
2768
- if let Some ( adj) = adj {
2769
- self . write_ty ( then_blk. id , adj. target ) ;
2770
- }
2771
- }
2772
2760
2773
- res
2774
- } else {
2775
- self . commit_if_ok ( |_| {
2776
- let trace = TypeTrace :: types ( & cause, true , then_ty, else_ty) ;
2777
- self . lub ( true , trace, & then_ty, & else_ty)
2778
- . map ( |ok| self . register_infer_ok_obligations ( ok) )
2779
- } )
2761
+ let coerce_to = expected. only_has_type ( self ) . unwrap_or ( then_ty) ;
2762
+ result = {
2763
+ self . try_coerce ( then_expr, then_ty, coerce_to)
2764
+ . and_then ( |t| {
2765
+ self . try_find_coercion_lub ( & cause, || Some ( then_expr) , t, else_expr, else_ty)
2766
+ } )
2780
2767
} ;
2781
2768
2782
2769
// We won't diverge unless both branches do (or the condition does).
@@ -3566,9 +3553,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3566
3553
tcx. mk_nil ( )
3567
3554
}
3568
3555
}
3569
- hir:: ExprIf ( ref cond, ref then_blk , ref opt_else_expr) => {
3570
- self . check_then_else ( & cond, & then_blk , opt_else_expr. as_ref ( ) . map ( |e| & * * e) ,
3571
- expr. span , expected)
3556
+ hir:: ExprIf ( ref cond, ref then_expr , ref opt_else_expr) => {
3557
+ self . check_then_else ( & cond, then_expr , opt_else_expr. as_ref ( ) . map ( |e| & * * e) ,
3558
+ expr. span , expected)
3572
3559
}
3573
3560
hir:: ExprWhile ( ref cond, ref body, _) => {
3574
3561
let unified = self . tcx . mk_nil ( ) ;
0 commit comments