@@ -754,9 +754,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
754
754
ty:: Predicate :: ObjectSafe ( trait_def_id) => {
755
755
let violations = self . tcx . global_tcx ( )
756
756
. 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
+ }
760
766
}
761
767
762
768
ty:: Predicate :: ClosureKind ( closure_def_id, closure_substs, kind) => {
@@ -884,7 +890,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
884
890
885
891
TraitNotObjectSafe ( did) => {
886
892
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
+ }
888
898
}
889
899
890
900
// already reported in the query
@@ -1293,12 +1303,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1293
1303
err
1294
1304
}
1295
1305
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
+ }
1302
1316
let trait_str = self . def_path_str ( trait_def_id) ;
1303
1317
let span = self . sess . source_map ( ) . def_span ( span) ;
1304
1318
let mut err = struct_span_err ! (
@@ -1313,7 +1327,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1313
1327
err. note ( & violation. error_msg ( ) ) ;
1314
1328
}
1315
1329
}
1316
- err
1330
+ Some ( err)
1317
1331
}
1318
1332
}
1319
1333
0 commit comments