@@ -15,7 +15,7 @@ use rustc_hir::{
15
15
use rustc_infer:: infer:: TyCtxtInferExt ;
16
16
use rustc_lint:: { LateContext , LateLintPass } ;
17
17
use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow , AutoBorrowMutability } ;
18
- use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeVisitable , TypeckResults } ;
18
+ use rustc_middle:: ty:: { self , Binder , BoundVariableKind , List , Ty , TyCtxt , TypeVisitable , TypeckResults } ;
19
19
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
20
20
use rustc_span:: { symbol:: sym, Span , Symbol , DUMMY_SP } ;
21
21
use rustc_trait_selection:: infer:: InferCtxtExt ;
@@ -651,7 +651,7 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
651
651
}
652
652
match parent {
653
653
Node :: Local ( Local { ty : Some ( ty) , span, .. } ) if span. ctxt ( ) == ctxt => {
654
- Some ( binding_ty_auto_deref_stability ( cx, ty, precedence) )
654
+ Some ( binding_ty_auto_deref_stability ( cx, ty, precedence, List :: empty ( ) ) )
655
655
} ,
656
656
Node :: Item ( & Item {
657
657
kind : ItemKind :: Static ( ..) | ItemKind :: Const ( ..) ,
@@ -703,13 +703,23 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
703
703
ExprKind :: Ret ( _) => {
704
704
let owner_id = cx. tcx . hir ( ) . body_owner ( cx. enclosing_body . unwrap ( ) ) ;
705
705
Some (
706
- if let Node :: Expr ( Expr {
707
- kind : ExprKind :: Closure ( & Closure { fn_decl, .. } ) ,
708
- ..
709
- } ) = cx. tcx . hir ( ) . get ( owner_id)
706
+ if let Node :: Expr (
707
+ closure @ Expr {
708
+ kind : ExprKind :: Closure ( & Closure { fn_decl, .. } ) ,
709
+ ..
710
+ } ,
711
+ ) = cx. tcx . hir ( ) . get ( owner_id)
710
712
{
711
713
match fn_decl. output {
712
- FnRetTy :: Return ( ty) => binding_ty_auto_deref_stability ( cx, ty, precedence) ,
714
+ FnRetTy :: Return ( ty) => {
715
+ if let Some ( sig) = expr_sig ( cx, closure)
716
+ && let Some ( output) = sig. output ( )
717
+ {
718
+ binding_ty_auto_deref_stability ( cx, ty, precedence, output. bound_vars ( ) )
719
+ } else {
720
+ Position :: Other ( precedence)
721
+ }
722
+ } ,
713
723
FnRetTy :: DefaultReturn ( _) => Position :: Other ( precedence) ,
714
724
}
715
725
} else {
@@ -731,7 +741,7 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
731
741
. map ( |( hir_ty, ty) | match hir_ty {
732
742
// Type inference for closures can depend on how they're called. Only go by the explicit
733
743
// types here.
734
- Some ( ty ) => binding_ty_auto_deref_stability ( cx, ty , precedence) ,
744
+ Some ( hir_ty ) => binding_ty_auto_deref_stability ( cx, hir_ty , precedence, ty . bound_vars ( ) ) ,
735
745
None => ty_auto_deref_stability ( cx, cx. tcx . erase_late_bound_regions ( ty) , precedence)
736
746
. position_for_arg ( ) ,
737
747
} ) ,
@@ -824,7 +834,12 @@ fn walk_parents<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (Position, &
824
834
//
825
835
// Here `y1` and `y2` would resolve to different types, so the type `&Box<_>` is not stable when
826
836
// switching to auto-dereferencing.
827
- fn binding_ty_auto_deref_stability ( cx : & LateContext < ' _ > , ty : & hir:: Ty < ' _ > , precedence : i8 ) -> Position {
837
+ fn binding_ty_auto_deref_stability < ' tcx > (
838
+ cx : & LateContext < ' tcx > ,
839
+ ty : & ' tcx hir:: Ty < ' _ > ,
840
+ precedence : i8 ,
841
+ binder_args : & ' tcx List < BoundVariableKind > ,
842
+ ) -> Position {
828
843
let TyKind :: Rptr ( _, ty) = & ty. kind else {
829
844
return Position :: Other ( precedence) ;
830
845
} ;
@@ -856,31 +871,31 @@ fn binding_ty_auto_deref_stability(cx: &LateContext<'_>, ty: &hir::Ty<'_>, prece
856
871
} else {
857
872
Position :: DerefStable (
858
873
precedence,
859
- cx
860
- . typeck_results ( )
861
- . node_type ( ty. ty . hir_id )
874
+ cx. tcx
875
+ . erase_late_bound_regions ( Binder :: bind_with_vars (
876
+ cx. typeck_results ( ) . node_type ( ty. ty . hir_id ) ,
877
+ binder_args,
878
+ ) )
862
879
. is_sized ( cx. tcx . at ( DUMMY_SP ) , cx. param_env . without_caller_bounds ( ) ) ,
863
880
)
864
881
}
865
882
} ,
866
- TyKind :: Slice ( _)
867
- | TyKind :: Array ( ..)
868
- | TyKind :: BareFn ( _)
869
- | TyKind :: Never
883
+ TyKind :: Slice ( _) => Position :: DerefStable ( precedence, false ) ,
884
+ TyKind :: Array ( ..) | TyKind :: Ptr ( _) | TyKind :: BareFn ( _) => Position :: DerefStable ( precedence, true ) ,
885
+ TyKind :: Never
870
886
| TyKind :: Tup ( _)
871
- | TyKind :: Ptr ( _)
872
887
| TyKind :: Path ( _) => Position :: DerefStable (
873
888
precedence,
874
- cx
875
- . typeck_results ( )
876
- . node_type ( ty. ty . hir_id )
877
- . is_sized ( cx. tcx . at ( DUMMY_SP ) , cx. param_env . without_caller_bounds ( ) ) ,
889
+ cx. tcx
890
+ . erase_late_bound_regions ( Binder :: bind_with_vars (
891
+ cx. typeck_results ( ) . node_type ( ty. ty . hir_id ) ,
892
+ binder_args,
893
+ ) )
894
+ . is_sized ( cx. tcx . at ( DUMMY_SP ) , cx. param_env . without_caller_bounds ( ) ) ,
878
895
) ,
879
- TyKind :: OpaqueDef ( ..)
880
- | TyKind :: Infer
881
- | TyKind :: Typeof ( ..)
882
- | TyKind :: TraitObject ( ..)
883
- | TyKind :: Err => Position :: ReborrowStable ( precedence) ,
896
+ TyKind :: OpaqueDef ( ..) | TyKind :: Infer | TyKind :: Typeof ( ..) | TyKind :: TraitObject ( ..) | TyKind :: Err => {
897
+ Position :: ReborrowStable ( precedence)
898
+ } ,
884
899
} ;
885
900
}
886
901
}
0 commit comments