@@ -597,6 +597,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
597597 }
598598 } ;
599599
600+ let mk_trace = |span, ( formal_ty, expected_ty) , provided_ty| {
601+ let mismatched_ty = if expected_ty == provided_ty {
602+ // If expected == provided, then we must have failed to sup
603+ // the formal type. Avoid printing out "expected Ty, found Ty"
604+ // in that case.
605+ formal_ty
606+ } else {
607+ expected_ty
608+ } ;
609+ TypeTrace :: types ( & self . misc ( span) , true , mismatched_ty, provided_ty)
610+ } ;
611+
600612 // The algorithm here is inspired by levenshtein distance and longest common subsequence.
601613 // We'll try to detect 4 different types of mistakes:
602614 // - An extra parameter has been provided that doesn't satisfy *any* of the other inputs
@@ -661,10 +673,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
661673 // A tuple wrap suggestion actually occurs within,
662674 // so don't do anything special here.
663675 err = self . err_ctxt ( ) . report_and_explain_type_error (
664- TypeTrace :: types (
665- & self . misc ( * lo) ,
666- true ,
667- formal_and_expected_inputs[ mismatch_idx. into ( ) ] . 1 ,
676+ mk_trace (
677+ * lo,
678+ formal_and_expected_inputs[ mismatch_idx. into ( ) ] ,
668679 provided_arg_tys[ mismatch_idx. into ( ) ] . 0 ,
669680 ) ,
670681 terr,
@@ -748,9 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
748759 errors. drain_filter ( |error| {
749760 let Error :: Invalid ( provided_idx, expected_idx, Compatibility :: Incompatible ( Some ( e) ) ) = error else { return false } ;
750761 let ( provided_ty, provided_span) = provided_arg_tys[ * provided_idx] ;
751- let ( expected_ty, _) = formal_and_expected_inputs[ * expected_idx] ;
752- let cause = & self . misc ( provided_span) ;
753- let trace = TypeTrace :: types ( cause, true , expected_ty, provided_ty) ;
762+ let trace = mk_trace ( provided_span, formal_and_expected_inputs[ * expected_idx] , provided_ty) ;
754763 if !matches ! ( trace. cause. as_failure_code( * e) , FailureCode :: Error0308 ( _) ) {
755764 self . err_ctxt ( ) . report_and_explain_type_error ( trace, * e) . emit ( ) ;
756765 return true ;
@@ -774,8 +783,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
774783 {
775784 let ( formal_ty, expected_ty) = formal_and_expected_inputs[ * expected_idx] ;
776785 let ( provided_ty, provided_arg_span) = provided_arg_tys[ * provided_idx] ;
777- let cause = & self . misc ( provided_arg_span) ;
778- let trace = TypeTrace :: types ( cause, true , expected_ty, provided_ty) ;
786+ let trace = mk_trace ( provided_arg_span, ( formal_ty, expected_ty) , provided_ty) ;
779787 let mut err = self . err_ctxt ( ) . report_and_explain_type_error ( trace, * err) ;
780788 self . emit_coerce_suggestions (
781789 & mut err,
@@ -847,8 +855,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
847855 let ( formal_ty, expected_ty) = formal_and_expected_inputs[ expected_idx] ;
848856 let ( provided_ty, provided_span) = provided_arg_tys[ provided_idx] ;
849857 if let Compatibility :: Incompatible ( error) = compatibility {
850- let cause = & self . misc ( provided_span) ;
851- let trace = TypeTrace :: types ( cause, true , expected_ty, provided_ty) ;
858+ let trace = mk_trace ( provided_span, ( formal_ty, expected_ty) , provided_ty) ;
852859 if let Some ( e) = error {
853860 self . err_ctxt ( ) . note_type_err (
854861 & mut err,
0 commit comments