@@ -754,9 +754,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
754754 ty:: Predicate :: ObjectSafe ( trait_def_id) => {
755755 let violations = self . tcx . global_tcx ( )
756756 . object_safety_violations ( trait_def_id) ;
757- self . tcx . report_object_safety_error ( span,
758- trait_def_id,
759- violations)
757+ if let Some ( err) = self . tcx . report_object_safety_error (
758+ span,
759+ trait_def_id,
760+ violations,
761+ ) {
762+ err
763+ } else {
764+ return ;
765+ }
760766 }
761767
762768 ty:: Predicate :: ClosureKind ( closure_def_id, closure_substs, kind) => {
@@ -884,7 +890,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
884890
885891 TraitNotObjectSafe ( did) => {
886892 let violations = self . tcx . global_tcx ( ) . object_safety_violations ( did) ;
887- self . tcx . report_object_safety_error ( span, did, violations)
893+ if let Some ( err) = self . tcx . report_object_safety_error ( span, did, violations) {
894+ err
895+ } else {
896+ return ;
897+ }
888898 }
889899
890900 // already reported in the query
@@ -1293,12 +1303,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12931303 err
12941304 }
12951305
1296- pub fn report_object_safety_error ( self ,
1297- span : Span ,
1298- trait_def_id : DefId ,
1299- violations : Vec < ObjectSafetyViolation > )
1300- -> DiagnosticBuilder < ' tcx >
1301- {
1306+ pub fn report_object_safety_error (
1307+ self ,
1308+ span : Span ,
1309+ trait_def_id : DefId ,
1310+ violations : Vec < ObjectSafetyViolation > ,
1311+ ) -> Option < DiagnosticBuilder < ' tcx > > {
1312+ if self . sess . trait_methods_not_found . borrow ( ) . contains ( & span) {
1313+ // Avoid emitting error caused by non-existing method (#58734)
1314+ return None ;
1315+ }
13021316 let trait_str = self . def_path_str ( trait_def_id) ;
13031317 let span = self . sess . source_map ( ) . def_span ( span) ;
13041318 let mut err = struct_span_err ! (
@@ -1313,7 +1327,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13131327 err. note ( & violation. error_msg ( ) ) ;
13141328 }
13151329 }
1316- err
1330+ Some ( err)
13171331 }
13181332}
13191333
0 commit comments