@@ -824,9 +824,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
824824 fn cmp_fn_sig (
825825 & self ,
826826 sig1 : & ty:: PolyFnSig < ' tcx > ,
827- fn_def1 : Option < ( DefId , & ' tcx [ ty:: GenericArg < ' tcx > ] ) > ,
827+ fn_def1 : Option < ( DefId , Option < & ' tcx [ ty:: GenericArg < ' tcx > ] > ) > ,
828828 sig2 : & ty:: PolyFnSig < ' tcx > ,
829- fn_def2 : Option < ( DefId , & ' tcx [ ty:: GenericArg < ' tcx > ] ) > ,
829+ fn_def2 : Option < ( DefId , Option < & ' tcx [ ty:: GenericArg < ' tcx > ] > ) > ,
830830 ) -> ( DiagStyledString , DiagStyledString ) {
831831 let sig1 = & ( self . normalize_fn_sig ) ( * sig1) ;
832832 let sig2 = & ( self . normalize_fn_sig ) ( * sig2) ;
@@ -850,8 +850,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
850850
851851 // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
852852 // ^^^^^^
853- values. 0 . push ( sig1. safety . prefix_str ( ) , sig1. safety != sig2. safety ) ;
854- values. 1 . push ( sig2. safety . prefix_str ( ) , sig1. safety != sig2. safety ) ;
853+ let safety = |fn_def, sig : ty:: FnSig < ' _ > | match fn_def {
854+ None => sig. safety . prefix_str ( ) ,
855+ Some ( ( did, _) ) => {
856+ if self . tcx . codegen_fn_attrs ( did) . safe_target_features {
857+ "#[target_features] "
858+ } else {
859+ sig. safety . prefix_str ( )
860+ }
861+ }
862+ } ;
863+ let safety1 = safety ( fn_def1, sig1) ;
864+ let safety2 = safety ( fn_def2, sig2) ;
865+ values. 0 . push ( safety1, safety1 != safety2) ;
866+ values. 1 . push ( safety2, safety1 != safety2) ;
855867
856868 // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
857869 // ^^^^^^^^^^
@@ -932,23 +944,23 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
932944 ( values. 1 ) . 0 . extend ( x2. 0 ) ;
933945 }
934946
935- let fmt = |( did, args) | format ! ( " {{{}}}" , self . tcx. def_path_str_with_args( did, args) ) ;
947+ let fmt = |did, args| format ! ( " {{{}}}" , self . tcx. def_path_str_with_args( did, args) ) ;
936948
937949 match ( fn_def1, fn_def2) {
938- ( None , None ) => { }
939- ( Some ( fn_def1) , Some ( fn_def2) ) => {
940- let path1 = fmt ( fn_def1) ;
941- let path2 = fmt ( fn_def2) ;
950+ ( Some ( ( fn_def1, Some ( fn_args1) ) ) , Some ( ( fn_def2, Some ( fn_args2) ) ) ) => {
951+ let path1 = fmt ( fn_def1, fn_args1) ;
952+ let path2 = fmt ( fn_def2, fn_args2) ;
942953 let same_path = path1 == path2;
943954 values. 0 . push ( path1, !same_path) ;
944955 values. 1 . push ( path2, !same_path) ;
945956 }
946- ( Some ( fn_def1) , None ) => {
947- values. 0 . push_highlighted ( fmt ( fn_def1) ) ;
957+ ( Some ( ( fn_def1, Some ( fn_args1 ) ) ) , None ) => {
958+ values. 0 . push_highlighted ( fmt ( fn_def1, fn_args1 ) ) ;
948959 }
949- ( None , Some ( fn_def2) ) => {
950- values. 1 . push_highlighted ( fmt ( fn_def2) ) ;
960+ ( None , Some ( ( fn_def2, Some ( fn_args2 ) ) ) ) => {
961+ values. 1 . push_highlighted ( fmt ( fn_def2, fn_args2 ) ) ;
951962 }
963+ _ => { }
952964 }
953965
954966 values
@@ -1339,17 +1351,22 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
13391351 ( ty:: FnDef ( did1, args1) , ty:: FnDef ( did2, args2) ) => {
13401352 let sig1 = self . tcx . fn_sig ( * did1) . instantiate ( self . tcx , args1) ;
13411353 let sig2 = self . tcx . fn_sig ( * did2) . instantiate ( self . tcx , args2) ;
1342- self . cmp_fn_sig ( & sig1, Some ( ( * did1, args1) ) , & sig2, Some ( ( * did2, args2) ) )
1354+ self . cmp_fn_sig (
1355+ & sig1,
1356+ Some ( ( * did1, Some ( args1) ) ) ,
1357+ & sig2,
1358+ Some ( ( * did2, Some ( args2) ) ) ,
1359+ )
13431360 }
13441361
13451362 ( ty:: FnDef ( did1, args1) , ty:: FnPtr ( sig_tys2, hdr2) ) => {
13461363 let sig1 = self . tcx . fn_sig ( * did1) . instantiate ( self . tcx , args1) ;
1347- self . cmp_fn_sig ( & sig1, Some ( ( * did1, args1) ) , & sig_tys2. with ( * hdr2) , None )
1364+ self . cmp_fn_sig ( & sig1, Some ( ( * did1, Some ( args1) ) ) , & sig_tys2. with ( * hdr2) , None )
13481365 }
13491366
13501367 ( ty:: FnPtr ( sig_tys1, hdr1) , ty:: FnDef ( did2, args2) ) => {
13511368 let sig2 = self . tcx . fn_sig ( * did2) . instantiate ( self . tcx , args2) ;
1352- self . cmp_fn_sig ( & sig_tys1. with ( * hdr1) , None , & sig2, Some ( ( * did2, args2) ) )
1369+ self . cmp_fn_sig ( & sig_tys1. with ( * hdr1) , None , & sig2, Some ( ( * did2, Some ( args2) ) ) )
13531370 }
13541371
13551372 ( ty:: FnPtr ( sig_tys1, hdr1) , ty:: FnPtr ( sig_tys2, hdr2) ) => {
@@ -1531,7 +1548,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15311548 ( false , Mismatch :: Fixed ( "existential projection" ) )
15321549 }
15331550 } ;
1534- let Some ( vals) = self . values_str ( values) else {
1551+ let Some ( vals) = self . values_str ( values, cause ) else {
15351552 // Derived error. Cancel the emitter.
15361553 // NOTE(eddyb) this was `.cancel()`, but `diag`
15371554 // is borrowed, so we can't fully defuse it.
@@ -1956,7 +1973,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19561973 } )
19571974 | ObligationCauseCode :: BlockTailExpression ( .., source) ) = code
19581975 && let hir:: MatchSource :: TryDesugar ( _) = source
1959- && let Some ( ( expected_ty, found_ty, _) ) = self . values_str ( trace. values )
1976+ && let Some ( ( expected_ty, found_ty, _) ) = self . values_str ( trace. values , & trace . cause )
19601977 {
19611978 suggestions. push ( TypeErrorAdditionalDiags :: TryCannotConvert {
19621979 found : found_ty. content ( ) ,
@@ -2085,6 +2102,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
20852102 fn values_str (
20862103 & self ,
20872104 values : ValuePairs < ' tcx > ,
2105+ cause : & ObligationCause < ' tcx > ,
20882106 ) -> Option < ( DiagStyledString , DiagStyledString , Option < PathBuf > ) > {
20892107 match values {
20902108 ValuePairs :: Regions ( exp_found) => self . expected_found_str ( exp_found) ,
@@ -2109,7 +2127,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21092127 if exp_found. references_error ( ) {
21102128 return None ;
21112129 }
2112- let ( exp, fnd) = self . cmp_fn_sig ( & exp_found. expected , None , & exp_found. found , None ) ;
2130+ let ( fn_def1, fn_def2) = if let ObligationCauseCode :: CompareImplItem {
2131+ impl_item_def_id,
2132+ trait_item_def_id,
2133+ ..
2134+ } = * cause. code ( )
2135+ {
2136+ ( Some ( ( trait_item_def_id, None ) ) , Some ( ( impl_item_def_id. to_def_id ( ) , None ) ) )
2137+ } else {
2138+ ( None , None )
2139+ } ;
2140+
2141+ let ( exp, fnd) =
2142+ self . cmp_fn_sig ( & exp_found. expected , fn_def1, & exp_found. found , fn_def2) ;
21132143 Some ( ( exp, fnd, None ) )
21142144 }
21152145 }
0 commit comments