@@ -1229,7 +1229,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
12291229 chain. push ( ( expr. span , prev_ty) ) ;
12301230
12311231 let mut prev = None ;
1232- for ( span, err_ty) in chain. into_iter ( ) . rev ( ) {
1232+ let mut iter = chain. into_iter ( ) . rev ( ) . peekable ( ) ;
1233+ while let Some ( ( span, err_ty) ) = iter. next ( ) {
1234+ let is_last = iter. peek ( ) . is_none ( ) ;
12331235 let err_ty = get_e_type ( err_ty) ;
12341236 let err_ty = match ( err_ty, prev) {
12351237 ( Some ( err_ty) , Some ( prev) ) if !self . can_eq ( obligation. param_env , err_ty, prev) => {
@@ -1241,27 +1243,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
12411243 continue ;
12421244 }
12431245 } ;
1244- if self
1246+
1247+ let implements_from = self
12451248 . infcx
12461249 . type_implements_trait (
12471250 self . tcx . get_diagnostic_item ( sym:: From ) . unwrap ( ) ,
12481251 [ self_ty, err_ty] ,
12491252 obligation. param_env ,
12501253 )
1251- . must_apply_modulo_regions ( )
1252- {
1253- if !suggested {
1254- let err_ty = self . tcx . short_string ( err_ty, err. long_ty_path ( ) ) ;
1255- err. span_label ( span, format ! ( "this has type `Result<_, {err_ty}>`" ) ) ;
1256- }
1254+ . must_apply_modulo_regions ( ) ;
1255+
1256+ let err_ty_str = self . tcx . short_string ( err_ty, err. long_ty_path ( ) ) ;
1257+ let label = if !implements_from && is_last {
1258+ format ! (
1259+ "this can't be annotated with `?` because it has type `Result<_, {err_ty_str}>`"
1260+ )
12571261 } else {
1258- let err_ty = self . tcx . short_string ( err_ty, err. long_ty_path ( ) ) ;
1259- err. span_label (
1260- span,
1261- format ! (
1262- "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`" ,
1263- ) ,
1264- ) ;
1262+ format ! ( "this has type `Result<_, {err_ty_str}>`" )
1263+ } ;
1264+
1265+ if !suggested || !implements_from {
1266+ err. span_label ( span, label) ;
12651267 }
12661268 prev = Some ( err_ty) ;
12671269 }
0 commit comments