@@ -18,14 +18,13 @@ use triomphe::Arc;
18
18
use crate :: {
19
19
autoderef:: { Autoderef , AutoderefKind } ,
20
20
db:: HirDatabase ,
21
- error_lifetime,
22
21
infer:: {
23
22
Adjust , Adjustment , AutoBorrow , InferOk , InferenceContext , OverloadedDeref , PointerCast ,
24
23
TypeError , TypeMismatch ,
25
24
} ,
26
25
utils:: ClosureSubst ,
27
- Canonical , DomainGoal , FnAbi , FnPointer , FnSig , Guidance , InEnvironment , Interner , Solution ,
28
- Substitution , TraitEnvironment , Ty , TyBuilder , TyExt ,
26
+ Canonical , DomainGoal , FnAbi , FnPointer , FnSig , Guidance , InEnvironment , Interner , Lifetime ,
27
+ Solution , Substitution , TraitEnvironment , Ty , TyBuilder , TyExt ,
29
28
} ;
30
29
31
30
use super :: unify:: InferenceTable ;
@@ -301,7 +300,7 @@ impl InferenceTable<'_> {
301
300
// Examine the supertype and consider auto-borrowing.
302
301
match to_ty. kind ( Interner ) {
303
302
TyKind :: Raw ( mt, _) => return self . coerce_ptr ( from_ty, to_ty, * mt) ,
304
- TyKind :: Ref ( mt, _ , _) => return self . coerce_ref ( from_ty, to_ty, * mt) ,
303
+ TyKind :: Ref ( mt, lt , _) => return self . coerce_ref ( from_ty, to_ty, * mt, lt ) ,
305
304
_ => { }
306
305
}
307
306
@@ -377,11 +376,17 @@ impl InferenceTable<'_> {
377
376
/// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`.
378
377
/// To match `A` with `B`, autoderef will be performed,
379
378
/// calling `deref`/`deref_mut` where necessary.
380
- fn coerce_ref ( & mut self , from_ty : Ty , to_ty : & Ty , to_mt : Mutability ) -> CoerceResult {
381
- let from_mt = match from_ty. kind ( Interner ) {
382
- & TyKind :: Ref ( mt, _, _) => {
383
- coerce_mutabilities ( mt, to_mt) ?;
384
- mt
379
+ fn coerce_ref (
380
+ & mut self ,
381
+ from_ty : Ty ,
382
+ to_ty : & Ty ,
383
+ to_mt : Mutability ,
384
+ to_lt : & Lifetime ,
385
+ ) -> CoerceResult {
386
+ let ( _from_lt, from_mt) = match from_ty. kind ( Interner ) {
387
+ TyKind :: Ref ( mt, lt, _) => {
388
+ coerce_mutabilities ( * mt, to_mt) ?;
389
+ ( lt. clone ( ) , * mt) // clone is probably not good?
385
390
}
386
391
_ => return self . unify_and ( & from_ty, to_ty, identity) ,
387
392
} ;
@@ -427,8 +432,8 @@ impl InferenceTable<'_> {
427
432
// compare those. Note that this means we use the target
428
433
// mutability [1], since it may be that we are coercing
429
434
// from `&mut T` to `&U`.
430
- let lt = error_lifetime ( ) ; // FIXME: handle lifetimes correctly, see rustc
431
- let derefd_from_ty = TyKind :: Ref ( to_mt, lt, referent_ty) . intern ( Interner ) ;
435
+ let lt = to_lt ; // FIXME: Involve rustc LUB and SUB flag checks
436
+ let derefd_from_ty = TyKind :: Ref ( to_mt, lt. clone ( ) , referent_ty) . intern ( Interner ) ;
432
437
match autoderef. table . try_unify ( & derefd_from_ty, to_ty) {
433
438
Ok ( result) => {
434
439
found = Some ( result. map ( |( ) | derefd_from_ty) ) ;
@@ -472,8 +477,10 @@ impl InferenceTable<'_> {
472
477
}
473
478
474
479
let mut adjustments = auto_deref_adjust_steps ( & autoderef) ;
475
- adjustments
476
- . push ( Adjustment { kind : Adjust :: Borrow ( AutoBorrow :: Ref ( to_mt) ) , target : ty. clone ( ) } ) ;
480
+ adjustments. push ( Adjustment {
481
+ kind : Adjust :: Borrow ( AutoBorrow :: Ref ( to_lt. clone ( ) , to_mt) ) ,
482
+ target : ty. clone ( ) ,
483
+ } ) ;
477
484
478
485
success ( adjustments, ty, goals)
479
486
}
@@ -621,11 +628,11 @@ impl InferenceTable<'_> {
621
628
( TyKind :: Ref ( from_mt, _, from_inner) , & TyKind :: Ref ( to_mt, _, _) ) => {
622
629
coerce_mutabilities ( * from_mt, to_mt) ?;
623
630
624
- let lt = error_lifetime ( ) ;
631
+ let lt = self . new_lifetime_var ( ) ;
625
632
Some ( (
626
633
Adjustment { kind : Adjust :: Deref ( None ) , target : from_inner. clone ( ) } ,
627
634
Adjustment {
628
- kind : Adjust :: Borrow ( AutoBorrow :: Ref ( to_mt) ) ,
635
+ kind : Adjust :: Borrow ( AutoBorrow :: Ref ( lt . clone ( ) , to_mt) ) ,
629
636
target : TyKind :: Ref ( to_mt, lt, from_inner. clone ( ) ) . intern ( Interner ) ,
630
637
} ,
631
638
) )
0 commit comments