@@ -948,6 +948,16 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
948948 /// Equate the inferred type and the annotated type for user type annotations
949949 #[ instrument( skip( self ) , level = "debug" ) ]
950950 fn check_user_type_annotations ( & mut self ) {
951+ // Promoted clone the `user_type_annotations` from their original body, because some
952+ // statements may refer to them. However, we don't want to apply the other type annotations
953+ // while checking the promoted: that would create spurious constraints that we won't be
954+ // able to verify. Instead, the annotations are checked on-demand before relating to the
955+ // actual type in MIR.
956+ if let DefKind :: Promoted = self . tcx ( ) . def_kind ( self . body . source . def_id ( ) ) {
957+ debug ! ( "promoted: skipping user type annotations" ) ;
958+ return ;
959+ }
960+
951961 debug ! ( ?self . user_type_annotations) ;
952962 for user_annotation in self . user_type_annotations {
953963 let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = * user_annotation;
@@ -1012,12 +1022,23 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10121022 locations : Locations ,
10131023 category : ConstraintCategory < ' tcx > ,
10141024 ) -> Fallible < ( ) > {
1015- let annotated_type = self . user_type_annotations [ user_ty. base ] . inferred_ty ;
1025+ let CanonicalUserTypeAnnotation {
1026+ span,
1027+ user_ty : ref canonical_user_ty,
1028+ inferred_ty : annotated_type,
1029+ } = self . user_type_annotations [ user_ty. base ] ;
10161030 trace ! ( ?annotated_type) ;
1017- let mut curr_projected_ty = PlaceTy :: from_ty ( annotated_type) ;
1031+
1032+ // We may have skipped checking the canonical annotation earlier, so do it now.
1033+ if let DefKind :: Promoted = self . tcx ( ) . def_kind ( self . body . source . def_id ( ) ) {
1034+ let annotation =
1035+ self . instantiate_canonical_with_fresh_inference_vars ( span, canonical_user_ty) ;
1036+ self . ascribe_user_type ( annotated_type, annotation, span) ;
1037+ }
10181038
10191039 let tcx = self . infcx . tcx ;
10201040
1041+ let mut curr_projected_ty = PlaceTy :: from_ty ( annotated_type) ;
10211042 for proj in & user_ty. projs {
10221043 if let ty:: Alias ( ty:: Opaque , ..) = curr_projected_ty. ty . kind ( ) {
10231044 // There is nothing that we can compare here if we go through an opaque type.
0 commit comments