@@ -106,7 +106,7 @@ pub trait TypeErrCtxtExt<'tcx> {
106
106
obligation : & PredicateObligation < ' tcx > ,
107
107
trait_ref : ty:: TraitRef < ' tcx > ,
108
108
err : & mut Diagnostic ,
109
- ) ;
109
+ ) -> bool ;
110
110
111
111
fn report_const_param_not_wf (
112
112
& self ,
@@ -507,8 +507,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
507
507
508
508
let mut err = struct_span_err ! ( self . tcx. sess, span, E0277 , "{}" , err_msg) ;
509
509
510
+ let mut suggested = false ;
510
511
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) ;
512
513
}
513
514
514
515
if is_try_conversion && let Some ( ret_span) = self . return_type_span ( & obligation) {
@@ -611,8 +612,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
611
612
612
613
self . suggest_floating_point_literal ( & obligation, & mut err, & trait_ref) ;
613
614
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) ;
616
616
suggested |= self . suggest_fn_call ( & obligation, & mut err, trait_predicate) ;
617
617
let impl_candidates = self . find_similar_impl_candidates ( trait_predicate) ;
618
618
suggested = if let & [ cand] = & impl_candidates[ ..] {
@@ -965,7 +965,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
965
965
obligation : & PredicateObligation < ' tcx > ,
966
966
trait_ref : ty:: TraitRef < ' tcx > ,
967
967
err : & mut Diagnostic ,
968
- ) {
968
+ ) -> bool {
969
969
let span = obligation. cause . span ;
970
970
struct V < ' v > {
971
971
search_span : Span ,
@@ -990,22 +990,22 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
990
990
Some ( hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Fn ( _, _, body_id) , .. } ) ) => {
991
991
body_id
992
992
}
993
- _ => return ,
993
+ _ => return false ,
994
994
} ;
995
995
let mut v = V { search_span : span, found : None } ;
996
996
v. visit_body ( self . tcx . hir ( ) . body ( * body_id) ) ;
997
997
let Some ( expr) = v. found else {
998
- return ;
998
+ return false ;
999
999
} ;
1000
1000
let Some ( typeck) = & self . typeck_results else {
1001
- return ;
1001
+ return false ;
1002
1002
} ;
1003
1003
let Some ( ( ObligationCauseCode :: QuestionMark , Some ( y) ) ) = obligation. cause . code ( ) . parent ( )
1004
1004
else {
1005
- return ;
1005
+ return false ;
1006
1006
} ;
1007
1007
if !self . tcx . is_diagnostic_item ( sym:: FromResidual , y. def_id ( ) ) {
1008
- return ;
1008
+ return false ;
1009
1009
}
1010
1010
let self_ty = trait_ref. self_ty ( ) ;
1011
1011
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> {
1030
1030
Some ( arg. as_type ( ) ?)
1031
1031
} ;
1032
1032
1033
+ let mut suggested = false ;
1033
1034
let mut chain = vec ! [ ] ;
1034
1035
1035
1036
// 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> {
1094
1095
)
1095
1096
. must_apply_modulo_regions ( )
1096
1097
{
1098
+ suggested = true ;
1097
1099
err. span_suggestion_short (
1098
1100
stmt. span . with_lo ( expr. span . hi ( ) ) ,
1099
1101
"remove this semicolon" ,
@@ -1150,17 +1152,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1150
1152
)
1151
1153
. must_apply_modulo_regions ( )
1152
1154
{
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
+ }
1154
1158
} else {
1155
1159
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
+ ) ;
1161
1165
}
1162
1166
prev = Some ( err_ty) ;
1163
1167
}
1168
+ suggested
1164
1169
}
1165
1170
1166
1171
fn report_const_param_not_wf (
0 commit comments