@@ -948,6 +948,16 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
948
948
/// Equate the inferred type and the annotated type for user type annotations
949
949
#[ instrument( skip( self ) , level = "debug" ) ]
950
950
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
+
951
961
debug ! ( ?self . user_type_annotations) ;
952
962
for user_annotation in self . user_type_annotations {
953
963
let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = * user_annotation;
@@ -1012,12 +1022,23 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
1012
1022
locations : Locations ,
1013
1023
category : ConstraintCategory < ' tcx > ,
1014
1024
) -> 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 ] ;
1016
1030
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
+ }
1018
1038
1019
1039
let tcx = self . infcx . tcx ;
1020
1040
1041
+ let mut curr_projected_ty = PlaceTy :: from_ty ( annotated_type) ;
1021
1042
for proj in & user_ty. projs {
1022
1043
if let ty:: Alias ( ty:: Opaque , ..) = curr_projected_ty. ty . kind ( ) {
1023
1044
// There is nothing that we can compare here if we go through an opaque type.
0 commit comments