@@ -18,14 +18,13 @@ use triomphe::Arc;
1818use crate :: {
1919 autoderef:: { Autoderef , AutoderefKind } ,
2020 db:: HirDatabase ,
21- error_lifetime,
2221 infer:: {
2322 Adjust , Adjustment , AutoBorrow , InferOk , InferenceContext , OverloadedDeref , PointerCast ,
2423 TypeError , TypeMismatch ,
2524 } ,
2625 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 ,
2928} ;
3029
3130use super :: unify:: InferenceTable ;
@@ -301,7 +300,7 @@ impl InferenceTable<'_> {
301300 // Examine the supertype and consider auto-borrowing.
302301 match to_ty. kind ( Interner ) {
303302 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 ) ,
305304 _ => { }
306305 }
307306
@@ -377,11 +376,17 @@ impl InferenceTable<'_> {
377376 /// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`.
378377 /// To match `A` with `B`, autoderef will be performed,
379378 /// 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?
385390 }
386391 _ => return self . unify_and ( & from_ty, to_ty, identity) ,
387392 } ;
@@ -427,8 +432,8 @@ impl InferenceTable<'_> {
427432 // compare those. Note that this means we use the target
428433 // mutability [1], since it may be that we are coercing
429434 // 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 ) ;
432437 match autoderef. table . try_unify ( & derefd_from_ty, to_ty) {
433438 Ok ( result) => {
434439 found = Some ( result. map ( |( ) | derefd_from_ty) ) ;
@@ -472,8 +477,10 @@ impl InferenceTable<'_> {
472477 }
473478
474479 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+ } ) ;
477484
478485 success ( adjustments, ty, goals)
479486 }
@@ -621,11 +628,11 @@ impl InferenceTable<'_> {
621628 ( TyKind :: Ref ( from_mt, _, from_inner) , & TyKind :: Ref ( to_mt, _, _) ) => {
622629 coerce_mutabilities ( * from_mt, to_mt) ?;
623630
624- let lt = error_lifetime ( ) ;
631+ let lt = self . new_lifetime_var ( ) ;
625632 Some ( (
626633 Adjustment { kind : Adjust :: Deref ( None ) , target : from_inner. clone ( ) } ,
627634 Adjustment {
628- kind : Adjust :: Borrow ( AutoBorrow :: Ref ( to_mt) ) ,
635+ kind : Adjust :: Borrow ( AutoBorrow :: Ref ( lt . clone ( ) , to_mt) ) ,
629636 target : TyKind :: Ref ( to_mt, lt, from_inner. clone ( ) ) . intern ( Interner ) ,
630637 } ,
631638 ) )
0 commit comments