@@ -597,6 +597,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
597
597
}
598
598
} ;
599
599
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
+
600
612
// The algorithm here is inspired by levenshtein distance and longest common subsequence.
601
613
// We'll try to detect 4 different types of mistakes:
602
614
// - 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> {
661
673
// A tuple wrap suggestion actually occurs within,
662
674
// so don't do anything special here.
663
675
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 ( ) ] ,
668
679
provided_arg_tys[ mismatch_idx. into ( ) ] . 0 ,
669
680
) ,
670
681
terr,
@@ -748,9 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
748
759
errors. drain_filter ( |error| {
749
760
let Error :: Invalid ( provided_idx, expected_idx, Compatibility :: Incompatible ( Some ( e) ) ) = error else { return false } ;
750
761
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) ;
754
763
if !matches ! ( trace. cause. as_failure_code( * e) , FailureCode :: Error0308 ( _) ) {
755
764
self . err_ctxt ( ) . report_and_explain_type_error ( trace, * e) . emit ( ) ;
756
765
return true ;
@@ -774,8 +783,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
774
783
{
775
784
let ( formal_ty, expected_ty) = formal_and_expected_inputs[ * expected_idx] ;
776
785
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) ;
779
787
let mut err = self . err_ctxt ( ) . report_and_explain_type_error ( trace, * err) ;
780
788
self . emit_coerce_suggestions (
781
789
& mut err,
@@ -847,8 +855,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
847
855
let ( formal_ty, expected_ty) = formal_and_expected_inputs[ expected_idx] ;
848
856
let ( provided_ty, provided_span) = provided_arg_tys[ provided_idx] ;
849
857
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) ;
852
859
if let Some ( e) = error {
853
860
self . err_ctxt ( ) . note_type_err (
854
861
& mut err,
0 commit comments