@@ -534,7 +534,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
534
534
return PlaceTy :: from_ty ( self . tcx ( ) . ty_error ( ) ) ;
535
535
}
536
536
}
537
- place_ty = self . sanitize_projection ( place_ty, elem, place, location) ;
537
+ place_ty = self . sanitize_projection ( place_ty, elem, place, location, context ) ;
538
538
}
539
539
540
540
if let PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: Copy ) = context {
@@ -630,12 +630,14 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
630
630
}
631
631
}
632
632
633
+ #[ instrument( skip( self ) , level = "debug" ) ]
633
634
fn sanitize_projection (
634
635
& mut self ,
635
636
base : PlaceTy < ' tcx > ,
636
637
pi : PlaceElem < ' tcx > ,
637
638
place : & Place < ' tcx > ,
638
639
location : Location ,
640
+ context : PlaceContext ,
639
641
) -> PlaceTy < ' tcx > {
640
642
debug ! ( "sanitize_projection: {:?} {:?} {:?}" , base, pi, place) ;
641
643
let tcx = self . tcx ( ) ;
@@ -713,8 +715,11 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
713
715
match self . field_ty ( place, base, field, location) {
714
716
Ok ( ty) => {
715
717
let ty = self . cx . normalize ( ty, location) ;
716
- if let Err ( terr) = self . cx . eq_types (
718
+ debug ! ( ?fty, ?ty) ;
719
+
720
+ if let Err ( terr) = self . cx . relate_types (
717
721
ty,
722
+ self . get_ambient_variance ( context) ,
718
723
fty,
719
724
location. to_locations ( ) ,
720
725
ConstraintCategory :: Boring ,
@@ -743,9 +748,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
743
748
let ty = self . sanitize_type ( place, ty) ;
744
749
let ty = self . cx . normalize ( ty, location) ;
745
750
self . cx
746
- . eq_types (
747
- base. ty ,
751
+ . relate_types (
748
752
ty,
753
+ self . get_ambient_variance ( context) ,
754
+ base. ty ,
749
755
location. to_locations ( ) ,
750
756
ConstraintCategory :: TypeAnnotation ,
751
757
)
@@ -760,6 +766,21 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
760
766
self . tcx ( ) . ty_error ( )
761
767
}
762
768
769
+ fn get_ambient_variance ( & self , context : PlaceContext ) -> ty:: Variance {
770
+ use rustc_middle:: mir:: visit:: NonMutatingUseContext :: * ;
771
+ use rustc_middle:: mir:: visit:: NonUseContext :: * ;
772
+
773
+ match context {
774
+ PlaceContext :: MutatingUse ( _) => ty:: Invariant ,
775
+ PlaceContext :: NonUse ( StorageDead | StorageLive | VarDebugInfo ) => ty:: Invariant ,
776
+ PlaceContext :: NonMutatingUse (
777
+ Inspect | Copy | Move | SharedBorrow | ShallowBorrow | UniqueBorrow | AddressOf
778
+ | Projection ,
779
+ ) => ty:: Covariant ,
780
+ PlaceContext :: NonUse ( AscribeUserTy ) => ty:: Covariant ,
781
+ }
782
+ }
783
+
763
784
fn field_ty (
764
785
& mut self ,
765
786
parent : & dyn fmt:: Debug ,
0 commit comments