@@ -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[ ..] {
@@ -967,7 +967,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
967967 obligation : & PredicateObligation < ' tcx > ,
968968 trait_ref : ty:: TraitRef < ' tcx > ,
969969 err : & mut Diagnostic ,
970- ) {
970+ ) -> bool {
971971 let span = obligation. cause . span ;
972972 struct V < ' v > {
973973 search_span : Span ,
@@ -992,22 +992,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
992992 Some ( hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Fn ( _, _, body_id) , .. } ) ) => {
993993 body_id
994994 }
995- _ => return ,
995+ _ => return false ,
996996 } ;
997997 let mut v = V { search_span : span, found : None } ;
998998 v. visit_body ( self . tcx . hir ( ) . body ( * body_id) ) ;
999999 let Some ( expr) = v. found else {
1000- return ;
1000+ return false ;
10011001 } ;
10021002 let Some ( typeck) = & self . typeck_results else {
1003- return ;
1003+ return false ;
10041004 } ;
10051005 let Some ( ( ObligationCauseCode :: QuestionMark , Some ( y) ) ) = obligation. cause . code ( ) . parent ( )
10061006 else {
1007- return ;
1007+ return false ;
10081008 } ;
10091009 if !self . tcx . is_diagnostic_item ( sym:: FromResidual , y. def_id ( ) ) {
1010- return ;
1010+ return false ;
10111011 }
10121012 let self_ty = trait_ref. self_ty ( ) ;
10131013 let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
@@ -1032,6 +1032,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10321032 Some ( arg. as_type ( ) ?)
10331033 } ;
10341034
1035+ let mut suggested = false ;
10351036 let mut chain = vec ! [ ] ;
10361037
10371038 // The following logic is simlar to `point_at_chain`, but that's focused on associated types
@@ -1096,6 +1097,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10961097 )
10971098 . must_apply_modulo_regions ( )
10981099 {
1100+ suggested = true ;
10991101 err. span_suggestion_short (
11001102 stmt. span . with_lo ( expr. span . hi ( ) ) ,
11011103 "remove this semicolon" ,
@@ -1152,17 +1154,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11521154 )
11531155 . must_apply_modulo_regions ( )
11541156 {
1155- err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1157+ if !suggested {
1158+ err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1159+ }
11561160 } else {
11571161 err. span_label (
1158- span,
1159- format ! (
1160- "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1161- ) ,
1162- ) ;
1162+ span,
1163+ format ! (
1164+ "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1165+ ) ,
1166+ ) ;
11631167 }
11641168 prev = Some ( err_ty) ;
11651169 }
1170+ suggested
11661171 }
11671172
11681173 fn report_const_param_not_wf (
0 commit comments