@@ -112,7 +112,7 @@ pub trait TypeErrCtxtExt<'tcx> {
112112 obligation : & PredicateObligation < ' tcx > ,
113113 trait_ref : ty:: TraitRef < ' tcx > ,
114114 err : & mut Diagnostic ,
115- ) ;
115+ ) -> bool ;
116116
117117 fn report_const_param_not_wf (
118118 & self ,
@@ -517,8 +517,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
517517
518518 let mut err = struct_span_err ! ( self . tcx. sess, span, E0277 , "{}" , err_msg) ;
519519
520+ let mut suggested = false ;
520521 if is_try_conversion {
521- self . try_conversion_context ( & obligation, trait_ref. skip_binder ( ) , & mut err) ;
522+ suggested = self . try_conversion_context ( & obligation, trait_ref. skip_binder ( ) , & mut err) ;
522523 }
523524
524525 if is_try_conversion && let Some ( ret_span) = self . return_type_span ( & obligation) {
@@ -621,8 +622,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
621622
622623 self . suggest_floating_point_literal ( & obligation, & mut err, & trait_ref) ;
623624 self . suggest_dereferencing_index ( & obligation, & mut err, trait_predicate) ;
624- let mut suggested =
625- self . suggest_dereferences ( & obligation, & mut err, trait_predicate) ;
625+ suggested |= self . suggest_dereferences ( & obligation, & mut err, trait_predicate) ;
626626 suggested |= self . suggest_fn_call ( & obligation, & mut err, trait_predicate) ;
627627 let impl_candidates = self . find_similar_impl_candidates ( trait_predicate) ;
628628 suggested = if let & [ cand] = & impl_candidates[ ..] {
@@ -1002,7 +1002,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10021002 obligation : & PredicateObligation < ' tcx > ,
10031003 trait_ref : ty:: TraitRef < ' tcx > ,
10041004 err : & mut Diagnostic ,
1005- ) {
1005+ ) -> bool {
10061006 let span = obligation. cause . span ;
10071007 struct V < ' v > {
10081008 search_span : Span ,
@@ -1027,22 +1027,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10271027 Some ( hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Fn ( _, _, body_id) , .. } ) ) => {
10281028 body_id
10291029 }
1030- _ => return ,
1030+ _ => return false ,
10311031 } ;
10321032 let mut v = V { search_span : span, found : None } ;
10331033 v. visit_body ( self . tcx . hir ( ) . body ( * body_id) ) ;
10341034 let Some ( expr) = v. found else {
1035- return ;
1035+ return false ;
10361036 } ;
10371037 let Some ( typeck) = & self . typeck_results else {
1038- return ;
1038+ return false ;
10391039 } ;
10401040 let Some ( ( ObligationCauseCode :: QuestionMark , Some ( y) ) ) = obligation. cause . code ( ) . parent ( )
10411041 else {
1042- return ;
1042+ return false ;
10431043 } ;
10441044 if !self . tcx . is_diagnostic_item ( sym:: FromResidual , y. def_id ( ) ) {
1045- return ;
1045+ return false ;
10461046 }
10471047 let self_ty = trait_ref. self_ty ( ) ;
10481048 let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
@@ -1067,6 +1067,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10671067 Some ( arg. as_type ( ) ?)
10681068 } ;
10691069
1070+ let mut suggested = false ;
10701071 let mut chain = vec ! [ ] ;
10711072
10721073 // The following logic is simlar to `point_at_chain`, but that's focused on associated types
@@ -1135,6 +1136,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11351136 )
11361137 . must_apply_modulo_regions ( )
11371138 {
1139+ suggested = true ;
11381140 err. span_suggestion_short (
11391141 stmt. span . with_lo ( expr. span . hi ( ) ) ,
11401142 "remove this semicolon" ,
@@ -1193,17 +1195,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11931195 )
11941196 . must_apply_modulo_regions ( )
11951197 {
1196- err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1198+ if !suggested {
1199+ err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1200+ }
11971201 } else {
11981202 err. span_label (
1199- span,
1200- format ! (
1201- "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1202- ) ,
1203- ) ;
1203+ span,
1204+ format ! (
1205+ "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1206+ ) ,
1207+ ) ;
12041208 }
12051209 prev = Some ( err_ty) ;
12061210 }
1211+ suggested
12071212 }
12081213
12091214 fn report_const_param_not_wf (
0 commit comments