11use rustc_errors:: { Applicability , Diag } ;
22use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
33use rustc_hir:: def_id:: LocalDefId ;
4- use rustc_hir:: { self as hir, ExprKind , PatKind } ;
4+ use rustc_hir:: { self as hir, ExprKind , HirId , PatKind } ;
55use rustc_hir_pretty:: ty_to_string;
66use rustc_middle:: ty:: { self , Ty } ;
77use rustc_span:: Span ;
@@ -412,89 +412,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
412412
413413 pub ( crate ) fn if_cause (
414414 & self ,
415- span : Span ,
416- cond_span : Span ,
415+ expr_id : HirId ,
417416 then_expr : & ' tcx hir:: Expr < ' tcx > ,
418417 else_expr : & ' tcx hir:: Expr < ' tcx > ,
419418 then_ty : Ty < ' tcx > ,
420419 else_ty : Ty < ' tcx > ,
421420 tail_defines_return_position_impl_trait : Option < LocalDefId > ,
422421 ) -> ObligationCause < ' tcx > {
423- let mut outer_span = if self . tcx . sess . source_map ( ) . is_multiline ( span) {
424- // The `if`/`else` isn't in one line in the output, include some context to make it
425- // clear it is an if/else expression:
426- // ```
427- // LL | let x = if true {
428- // | _____________-
429- // LL || 10i32
430- // || ----- expected because of this
431- // LL || } else {
432- // LL || 10u32
433- // || ^^^^^ expected `i32`, found `u32`
434- // LL || };
435- // ||_____- `if` and `else` have incompatible types
436- // ```
437- Some ( span)
438- } else {
439- // The entire expression is in one line, only point at the arms
440- // ```
441- // LL | let x = if true { 10i32 } else { 10u32 };
442- // | ----- ^^^^^ expected `i32`, found `u32`
443- // | |
444- // | expected because of this
445- // ```
446- None
447- } ;
448-
449422 let ( error_sp, else_id) = if let ExprKind :: Block ( block, _) = & else_expr. kind {
450423 let block = block. innermost_block ( ) ;
451-
452- // Avoid overlapping spans that aren't as readable:
453- // ```
454- // 2 | let x = if true {
455- // | _____________-
456- // 3 | | 3
457- // | | - expected because of this
458- // 4 | | } else {
459- // | |____________^
460- // 5 | ||
461- // 6 | || };
462- // | || ^
463- // | ||_____|
464- // | |______if and else have incompatible types
465- // | expected integer, found `()`
466- // ```
467- // by not pointing at the entire expression:
468- // ```
469- // 2 | let x = if true {
470- // | ------- `if` and `else` have incompatible types
471- // 3 | 3
472- // | - expected because of this
473- // 4 | } else {
474- // | ____________^
475- // 5 | |
476- // 6 | | };
477- // | |_____^ expected integer, found `()`
478- // ```
479- if block. expr . is_none ( )
480- && block. stmts . is_empty ( )
481- && let Some ( outer_span) = & mut outer_span
482- && let Some ( cond_span) = cond_span. find_ancestor_inside ( * outer_span)
483- {
484- * outer_span = outer_span. with_hi ( cond_span. hi ( ) )
485- }
486-
487424 ( self . find_block_span ( block) , block. hir_id )
488425 } else {
489426 ( else_expr. span , else_expr. hir_id )
490427 } ;
491428
492429 let then_id = if let ExprKind :: Block ( block, _) = & then_expr. kind {
493430 let block = block. innermost_block ( ) ;
494- // Exclude overlapping spans
495- if block. expr . is_none ( ) && block. stmts . is_empty ( ) {
496- outer_span = None ;
497- }
498431 block. hir_id
499432 } else {
500433 then_expr. hir_id
@@ -504,11 +437,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
504437 self . cause (
505438 error_sp,
506439 ObligationCauseCode :: IfExpression ( Box :: new ( IfExpressionCause {
440+ expr_id,
507441 else_id,
508442 then_id,
509443 then_ty,
510444 else_ty,
511- outer_span,
512445 tail_defines_return_position_impl_trait,
513446 } ) ) ,
514447 )
0 commit comments