@@ -106,7 +106,7 @@ pub trait TypeErrCtxtExt<'tcx> {
106106 obligation : & PredicateObligation < ' tcx > ,
107107 trait_ref : ty:: TraitRef < ' tcx > ,
108108 err : & mut Diagnostic ,
109- ) ;
109+ ) -> bool ;
110110
111111 fn report_const_param_not_wf (
112112 & self ,
@@ -507,8 +507,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
507507
508508 let mut err = struct_span_err ! ( self . tcx. sess, span, E0277 , "{}" , err_msg) ;
509509
510+ let mut suggested = false ;
510511 if is_try_conversion {
511- self . try_conversion_context ( & obligation, trait_ref. skip_binder ( ) , & mut err) ;
512+ suggested = self . try_conversion_context ( & obligation, trait_ref. skip_binder ( ) , & mut err) ;
512513 }
513514
514515 if is_try_conversion && let Some ( ret_span) = self . return_type_span ( & obligation) {
@@ -611,8 +612,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
611612
612613 self . suggest_floating_point_literal ( & obligation, & mut err, & trait_ref) ;
613614 self . suggest_dereferencing_index ( & obligation, & mut err, trait_predicate) ;
614- let mut suggested =
615- self . suggest_dereferences ( & obligation, & mut err, trait_predicate) ;
615+ suggested |= self . suggest_dereferences ( & obligation, & mut err, trait_predicate) ;
616616 suggested |= self . suggest_fn_call ( & obligation, & mut err, trait_predicate) ;
617617 let impl_candidates = self . find_similar_impl_candidates ( trait_predicate) ;
618618 suggested = if let & [ cand] = & impl_candidates[ ..] {
@@ -965,7 +965,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
965965 obligation : & PredicateObligation < ' tcx > ,
966966 trait_ref : ty:: TraitRef < ' tcx > ,
967967 err : & mut Diagnostic ,
968- ) {
968+ ) -> bool {
969969 let span = obligation. cause . span ;
970970 struct V < ' v > {
971971 search_span : Span ,
@@ -990,22 +990,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
990990 Some ( hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Fn ( _, _, body_id) , .. } ) ) => {
991991 body_id
992992 }
993- _ => return ,
993+ _ => return false ,
994994 } ;
995995 let mut v = V { search_span : span, found : None } ;
996996 v. visit_body ( self . tcx . hir ( ) . body ( * body_id) ) ;
997997 let Some ( expr) = v. found else {
998- return ;
998+ return false ;
999999 } ;
10001000 let Some ( typeck) = & self . typeck_results else {
1001- return ;
1001+ return false ;
10021002 } ;
10031003 let Some ( ( ObligationCauseCode :: QuestionMark , Some ( y) ) ) = obligation. cause . code ( ) . parent ( )
10041004 else {
1005- return ;
1005+ return false ;
10061006 } ;
10071007 if !self . tcx . is_diagnostic_item ( sym:: FromResidual , y. def_id ( ) ) {
1008- return ;
1008+ return false ;
10091009 }
10101010 let self_ty = trait_ref. self_ty ( ) ;
10111011 let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
@@ -1030,6 +1030,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10301030 Some ( arg. as_type ( ) ?)
10311031 } ;
10321032
1033+ let mut suggested = false ;
10331034 let mut chain = vec ! [ ] ;
10341035
10351036 // The following logic is simlar to `point_at_chain`, but that's focused on associated types
@@ -1094,6 +1095,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10941095 )
10951096 . must_apply_modulo_regions ( )
10961097 {
1098+ suggested = true ;
10971099 err. span_suggestion_short (
10981100 stmt. span . with_lo ( expr. span . hi ( ) ) ,
10991101 "remove this semicolon" ,
@@ -1150,17 +1152,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11501152 )
11511153 . must_apply_modulo_regions ( )
11521154 {
1153- err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1155+ if !suggested {
1156+ err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1157+ }
11541158 } else {
11551159 err. span_label (
1156- span,
1157- format ! (
1158- "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1159- ) ,
1160- ) ;
1160+ span,
1161+ format ! (
1162+ "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1163+ ) ,
1164+ ) ;
11611165 }
11621166 prev = Some ( err_ty) ;
11631167 }
1168+ suggested
11641169 }
11651170
11661171 fn report_const_param_not_wf (
0 commit comments