@@ -534,7 +534,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
534534 return PlaceTy :: from_ty ( self . tcx ( ) . ty_error ( ) ) ;
535535 }
536536 }
537- place_ty = self . sanitize_projection ( place_ty, elem, place, location) ;
537+ place_ty = self . sanitize_projection ( place_ty, elem, place, location, context ) ;
538538 }
539539
540540 if let PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: Copy ) = context {
@@ -630,12 +630,14 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
630630 }
631631 }
632632
633+ #[ instrument( skip( self ) , level = "debug" ) ]
633634 fn sanitize_projection (
634635 & mut self ,
635636 base : PlaceTy < ' tcx > ,
636637 pi : PlaceElem < ' tcx > ,
637638 place : & Place < ' tcx > ,
638639 location : Location ,
640+ context : PlaceContext ,
639641 ) -> PlaceTy < ' tcx > {
640642 debug ! ( "sanitize_projection: {:?} {:?} {:?}" , base, pi, place) ;
641643 let tcx = self . tcx ( ) ;
@@ -713,8 +715,11 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
713715 match self . field_ty ( place, base, field, location) {
714716 Ok ( ty) => {
715717 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 (
717721 ty,
722+ self . get_ambient_variance ( context) ,
718723 fty,
719724 location. to_locations ( ) ,
720725 ConstraintCategory :: Boring ,
@@ -743,9 +748,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
743748 let ty = self . sanitize_type ( place, ty) ;
744749 let ty = self . cx . normalize ( ty, location) ;
745750 self . cx
746- . eq_types (
747- base. ty ,
751+ . relate_types (
748752 ty,
753+ self . get_ambient_variance ( context) ,
754+ base. ty ,
749755 location. to_locations ( ) ,
750756 ConstraintCategory :: TypeAnnotation ,
751757 )
@@ -760,6 +766,13 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
760766 self . tcx ( ) . ty_error ( )
761767 }
762768
769+ fn get_ambient_variance ( & self , context : PlaceContext ) -> ty:: Variance {
770+ match context {
771+ PlaceContext :: MutatingUse ( _) => ty:: Invariant ,
772+ PlaceContext :: NonMutatingUse ( _) | PlaceContext :: NonUse ( _) => ty:: Covariant ,
773+ }
774+ }
775+
763776 fn field_ty (
764777 & mut self ,
765778 parent : & dyn fmt:: Debug ,
0 commit comments