@@ -252,16 +252,16 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
252
252
} ) ) ;
253
253
}
254
254
255
- let mut res = Ok ( ( ) ) ;
256
255
if def_a. repr ( ) . c ( ) || def_a. repr ( ) . packed ( ) {
257
- res = Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynRepr { span } ) ) ;
256
+ return Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynRepr { span } ) ) ;
258
257
}
259
258
260
259
let fields = & def_a. non_enum_variant ( ) . fields ;
261
260
261
+ let mut res = Ok ( ( ) ) ;
262
262
let coerced_fields = fields
263
- . iter ( )
264
- . filter ( | field| {
263
+ . iter_enumerated ( )
264
+ . filter_map ( | ( i , field) | {
265
265
// Ignore PhantomData fields
266
266
let unnormalized_ty = tcx. type_of ( field. did ) . instantiate_identity ( ) ;
267
267
if tcx
@@ -272,7 +272,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
272
272
. unwrap_or ( unnormalized_ty)
273
273
. is_phantom_data ( )
274
274
{
275
- return false ;
275
+ return None ;
276
276
}
277
277
278
278
let ty_a = field. ty ( tcx, args_a) ;
@@ -290,7 +290,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
290
290
&& !ty_a. has_non_region_param ( )
291
291
{
292
292
// ignore 1-ZST fields
293
- return false ;
293
+ return None ;
294
294
}
295
295
296
296
res = Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynZST {
@@ -299,60 +299,57 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
299
299
ty : ty_a,
300
300
} ) ) ;
301
301
302
- return false ;
302
+ None
303
+ } else {
304
+ Some ( ( i, ty_a, ty_b) )
303
305
}
304
-
305
- true
306
306
} )
307
307
. collect :: < Vec < _ > > ( ) ;
308
+ res?;
308
309
309
310
if coerced_fields. is_empty ( ) {
310
- res = Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynSingle {
311
+ return Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynSingle {
311
312
span,
312
313
trait_name : "DispatchFromDyn" ,
313
314
note : true ,
314
315
} ) ) ;
315
316
} else if coerced_fields. len ( ) > 1 {
316
- res = Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynMulti {
317
+ return Err ( tcx. dcx ( ) . emit_err ( errors:: DispatchFromDynMulti {
317
318
span,
318
319
coercions_note : true ,
319
320
number : coerced_fields. len ( ) ,
320
321
coercions : coerced_fields
321
322
. iter ( )
322
- . map ( |field| {
323
- format ! (
324
- "`{}` (`{}` to `{}`)" ,
325
- field. name,
326
- field. ty( tcx, args_a) ,
327
- field. ty( tcx, args_b) ,
328
- )
323
+ . map ( |& ( i, ty_a, ty_b) | {
324
+ format ! ( "`{}` (`{}` to `{}`)" , fields[ i] . name, ty_a, ty_b, )
329
325
} )
330
326
. collect :: < Vec < _ > > ( )
331
327
. join ( ", " ) ,
332
328
} ) ) ;
333
329
} else {
334
330
let ocx = ObligationCtxt :: new_with_diagnostics ( & infcx) ;
335
- for field in coerced_fields {
331
+ for ( _ , ty_a , ty_b ) in coerced_fields {
336
332
ocx. register_obligation ( Obligation :: new (
337
333
tcx,
338
334
cause. clone ( ) ,
339
335
param_env,
340
336
ty:: TraitRef :: new (
341
337
tcx,
342
338
dispatch_from_dyn_trait,
343
- [ field . ty ( tcx , args_a ) , field . ty ( tcx , args_b ) ] ,
339
+ [ ty_a , ty_b ] ,
344
340
) ,
345
341
) ) ;
346
342
}
347
343
let errors = ocx. select_all_or_error ( ) ;
348
344
if !errors. is_empty ( ) {
349
- res = Err ( infcx. err_ctxt ( ) . report_fulfillment_errors ( errors) ) ;
345
+ return Err ( infcx. err_ctxt ( ) . report_fulfillment_errors ( errors) ) ;
350
346
}
351
347
352
348
// Finally, resolve all regions.
353
- res = res . and ( ocx. resolve_regions_and_report_errors ( impl_did, param_env, [ ] ) ) ;
349
+ ocx. resolve_regions_and_report_errors ( impl_did, param_env, [ ] ) ? ;
354
350
}
355
- res
351
+
352
+ Ok ( ( ) )
356
353
}
357
354
_ => Err ( tcx
358
355
. dcx ( )
@@ -558,11 +555,11 @@ pub(crate) fn coerce_unsized_info<'tcx>(
558
555
ocx. register_obligation ( obligation) ;
559
556
let errors = ocx. select_all_or_error ( ) ;
560
557
if !errors. is_empty ( ) {
561
- infcx. err_ctxt ( ) . report_fulfillment_errors ( errors) ;
558
+ return Err ( infcx. err_ctxt ( ) . report_fulfillment_errors ( errors) ) ;
562
559
}
563
560
564
561
// Finally, resolve all regions.
565
- let _ = ocx. resolve_regions_and_report_errors ( impl_did, param_env, [ ] ) ;
562
+ ocx. resolve_regions_and_report_errors ( impl_did, param_env, [ ] ) ? ;
566
563
567
564
Ok ( CoerceUnsizedInfo { custom_kind : kind } )
568
565
}
0 commit comments