@@ -453,18 +453,32 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
453
453
}
454
454
_ => ( source, None ) ,
455
455
} ;
456
- let source = source. adjust_for_autoref ( self . tcx , reborrow) ;
456
+ let coerce_source = source. adjust_for_autoref ( self . tcx , reborrow) ;
457
+
458
+ let adjust = Adjust :: DerefRef {
459
+ autoderefs : if reborrow. is_some ( ) { 1 } else { 0 } ,
460
+ autoref : reborrow,
461
+ unsize : true ,
462
+ } ;
463
+
464
+ // Setup either a subtyping or a LUB relationship between
465
+ // the `CoerceUnsized` target type and the expected type.
466
+ // We only have the latter, so we use an inference variable
467
+ // for the former and let type inference do the rest.
468
+ let origin = TypeVariableOrigin :: MiscVariable ( self . cause . span ) ;
469
+ let coerce_target = self . next_ty_var ( origin) ;
470
+ let mut coercion = self . unify_and ( coerce_target, target, adjust) ?;
457
471
458
472
let mut selcx = traits:: SelectionContext :: new ( self ) ;
459
473
460
474
// Use a FIFO queue for this custom fulfillment procedure.
461
475
let mut queue = VecDeque :: new ( ) ;
462
- let mut obligations = vec ! [ ] ;
463
476
464
477
// Create an obligation for `Source: CoerceUnsized<Target>`.
465
478
let cause = ObligationCause :: misc ( self . cause . span , self . body_id ) ;
466
479
queue. push_back ( self . tcx
467
- . predicate_for_trait_def ( cause, coerce_unsized_did, 0 , source, & [ target] ) ) ;
480
+ . predicate_for_trait_def ( cause, coerce_unsized_did, 0 ,
481
+ coerce_source, & [ coerce_target] ) ) ;
468
482
469
483
// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
470
484
// emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
@@ -475,7 +489,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
475
489
let trait_ref = match obligation. predicate {
476
490
ty:: Predicate :: Trait ( ref tr) if traits. contains ( & tr. def_id ( ) ) => tr. clone ( ) ,
477
491
_ => {
478
- obligations. push ( obligation) ;
492
+ coercion . obligations . push ( obligation) ;
479
493
continue ;
480
494
}
481
495
} ;
@@ -503,11 +517,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
503
517
}
504
518
}
505
519
506
- success ( Adjust :: DerefRef {
507
- autoderefs : if reborrow. is_some ( ) { 1 } else { 0 } ,
508
- autoref : reborrow,
509
- unsize : true ,
510
- } , target, obligations)
520
+ Ok ( coercion)
511
521
}
512
522
513
523
fn coerce_from_safe_fn ( & self ,
0 commit comments